/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-19 20:02:10 UTC
  • Revision ID: tim@ed.am-20140319200210-b6nm63rpktfmw0l3
changed working of output

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