/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/command/init.py

  • Committer: Tim Marston
  • Date: 2016-02-23 22:37:03 UTC
  • Revision ID: tim@ed.am-20160223223703-sx94svvstwt8xvrb
determine and instantiate repo vcs dynamically; for new repos, added default vcs
configuration option and allow override in init command arguments; re-added
handling of -v/--verbose arguments to commands and removed from program (since
there may be problems parsing all arguments here)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# command_init.py
 
1
# init.py
2
2
#
3
3
# Copyright (C) 2013 Tim Marston <tim@edm.am>
4
4
#
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import sys, os
23
 
import getopt
24
 
 
25
 
 
26
 
class CommandInit:
 
22
import sys, os, re, getopt, shutil, subprocess
 
23
from command import Command
 
24
import stdhome.the as the
 
25
from stdhome.deployment import Deployment
 
26
 
 
27
 
 
28
class InitCommand( Command ):
 
29
 
 
30
 
 
31
        def __init__( self ):
 
32
                self.quiet = False
 
33
                self.vcs = the.config.default_vcs
 
34
 
27
35
 
28
36
        def print_help( self ):
29
 
                print "Usage: " + self.program + " init [URL] [--repo=REPO]"
 
37
                print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
30
38
                print
31
39
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
32
40
                print "Initialise a local repository."
38
46
                print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
39
47
                print
40
48
                print "Options:"
41
 
                print "     --repo     select the repo to check-out or create (defaults to 'home')"
42
 
                print "     --help     display help and exit"
 
49
                print "      --quiet      do not report changes to the home directory"
 
50
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
 
51
                print "  -v, --verbose    display information about what is being done"
 
52
                print "      --vcs=VCS    select the version control system"
 
53
                print "      --help       display help and exit"
43
54
                exit( 0 )
44
55
 
45
56
 
46
 
        def run( self, stdhome ):
47
 
                # parse command line
48
 
                try:
49
 
                        opts, args = getopt.gnu_getopt(
50
 
                                sys.argv[ 1: ], "",
51
 
                                [ "repo:", "help" ] )
52
 
                except getopt.GetoptError as e:
53
 
                        stdhome.print_usage( e )
54
 
                for opt, optargs in opts:
55
 
                        if opt == "--help":
 
57
        def parse_command_line( self ):
 
58
                opts, args = getopt.gnu_getopt(
 
59
                        sys.argv[ 1: ], "r:v",
 
60
                        [ "quiet", "repo=", "vcs=", "verbose", "help" ] )
 
61
                for opt, optarg in opts:
 
62
                        if opt == "--quiet":
 
63
                                self.quiet = True
 
64
                        elif opt in [ '--repo', '-r' ]:
 
65
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
 
66
                                        raise the.program.FatalError(
 
67
                                                'invalid repository name: ' + optarg )
 
68
                                the.repo = optarg
 
69
                        elif opt == "--vcs":
 
70
                                self.vcs = optarg
 
71
                        elif opt in [ '--verbose', '-v' ]:
 
72
                                the.verbose += 1
 
73
                        elif opt == "--help":
56
74
                                self.print_help()
57
 
                        elif aopt == "--repo":
58
 
                                self.repo = optarg
59
 
 
60
 
                print "init"
 
75
 
 
76
                # discard first argument (the command)
 
77
                args.pop( 0 )
 
78
 
 
79
                # URL argument
 
80
                self.url = args[ 0 ].strip() if len( args ) else None
 
81
 
 
82
                # check remaining arguments
 
83
                if len( args ) > 1:
 
84
                        raise the.program.UsageError( 'too many arguments' )
 
85
 
 
86
 
 
87
        def run( self ):
 
88
 
 
89
                # set up repo and check it *doesn't* already exists
 
90
                the.repo.check_dir_exists( False )
 
91
                the.repo.set_vcs( self.vcs )
 
92
 
 
93
                # ensure our top-level directory exists
 
94
                if not os.path.exists( the.full_dir ):
 
95
                        os.mkdir( the.full_dir )
 
96
 
 
97
                # checkout a remote repo, or create an empty local one?
 
98
                if self.url:
 
99
 
 
100
                        # expand url if it's a simple hostname
 
101
                        if re.search( '^[0-9a-zA-z.]+$', self.url ):
 
102
                                self.url = the.repo.vcs.expand_repo_url( self.url )
 
103
 
 
104
                        # initialise deployment (with an empty repo)
 
105
                        deployment = Deployment()
 
106
                        deployment.copy_in()
 
107
 
 
108
                        # perform bzr checkout
 
109
                        if the.verbose >= 1: print "checking out %s" % the.repo.dir
 
110
                        try:
 
111
                                the.repo.vcs.checkout( self.url )
 
112
                        except Exception:
 
113
 
 
114
                                # attempt to clean-up repo dir
 
115
                                if os.path.exists( the.repo.full_dir ):
 
116
                                        try:
 
117
                                                shutil.rmtree( the.repo.full_dir )
 
118
                                        except OSError:
 
119
                                                pass
 
120
 
 
121
                                raise
 
122
 
 
123
                        message = ''
 
124
 
 
125
                        # check for deployment conclicts
 
126
                        conflicts = deployment.get_conflicts()
 
127
                        if conflicts:
 
128
                                message += 'deployment conflicts:\n  %s' % \
 
129
                                                   '\n  '.join( conflicts )
 
130
 
 
131
                        # stop if there are conflicts
 
132
                        if message:
 
133
                                raise the.program.FatalError(
 
134
                                        'there were conflicts...\n' + message )
 
135
 
 
136
                        # perform deployment
 
137
                        deployment.copy_out( self.quiet )
 
138
 
 
139
                else:
 
140
 
 
141
                        # perform bzr init
 
142
                        if the.verbose >= 1: print 'initialising %s' % the.repo.dir
 
143
                        try:
 
144
                                the.repo.vcs.init()
 
145
                        except Exception:
 
146
 
 
147
                                # attempt to clean-up repo dir, and die
 
148
                                if os.path.exists( the.repo.full_dir ):
 
149
                                        try:
 
150
                                                shutil.rmtree( the.repo.full_dir )
 
151
                                        except OSError:
 
152
                                                pass
 
153
 
 
154
                                raise