/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 19:35:21 UTC
  • Revision ID: tim@ed.am-20160223193521-2vgtxbfos50rrpku
renamed version -> VERSION

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from command import Command
24
24
import stdhome.the as the
25
25
from stdhome.deployment import Deployment
26
 
from stdhome.subprocess import Popen
27
26
 
28
27
 
29
28
class InitCommand( Command ):
30
29
 
31
30
 
32
 
        def __init__( self ):
33
 
                self.repo = None
34
 
 
35
 
 
36
31
        def print_help( self ):
37
32
                print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
38
33
                print
46
41
                print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
47
42
                print
48
43
                print "Options:"
 
44
                print "      --quiet      do not report changes to the home directory"
49
45
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
50
46
                print "  -v, --verbose    display information about what is being done"
51
47
                print "      --help       display help and exit"
55
51
        def parse_command_line( self ):
56
52
                opts, args = getopt.gnu_getopt(
57
53
                        sys.argv[ 1: ], "r:v",
58
 
                        [ "repo=", "verbose", "help" ] )
 
54
                        [ "quiet", "repo=", "verbose", "help" ] )
59
55
                for opt, optarg in opts:
60
 
                        if opt in [ '--repo', '-r' ]:
61
 
                                if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
 
56
                        if opt == "--quiet":
 
57
                                self.quiet = True
 
58
                        elif opt in [ '--repo', '-r' ]:
 
59
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
62
60
                                        raise the.program.FatalError(
63
61
                                                'invalid repository name: ' + optarg )
64
 
                                self.repo = optarg
65
 
                        elif opt in [ '--verbose', '-v' ]:
66
 
                                the.verbose += 1
 
62
                                the.repo = optarg
67
63
                        elif opt == "--help":
68
64
                                self.print_help()
69
 
                
 
65
 
70
66
                # discard first argument (the command)
71
67
                args.pop( 0 )
72
68
 
73
69
                # URL argument
74
70
                self.url = args[ 0 ].strip() if len( args ) else None
75
71
 
76
 
                # remaining arguments
 
72
                # check remaining arguments
77
73
                if len( args ) > 1:
78
74
                        raise the.program.UsageError( 'too many arguments' )
79
75
 
81
77
        def run( self ):
82
78
 
83
79
                # set up repo and check it *doesn't* already exists
84
 
                the.set_repo( self.repo )
85
80
                the.repo.check_dir_exists( False )
86
81
 
87
82
                # ensure our top-level directory exists
92
87
                if self.url:
93
88
 
94
89
                        # expand url if it's a simple hostname
95
 
                        if re.match( '^[0-9a-zA-z.]+$', self.url ):
96
 
                                self.url = 'sftp://%s/%s/%s' % \
 
90
                        if re.search( '^[0-9a-zA-z.]+$', self.url ):
 
91
                                self.url = 'bzr+ssh://%s/%s/%s' % \
97
92
                                                   ( self.url, the.dir, the.repo.name )
98
93
 
99
94
                        # initialise deployment (with an empty repo)
101
96
                        deployment.copy_in()
102
97
 
103
98
                        # perform bzr checkout
104
 
                        if the.verbose: print "checking out %s" % the.repo.dir
 
99
                        if the.verbose >= 1: print "checking out %s" % the.repo.dir
105
100
                        try:
106
101
                                the.repo.vcs.checkout( self.url )
107
102
                        except Exception as e:
114
109
 
115
110
                                raise e
116
111
 
 
112
                        message = ''
 
113
 
 
114
                        # check for deployment conclicts
 
115
                        conflicts = deployment.get_conflicts()
 
116
                        if conflicts:
 
117
                                message += 'deployment conflicts:\n  %s' % \
 
118
                                                   '\n  '.join( conflicts )
 
119
 
 
120
                        # stop if there are conflicts
 
121
                        if message:
 
122
                                raise the.program.FatalError(
 
123
                                        'there were conflicts...\n' + message )
 
124
 
117
125
                        # perform deployment
118
 
                        try:
119
 
                                deployment.copy_out()
120
 
                        except deployment.Conflict as e:
121
 
                                raise the.program.FatalError( e.msg )
 
126
                        deployment.copy_out( self.quiet )
122
127
 
123
128
                else:
124
129
 
125
130
                        # perform bzr init
126
 
                        if the.verbose: print 'initialising %s' % the.repo.dir
 
131
                        if the.verbose >= 1: print 'initialising %s' % the.repo.dir
127
132
                        try:
128
133
                                the.repo.vcs.init()
129
134
                        except Exception as e: