/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
1
# bzr.py
2
#
3
# Copyright (C) 2014 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
22
import subprocess, os, re, shutil
23
from subprocess import Popen
24
import StringIO
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
25
from vcs import Vcs
26
from stdhome import the
27
28
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
29
class BzrVcs( Vcs ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
30
31
32
	def __init__( self, dir ):
33
		"""Init class
34
35
		@param dir the fully-qualified directory to work in.
36
		"""
53 by Tim Marston
minor comment formatting tweak
37
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
38
		self.dir = dir
39
40
63 by Tim Marston
determine and instantiate repo vcs dynamically; for new repos, added default vcs
41
	def has_authority( self ):
42
		"""Check that the directory is under this VCS's control.
43
		"""
44
45
		return os.path.exists( os.path.join( self.dir, '.bzr' ) )
46
47
48
	def expand_repo_url( self, url ):
49
		"""Convert a simple hostname in to an URL that the VCS can use.
50
		"""
51
52
		return 'bzr+ssh://%s/%s/%s' % ( url, the.dir, the.repo.name )
53
54
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
55
	def init( self ):
56
		"""Create a new, empty branch
57
		"""
58
59
		# the directory shouldn't exist
60
		os.mkdir( self.dir )
61
62
		# bzr init
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
63
		try:
64
			self.run( [ 'bzr', 'init', '.' ] )
65
		except self.VcsError as e:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
66
67
			# attempt to clean-up dir
68
			try:
69
				shutil.rmtree( self.dir )
70
			except OSError:
71
				pass
72
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
73
			raise
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
74
75
76
	def checkout( self, url ):
77
		"""Checkout a new copy of a remote branch.
78
79
		@param url the remote repository URL
80
		"""
81
82
		# the directory shouldn't exist
83
		os.mkdir( self.dir )
84
85
		# bzr co
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
86
		try:
87
			self.run( [ 'bzr', 'checkout', url, '.' ] )
88
		except self.VcsError as e:
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
89
90
			# attempt to clean-up dir
91
			try:
92
				shutil.rmtree( self.dir )
93
			except OSError:
94
				pass
95
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
96
			raise
97
98
99
	def get_revno( self ):
100
		"""Obtain some sort of revision identifier
101
		"""
102
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
103
		# bzr revno
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
104
		output = self.run( [ 'bzr', 'revno', '--tree' ] )
105
106
		# parse revno
107
		buf = StringIO.StringIO( output )
108
		return buf.readline().rstrip()
109
110
111
	def revert( self, revno = None ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
112
		"""Revert the branch so that there are no outstanding changes or unknown files.
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
113
		If a revno is supplied, then the repository is reverted to that
114
		revision.
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
115
		"""
116
26 by Tim Marston
fixed more bugs in the bzr backend
117
		# bzr st
118
		output = self.run( [ 'bzr', 'status' ] )
119
		files = self.parse_file_blocks( output )
120
121
		# remove kind changed files (or they can cause `bzr revert` to break in
122
		# strange situations, like when a directory has been replaced with a
123
		# symlink to a non-existant file)
124
		if 'kind changed' in files:
125
			for file in files[ 'kind changed' ]:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
126
				matches = re.search( '^(.+?)[/@+]? \([^)]+\)$', file )
26 by Tim Marston
fixed more bugs in the bzr backend
127
				if not matches:
128
					raise RunTimeError(
129
						'failed to parse bzr kind change: %s' % file )
130
				file = matches.group( 1 )
32 by Tim Marston
make verbose levels clearer
131
				if the.verbose >= 2: print "removing (kind changed): " + file
26 by Tim Marston
fixed more bugs in the bzr backend
132
				full_file = os.path.join( self.dir, file )
133
				if os.path.isfile( full_file ) or os.path.islink( full_file ):
134
					os.unlink( full_file )
135
				elif os.path.isdir( full_file ):
136
					shutil.rmtree( full_file )
137
				else:
138
					raise RuntimeError( 'exotic file in repo: %s' % file )
139
140
		# bzr revert
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
141
		self.run( [ 'bzr', 'revert', '--no-backup' ] )
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
142
143
		# bzr st
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
144
		output = self.run( [ 'bzr', 'status' ] )
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
145
		files = self.parse_file_blocks( output )
146
147
		# remove unknown files
148
		if 'unknown' in files:
149
			for file in files[ 'unknown' ]:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
150
				matches = re.search( r'^(.+?)[/@+]?$', file )
151
				if not matches:
152
					raise RunTimeError(
153
						'failed to parse bzr unknowns: %s' % file )
154
				file = matches.group( 1 )
32 by Tim Marston
make verbose levels clearer
155
				if the.verbose >= 2: print "removing (unknown): " + file
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
156
				full_file = os.path.join( self.dir, file )
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
157
				if os.path.isfile( full_file ) or os.path.islink( full_file ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
158
					os.unlink( full_file )
22 by Tim Marston
fixed some bugs in Bzr.revert()
159
				elif os.path.isdir( full_file ):
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
160
					shutil.rmtree( full_file )
161
				else:
162
					raise RuntimeError( 'exotic file in repo: %s' % file )
163
51 by Tim Marston
added info for add command to --help; fixed bug with add command where all files
164
		# if a revision identifier has been given, ensure we're updated to that
165
		if revno is not None and self.get_revno() != revno:
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
166
167
			# bzr update
168
			self.run( [ 'bzr', 'update', '-r', revno ] )
169
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
170
171
	def update( self ):
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
172
		"""Update the branch, pulling down any upstream changes and merging them.  This
173
		method returns a list of the files that were modified as part of this
174
		operation.
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
175
		"""
176
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
177
#		WARNING: the following might cause bzr to ask for your ssh password more than
178
#		once during an update!!!
179
#
180
#		# get revno
181
#		revno = self.get_revno()
182
#
183
#		# update to current revision (pull in history without updating tree)
184
#		self.run( [ 'bzr', 'update', '-r', revno ] )
185
#
186
#		# get log output
187
#		next_revno = str( int( revno ) + 1 )
188
#		output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
189
#
190
#		# parse output
191
#		keep_files = list()
192
#		buf = StringIO.StringIO( output )
193
#		in_message = False
194
#		for line in buf:
195
#			line = line.rstrip( '\n' )
196
#			if line.lower() == 'message:':
197
#				in_message = True
198
#			elif in_message:
199
#				if line[ : 2 ] != '  ':
200
#					in_message = False
201
#				else:
202
#					line = line[ 2 : ]
203
#
204
#					# process directives
205
#					if line[ : 6 ].lower() == 'keep: ':
206
#						file = line[ 6 : ]
207
#						if file in rename_files: file = rename_files[ file ]
208
#						keep_files.append( file )
209
#					elif line[ : 8 ].lower() == 'rename: ':
210
#						rename_from = line[ 8 : ]
211
#					elif line[ : 4 ].lower() == 'to: ':
212
#						if rename_from in rename_files:
213
#							rename_from = rename_files[ rename_from ]
214
#						rename_files[ line[ 4 : ] ] = rename_from
215
216
		# bzr update properly
217
		output = self.run( [ 'bzr', 'update' ] )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
218
219
		# parse output (see logic in report() in bzrlib/delta.py)
220
		files = list()
221
		buf = StringIO.StringIO( output )
222
		for line in buf:
223
			if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
224
			line = line.rstrip()
225
226
			# renames show before and after file names
227
			matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
228
			if matches:
229
				files.append( matches.group( 1 ) )
230
				files.append( matches.group( 2 ) )
231
				continue
232
233
			# kind changes shows the same name twice
234
			matches = re.search( '^.K. (.*?)[/@+]? => (.*?)[/@+]?$', line )
235
			if matches:
236
				files.append( matches.group( 1 ) )
237
				continue
238
239
			# other entries have only one filename
240
			matches = re.search( '^... (.*?)[/@+]?$', line )
241
			if matches:
242
				files.append( matches.group( 1 ) )
243
				continue
244
245
			raise RuntimeError(
26 by Tim Marston
fixed more bugs in the bzr backend
246
				'failed to parse bzr update output line:\n%s' % line )
5 by Tim Marston
moved copy-in, copy-out and deployment conflict checking to a set of "walkers";
247
248
		return files
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
249
250
251
	def has_changes( self ):
252
		"""Check if the branch has any local modifications.
253
		"""
254
255
		# bzr status
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
256
		output = self.run( [ 'bzr', 'status', '--no-pending' ] )
257
258
		# parse output
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
259
		files = self.parse_file_blocks( output )
260
		return True if len( files ) else False
261
262
263
	def get_conflicts( self ):
264
		"""Return a list of files that have conflicts.
265
		"""
266
267
		# bzr status
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
268
		output = self.run( [ 'bzr', 'status', '--no-pending' ] )
269
270
		# parse output
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
271
		files = self.parse_file_blocks( output )
272
		return files['conflicts'] if 'conflicts' in files else None
273
274
41 by Tim Marston
added add command
275
	def add( self, files ):
276
		"""Make sure files are added to version control.
277
		@param files a list of relative filenames
278
		"""
279
280
		# bzr add
51 by Tim Marston
added info for add command to --help; fixed bug with add command where all files
281
		self.run( [ 'bzr', 'add', '-N' ] + files )
41 by Tim Marston
added add command
282
283
284
	def commit( self ):
285
		"""Commit changes to the repo.
286
		"""
287
288
		# bzr commit
289
		self.run( [ 'bzr', 'commit', '-m', '' ] )
290
291
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
292
	def run( self, cmd ):
32 by Tim Marston
make verbose levels clearer
293
		if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
294
		p = Popen( cmd, cwd = self.dir,
295
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
296
		output = p.communicate()[ 0 ]
297
		if p.returncode > 0:
298
			raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
299
		if the.verbose >= 2:
300
			print re.sub( '(^|\n)', '\\1  > ', output.rstrip() )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
301
		return output
302
303
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
304
	def parse_file_blocks( self, output ):
305
		res = dict()
306
		current = None
307
		buf = StringIO.StringIO( output )
308
		for line in buf:
309
			matches = re.search( '^([a-z ]+):$', line, re.I )
310
			if matches:
311
				current = matches.group( 1 )
312
				continue
313
			if current:
314
				matches = re.search( '^  ([^ ].*)$', line )
315
				if matches:
316
					if not current in res:
317
						res[ current ] = list()
318
					res[ current ].append( matches.group( 1 ) )
319
					continue
26 by Tim Marston
fixed more bugs in the bzr backend
320
			if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
3 by Tim Marston
added bzr as a vcs backend; finished init command; implemented deployment
321
			if re.search( '^working tree is out of date', line ): continue
322
			raise self.ParseError( "unrecognised line: %s" % line )
323
		return res
324
325
326
	class ParseError( Exception ):
327
		pass