/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-13 13:30:50 UTC
  • Revision ID: tim@ed.am-20160213133050-4xw8cgs2lp34zq9c
remove set declaration syntax (not compatible w/ python 2.6)

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