/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

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
 
import the
24
 
from deployment import Deployment
25
 
from subprocess import Popen
 
22
import sys, os
 
23
import getopt
26
24
 
27
25
 
28
26
class CommandInit:
29
27
 
30
 
 
31
 
        def __init__( self ):
32
 
                self.repo = None
33
 
 
34
 
 
35
28
        def print_help( self ):
36
 
                print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
 
29
                print "Usage: " + self.program + " init [URL] [--repo=REPO]"
37
30
                print
38
31
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
39
32
                print "Initialise a local repository."
45
38
                print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
46
39
                print
47
40
                print "Options:"
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"
 
41
                print "     --repo     select the repo to check-out or create (defaults to 'home')"
 
42
                print "     --help     display help and exit"
51
43
                exit( 0 )
52
44
 
53
45
 
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 )
 
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":
 
56
                                self.print_help()
 
57
                        elif aopt == "--repo":
63
58
                                self.repo = optarg
64
 
                        elif opt in [ '--verbose', '-v' ]:
65
 
                                the.verbose = True
66
 
                        elif opt == "--help":
67
 
                                self.print_help()
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
 
59
 
 
60
                print "init"