/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:49 UTC
  • Revision ID: tim@ed.am-20140212215149-msaxl7vo98il5i4a
added more commands

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
#
20
20
 
21
21
 
22
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 ):
 
23
import the
 
24
from deployment import Deployment
 
25
from subprocess import Popen
 
26
 
 
27
 
 
28
class CommandInit:
 
29
 
 
30
 
 
31
        def __init__( self ):
 
32
                self.repo = None
29
33
 
30
34
 
31
35
        def print_help( self ):
56
60
                                if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
57
61
                                        raise the.program.FatalError(
58
62
                                                'invalid repository name: ' + optarg )
59
 
                                the.repo = optarg
 
63
                                self.repo = optarg
 
64
                        elif opt in [ '--verbose', '-v' ]:
 
65
                                the.verbose = True
60
66
                        elif opt == "--help":
61
67
                                self.print_help()
62
 
 
 
68
                
63
69
                # discard first argument (the command)
64
70
                args.pop( 0 )
65
71
 
66
72
                # URL argument
67
73
                self.url = args[ 0 ].strip() if len( args ) else None
68
74
 
69
 
                # check remaining arguments
 
75
                # remaining arguments
70
76
                if len( args ) > 1:
71
77
                        raise the.program.UsageError( 'too many arguments' )
72
78
 
74
80
        def run( self ):
75
81
 
76
82
                # set up repo and check it *doesn't* already exists
 
83
                the.set_repo( self.repo )
77
84
                the.repo.check_dir_exists( False )
78
85
 
79
86
                # ensure our top-level directory exists
85
92
 
86
93
                        # expand url if it's a simple hostname
87
94
                        if re.match( '^[0-9a-zA-z.]+$', self.url ):
88
 
                                self.url = 'bzr+ssh://%s/%s/%s' % \
 
95
                                self.url = 'sftp://%s/%s/%s' % \
89
96
                                                   ( self.url, the.dir, the.repo.name )
90
97
 
91
98
                        # initialise deployment (with an empty repo)
93
100
                        deployment.copy_in()
94
101
 
95
102
                        # perform bzr checkout
96
 
                        if the.verbose >= 1: print "checking out %s" % the.repo.dir
 
103
                        if the.verbose: print "checking out %s" % the.repo.dir
97
104
                        try:
98
105
                                the.repo.vcs.checkout( self.url )
99
106
                        except Exception as e:
106
113
 
107
114
                                raise e
108
115
 
109
 
                        message = ''
110
 
 
111
 
                        # check for deployment conclicts
112
 
                        conflicts = deployment.get_conflicts()
113
 
                        if conflicts:
114
 
                                message += 'deployment conflicts:\n  %s' % \
115
 
                                                   '\n  '.join( conflicts )
116
 
 
117
 
                        # stop if there are conflicts
118
 
                        if message:
119
 
                                raise the.program.FatalError(
120
 
                                        'there were conflicts...\n' + message )
121
 
 
122
116
                        # perform deployment
123
 
                        deployment.copy_out()
 
117
                        try:
 
118
                                deployment.copy_out()
 
119
                        except deployment.Conflict as e:
 
120
                                raise the.program.FatalError( e.msg )
124
121
 
125
122
                else:
126
123
 
127
124
                        # perform bzr init
128
 
                        if the.verbose >= 1: print 'initialising %s' % the.repo.dir
 
125
                        if the.verbose: print 'initialising %s' % the.repo.dir
129
126
                        try:
130
127
                                the.repo.vcs.init()
131
128
                        except Exception as e: