/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-02-12 21:51:08 UTC
  • Revision ID: tim@ed.am-20140212215108-stk5z0nlvgpi4oa8
added bzr as a vcs backend; finished init command; implemented deployment

Show diffs side-by-side

added added

removed removed

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