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