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