/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
1
# init.py
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
2
#
3
# Copyright (C) 2013 Tim Marston <tim@edm.am>
4
#
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
22
import sys, os, re, getopt, shutil, subprocess
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
23
from command import Command
24
import stdhome.the as the
25
from stdhome.deployment import Deployment
26
27
28
class InitCommand( Command ):
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
29
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
30
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
31
	def print_help( self ):
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
32
		print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
33
		print
34
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
35
		print "Initialise a local repository."
36
		print
37
		print "If an URL is given, the local reposity is a checkout of it (i.e., you can"
38
		print "receive updates from it and changes you commit will be sent to it).  The URL"
39
		print "can take the form of a simple hostname, such as \"example.com\", or it can be a"
40
		print "fully-qualified bazaar URL.  (Actually, in the first case, where it is a simple"
41
		print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
42
		print
43
		print "Options:"
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
44
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
45
		print "  -v, --verbose    display information about what is being done"
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
46
		print "      --help       display help and exit"
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
47
		exit( 0 )
48
49
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
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:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
55
			if opt in [ '--repo', '-r' ]:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
56
				if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
57
					raise the.program.FatalError(
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
58
						'invalid repository name: ' + optarg )
17 by Tim Marston
read ~/.stdhomerc; commands set repo before run(); program performs late
59
				the.repo = optarg
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
60
			elif opt in [ '--verbose', '-v' ]:
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
61
				the.verbose += 1
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
62
			elif opt == "--help":
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
63
				self.print_help()
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
64
		
65
		# discard first argument (the command)
66
		args.pop( 0 )
67
68
		# URL argument
69
		self.url = args[ 0 ].strip() if len( args ) else None
70
35 by Tim Marston
made checks for verbose >= 1 explicit
71
		# check remaining arguments
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
72
		if len( args ) > 1:
73
			raise the.program.UsageError( 'too many arguments' )
74
75
76
	def run( self ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
77
78
		# set up repo and check it *doesn't* already exists
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
79
		the.repo.check_dir_exists( False )
80
81
		# ensure our top-level directory exists
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
82
		if not os.path.exists( the.full_dir ):
83
			os.mkdir( the.full_dir )
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
84
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
85
		# checkout a remote repo, or create an empty local one?
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
86
		if self.url:
87
88
			# expand url if it's a simple hostname
89
			if re.match( '^[0-9a-zA-z.]+$', self.url ):
45 by Tim Marston
switch to using bzr+ssh, rather than sftp (to work around broken pimiko)
90
				self.url = 'bzr+ssh://%s/%s/%s' % \
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
91
						   ( self.url, the.dir, the.repo.name )
92
93
			# initialise deployment (with an empty repo)
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
94
			deployment = Deployment()
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
95
			deployment.copy_in()
96
97
			# perform bzr checkout
35 by Tim Marston
made checks for verbose >= 1 explicit
98
			if the.verbose >= 1: print "checking out %s" % the.repo.dir
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
99
			try:
100
				the.repo.vcs.checkout( self.url )
101
			except Exception as e:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
102
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
103
				# attempt to clean-up repo dir
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
104
				try:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
105
					shutil.rmtree( the.repo.full_dir )
106
				except OSError:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
107
					pass
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
108
109
				raise e
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
110
111
			# perform deployment
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
112
			try:
113
				deployment.copy_out()
114
			except deployment.Conflict as e:
115
				raise the.program.FatalError( e.msg )
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
116
117
		else:
118
119
			# perform bzr init
35 by Tim Marston
made checks for verbose >= 1 explicit
120
			if the.verbose >= 1: print 'initialising %s' % the.repo.dir
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
121
			try:
122
				the.repo.vcs.init()
123
			except Exception as e:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
124
125
				# attempt to clean-up repo dir, and die
126
				try:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
127
					shutil.rmtree( the.repo.full_dir )
128
				except OSError:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
129
					pass
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
130
131
				raise e