19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
28
class InitCommand( Command ):
28
31
def print_help( self ):
29
print "Usage: " + self.program + " init [URL] [--repo=REPO]"
32
print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
31
34
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
32
35
print "Initialise a local repository."
38
41
print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
41
print " --repo select the repo to check-out or create (defaults to 'home')"
42
print " --help display help and exit"
44
print " -r, --repo=REPO select the repo to check-out or create (defaults to 'home')"
45
print " -v, --verbose display information about what is being done"
46
print " --help display help and exit"
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:
50
def parse_command_line( self ):
51
opts, args = getopt.gnu_getopt(
52
sys.argv[ 1: ], "r:v",
53
[ "repo=", "verbose", "help" ] )
54
for opt, optarg in opts:
55
if opt in [ '--repo', '-r' ]:
56
if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
57
raise the.program.FatalError(
58
'invalid repository name: ' + optarg )
60
elif opt in [ '--verbose', '-v' ]:
57
elif aopt == "--repo":
65
# discard first argument (the command)
69
self.url = args[ 0 ].strip() if len( args ) else None
71
# check remaining arguments
73
raise the.program.UsageError( 'too many arguments' )
78
# set up repo and check it *doesn't* already exists
79
the.repo.check_dir_exists( False )
81
# ensure our top-level directory exists
82
if not os.path.exists( the.full_dir ):
83
os.mkdir( the.full_dir )
85
# checkout a remote repo, or create an empty local one?
88
# expand url if it's a simple hostname
89
if re.match( '^[0-9a-zA-z.]+$', self.url ):
90
self.url = 'bzr+ssh://%s/%s/%s' % \
91
( self.url, the.dir, the.repo.name )
93
# initialise deployment (with an empty repo)
94
deployment = Deployment()
97
# perform bzr checkout
98
if the.verbose >= 1: print "checking out %s" % the.repo.dir
100
the.repo.vcs.checkout( self.url )
101
except Exception as e:
103
# attempt to clean-up repo dir
105
shutil.rmtree( the.repo.full_dir )
113
deployment.copy_out()
114
except deployment.Conflict as e:
115
raise the.program.FatalError( e.msg )
120
if the.verbose >= 1: print 'initialising %s' % the.repo.dir
123
except Exception as e:
125
# attempt to clean-up repo dir, and die
127
shutil.rmtree( the.repo.full_dir )