/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# command_init.py
#
# Copyright (C) 2013 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys, os, re, getopt, shutil, subprocess
import the, deployment
from subprocess import Popen


class CommandInit:


	def __init__( self ):
		self.repo = None


	def print_help( self ):
		print "Usage: " + the.program.name + " init [URL] [--repo=REPO]"
		print
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
		print "Initialise a local repository."
		print
		print "If an URL is given, the local reposity is a checkout of it (i.e., you can"
		print "receive updates from it and changes you commit will be sent to it).  The URL"
		print "can take the form of a simple hostname, such as \"example.com\", or it can be a"
		print "fully-qualified bazaar URL.  (Actually, in the first case, where it is a simple"
		print "hostname, it is internally expanded to scp://HOSTNAME/~/.stdhome/REPO)."
		print
		print "Options:"
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
		print "      --help       display help and exit"
		exit( 0 )


	def parse_command_line( self ):
		opts, args = getopt.gnu_getopt(
			sys.argv[ 1: ], "r:v",
			[ "repo=", "verbose", "help" ] )
		for opt, optarg in opts:
			if [ '--repo', '-r' ].count( opt ):
				if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
					raise the.FatalError(
						'invalid repository name: ' + optarg )
				self.repo = optarg
			elif [ '--verbose', '-v' ].count( opt ):
				the.verbose = True
			elif opt == "--help":
				self.print_help()
		
		# discard first argument (the command)
		args.pop( 0 )

		# URL argument
		self.url = args[ 0 ].strip() if len( args ) else None

		# remaining arguments
		if len( args ) > 1:
			raise the.program.UsageError( 'too many arguments' )


	def run( self ):
		the.set_repo( self.repo )

		# repo dir must not already exist
		the.repo.check_dir_exists( False )

		# ensure our top-level directory exists
		if not os.path.exists( the.expanded_dir ):
			os.mkdir( the.expanded_dir )

		if self.url:

			# expand url if it's a simple hostname
			if re.match( '^[0-9a-zA-z.]+$', self.url ):
				self.url = 'sftp://%s/%s/%s' % \
						   ( self.url, the.dir, the.repo.name )

			# initialise deployment (with an empty repo)
			deployment = Deployment( self.repo )
			deployment.copy_in()

			# perform bzr checkout
			print "checking out %s" % the.repo.dir
			p = Popen( [ 'bzr', 'co', self.url, the.repo.name ],
					   cwd = the.expanded_dir,
					   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
			out = p.communicate()[ 0 ]
			if p.returncode > 0:

				# attempt to clean-up repo dir, and die
				try:
					shutil.rmtree( the.repo.expanded_dir )
				except OSError as e:
					pass
				raise the.program.FatalError( 'checkout failed', out )

			# perform deployment
			deployment.copy_out()

		else:

			# perform bzr init
			print 'creating %s' % the.repo.dir
			p = Popen( [ 'bzr', 'init', the.repo.name ],
					   cwd = the.expanded_dir,
					   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
			out = p.communicate()[ 0 ]
			if p.returncode > 0:

				# attempt to clean-up repo dir, and die
				try:
					shutil.rmtree( the.repo.expanded_dir )
				except OSError as e:
					pass
				raise the.program.FatalError( 'init failed', out )