/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: 2013-12-08 19:28:11 UTC
  • Revision ID: tim@ed.am-20131208192811-r20qj7cgmn4duw11
initial commit; basic app startup and initial command-line processing

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
#
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
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
 
 
 
22
import sys, os
 
23
import getopt
 
24
 
 
25
 
 
26
class CommandInit:
30
27
 
31
28
        def print_help( self ):
32
 
                print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
 
29
                print "Usage: " + self.program + " init [URL] [--repo=REPO]"
33
30
                print
34
31
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
35
32
                print "Initialise a local repository."
41
38
                print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
42
39
                print
43
40
                print "Options:"
44
 
                print "      --quiet      do not report changes to the home directory"
45
 
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
46
 
                print "  -v, --verbose    display information about what is being done"
47
 
                print "      --help       display help and exit"
 
41
                print "     --repo     select the repo to check-out or create (defaults to 'home')"
 
42
                print "     --help     display help and exit"
48
43
                exit( 0 )
49
44
 
50
45
 
51
 
        def parse_command_line( self ):
52
 
                opts, args = getopt.gnu_getopt(
53
 
                        sys.argv[ 1: ], "r:v",
54
 
                        [ "quiet", "repo=", "verbose", "help" ] )
55
 
                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 ):
60
 
                                        raise the.program.FatalError(
61
 
                                                'invalid repository name: ' + optarg )
62
 
                                the.repo = optarg
63
 
                        elif opt == "--help":
 
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":
64
56
                                self.print_help()
65
 
 
66
 
                # discard first argument (the command)
67
 
                args.pop( 0 )
68
 
 
69
 
                # URL argument
70
 
                self.url = args[ 0 ].strip() if len( args ) else None
71
 
 
72
 
                # check remaining arguments
73
 
                if len( args ) > 1:
74
 
                        raise the.program.UsageError( 'too many arguments' )
75
 
 
76
 
 
77
 
        def run( self ):
78
 
 
79
 
                # set up repo and check it *doesn't* already exists
80
 
                the.repo.check_dir_exists( False )
81
 
 
82
 
                # ensure our top-level directory exists
83
 
                if not os.path.exists( the.full_dir ):
84
 
                        os.mkdir( the.full_dir )
85
 
 
86
 
                # checkout a remote repo, or create an empty local one?
87
 
                if self.url:
88
 
 
89
 
                        # 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' % \
92
 
                                                   ( self.url, the.dir, the.repo.name )
93
 
 
94
 
                        # initialise deployment (with an empty repo)
95
 
                        deployment = Deployment()
96
 
                        deployment.copy_in()
97
 
 
98
 
                        # perform bzr checkout
99
 
                        if the.verbose >= 1: print "checking out %s" % the.repo.dir
100
 
                        try:
101
 
                                the.repo.vcs.checkout( self.url )
102
 
                        except Exception as e:
103
 
 
104
 
                                # attempt to clean-up repo dir
105
 
                                try:
106
 
                                        shutil.rmtree( the.repo.full_dir )
107
 
                                except OSError:
108
 
                                        pass
109
 
 
110
 
                                raise e
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
 
 
125
 
                        # perform deployment
126
 
                        deployment.copy_out( self.quiet )
127
 
 
128
 
                else:
129
 
 
130
 
                        # perform bzr init
131
 
                        if the.verbose >= 1: print 'initialising %s' % the.repo.dir
132
 
                        try:
133
 
                                the.repo.vcs.init()
134
 
                        except Exception as e:
135
 
 
136
 
                                # attempt to clean-up repo dir, and die
137
 
                                try:
138
 
                                        shutil.rmtree( the.repo.full_dir )
139
 
                                except OSError:
140
 
                                        pass
141
 
 
142
 
                                raise e
 
57
                        elif aopt == "--repo":
 
58
                                self.repo = optarg
 
59
 
 
60
                print "init"