/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: 2014-03-08 00:40:42 UTC
  • Revision ID: tim@ed.am-20140308004042-j5rco14bjp3teegj
updated .bzrignore

Show diffs side-by-side

added added

removed removed

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