19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import sys, os, re, getopt, shutil, subprocess
24
from deployment import Deployment
25
from subprocess import Popen
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]"
38
31
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
39
32
print "Initialise a local repository."
45
38
print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
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"
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 ):
49
opts, args = getopt.gnu_getopt(
52
except getopt.GetoptError as e:
53
stdhome.print_usage( e )
54
for opt, optargs in opts:
57
elif aopt == "--repo":
64
elif opt in [ '--verbose', '-v' ]:
69
# discard first argument (the command)
73
self.url = args[ 0 ].strip() if len( args ) else None
77
raise the.program.UsageError( 'too many arguments' )
82
# set up repo and check it *doesn't* already exists
83
the.set_repo( self.repo )
84
the.repo.check_dir_exists( False )
86
# ensure our top-level directory exists
87
if not os.path.exists( the.full_dir ):
88
os.mkdir( the.full_dir )
90
# checkout a remote repo, or create an empty local one?
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 )
98
# initialise deployment (with an empty repo)
99
deployment = Deployment()
102
# perform bzr checkout
103
if the.verbose: print "checking out %s" % the.repo.dir
105
the.repo.vcs.checkout( self.url )
106
except Exception as e:
108
# attempt to clean-up repo dir
110
shutil.rmtree( the.repo.full_dir )
118
deployment.copy_out()
119
except deployment.Conflict as e:
120
raise the.program.FatalError( e.msg )
125
if the.verbose: print 'initialising %s' % the.repo.dir
128
except Exception as e:
130
# attempt to clean-up repo dir, and die
132
shutil.rmtree( the.repo.full_dir )