/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
1
# command_init.py
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
23
import the, deployment
24
from subprocess import Popen
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
25
26
27
class CommandInit:
28
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
29
30
	def __init__( self ):
31
		self.repo = None
32
33
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
34
	def print_help( self ):
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
35
		print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
36
		print
37
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
38
		print "Initialise a local repository."
39
		print
40
		print "If an URL is given, the local reposity is a checkout of it (i.e., you can"
41
		print "receive updates from it and changes you commit will be sent to it).  The URL"
42
		print "can take the form of a simple hostname, such as \"example.com\", or it can be a"
43
		print "fully-qualified bazaar URL.  (Actually, in the first case, where it is a simple"
44
		print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
45
		print
46
		print "Options:"
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
47
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
48
		print "      --help       display help and exit"
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
49
		exit( 0 )
50
51
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
52
	def parse_command_line( self ):
53
		opts, args = getopt.gnu_getopt(
54
			sys.argv[ 1: ], "r:v",
55
			[ "repo=", "verbose", "help" ] )
56
		for opt, optarg in opts:
57
			if [ '--repo', '-r' ].count( opt ):
58
				if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
59
					raise the.FatalError(
60
						'invalid repository name: ' + optarg )
61
				self.repo = optarg
62
			elif [ '--verbose', '-v' ].count( opt ):
63
				the.verbose = True
64
			elif opt == "--help":
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
65
				self.print_help()
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
66
		
67
		# discard first argument (the command)
68
		args.pop( 0 )
69
70
		# URL argument
71
		self.url = args[ 0 ].strip() if len( args ) else None
72
73
		# remaining arguments
74
		if len( args ) > 1:
75
			raise the.program.UsageError( 'too many arguments' )
76
77
78
	def run( self ):
79
		the.set_repo( self.repo )
80
81
		# repo dir must not already exist
82
		the.repo.check_dir_exists( False )
83
84
		# ensure our top-level directory exists
85
		if not os.path.exists( the.expanded_dir ):
86
			os.mkdir( the.expanded_dir )
87
88
		if self.url:
89
90
			# expand url if it's a simple hostname
91
			if re.match( '^[0-9a-zA-z.]+$', self.url ):
92
				self.url = 'sftp://%s/%s/%s' % \
93
						   ( self.url, the.dir, the.repo.name )
94
95
			# initialise deployment (with an empty repo)
96
			deployment = Deployment( self.repo )
97
			deployment.copy_in()
98
99
			# perform bzr checkout
100
			print "checking out %s" % the.repo.dir
101
			p = Popen( [ 'bzr', 'co', self.url, the.repo.name ],
102
					   cwd = the.expanded_dir,
103
					   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
104
			out = p.communicate()[ 0 ]
105
			if p.returncode > 0:
106
107
				# attempt to clean-up repo dir, and die
108
				try:
109
					shutil.rmtree( the.repo.expanded_dir )
110
				except OSError as e:
111
					pass
112
				raise the.program.FatalError( 'checkout failed', out )
113
114
			# perform deployment
115
			deployment.copy_out()
116
117
		else:
118
119
			# perform bzr init
120
			print 'creating %s' % the.repo.dir
121
			p = Popen( [ 'bzr', 'init', the.repo.name ],
122
					   cwd = the.expanded_dir,
123
					   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
124
			out = p.communicate()[ 0 ]
125
			if p.returncode > 0:
126
127
				# attempt to clean-up repo dir, and die
128
				try:
129
					shutil.rmtree( the.repo.expanded_dir )
130
				except OSError as e:
131
					pass
132
				raise the.program.FatalError( 'init failed', out )