/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
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
60
			elif opt == "--help":
1 by Tim Marston
initial commit; basic app startup and initial command-line processing
61
				self.print_help()
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
62
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
63
		# discard first argument (the command)
64
		args.pop( 0 )
65
66
		# URL argument
67
		self.url = args[ 0 ].strip() if len( args ) else None
68
35 by Tim Marston
made checks for verbose >= 1 explicit
69
		# check remaining arguments
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
70
		if len( args ) > 1:
71
			raise the.program.UsageError( 'too many arguments' )
72
73
74
	def run( self ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
75
76
		# 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
77
		the.repo.check_dir_exists( False )
78
79
		# ensure our top-level directory exists
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
80
		if not os.path.exists( the.full_dir ):
81
			os.mkdir( the.full_dir )
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
82
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
83
		# 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
84
		if self.url:
85
86
			# expand url if it's a simple hostname
87
			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)
88
				self.url = 'bzr+ssh://%s/%s/%s' % \
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
89
						   ( self.url, the.dir, the.repo.name )
90
91
			# initialise deployment (with an empty repo)
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
92
			deployment = Deployment()
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
93
			deployment.copy_in()
94
95
			# perform bzr checkout
35 by Tim Marston
made checks for verbose >= 1 explicit
96
			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
97
			try:
98
				the.repo.vcs.checkout( self.url )
99
			except Exception as e:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
100
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
101
				# attempt to clean-up repo dir
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
102
				try:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
103
					shutil.rmtree( the.repo.full_dir )
104
				except OSError:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
105
					pass
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
106
107
				raise e
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
108
60 by Tim Marston
fixed init command
109
			message = ''
110
59 by Tim Marston
switched init command (and update command) to checking for conflicts prior to
111
			# check for deployment conclicts
112
			conflicts = deployment.get_conflicts()
113
			if conflicts:
114
				message += 'deployment conflicts:\n  %s' % \
115
						   '\n  '.join( conflicts )
116
60 by Tim Marston
fixed init command
117
			# stop if there are conflicts
118
			if message:
119
				raise the.program.FatalError(
120
					'there were conflicts...\n' + message )
121
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
122
			# perform deployment
59 by Tim Marston
switched init command (and update command) to checking for conflicts prior to
123
			deployment.copy_out()
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
124
125
		else:
126
127
			# perform bzr init
35 by Tim Marston
made checks for verbose >= 1 explicit
128
			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
129
			try:
130
				the.repo.vcs.init()
131
			except Exception as e:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
132
133
				# attempt to clean-up repo dir, and die
134
				try:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
135
					shutil.rmtree( the.repo.full_dir )
136
				except OSError:
2 by Tim Marston
added global objects (the.repo, the.program), deployment object and implemented
137
					pass
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
138
139
				raise e