/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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# bzr.py
#
# Copyright (C) 2014 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 subprocess, os, re, shutil
from subprocess import Popen
import stdhome.the as the
import StringIO


class VcsBzr:


	def __init__( self, dir ):
		"""Init class

		@param dir the fully-qualified directory to work in.
		"""
		self.dir = dir


	def init( self ):
		"""Create a new, empty branch
		"""

		# the directory shouldn't exist
		os.mkdir( self.dir )

		# bzr init
		p = Popen( [ 'bzr', 'init', '.' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:

			# attempt to clean-up dir
			try:
				shutil.rmtree( self.dir )
			except OSError:
				pass

			raise the.program.FatalError( 'bzr init failed', output )


	def checkout( self, url ):
		"""Checkout a new copy of a remote branch.

		@param url the remote repository URL
		"""

		# the directory shouldn't exist
		os.mkdir( self.dir )

		# bzr co
		p = Popen( [ 'bzr', 'co', url, '.' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:

			# attempt to clean-up dir
			try:
				shutil.rmtree( self.dir )
			except OSError:
				pass

			raise the.program.FatalError( 'bzr checkout failed', output )


	def revert( self ):
		"""Revert the branch so that there are no outstanding changes or unknown files.
		"""

		# bzr revert
		p = Popen( [ 'bzr', 'revert', '--no-backup' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:
			raise the.program.FatalError( 'bzr revert failed', output )

		# bzr st
		p = Popen( [ 'bzr', 'st' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:
			raise the.program.FatalError( 'bzr status failed', output )
		files = self.parse_file_blocks( output )

		# remove unknown files
		if 'unknown' in files:
			for file in files[ 'unknown' ]:
				full_file = os.path.join( self.dir, file )
				if os.path.isfile( full_file ):
					os.unlink( full_file )
				elif os.full_file.isdir( full_file ):
					shutil.rmtree( full_file )
				else:
					raise RuntimeError( 'exotic file in repo: %s' % file )


	def update( self ):
		"""Update the branch, pulling down any upstream changes and merging them.
		"""

		# bzr update
		p = Popen( [ 'bzr', 'update' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:
			raise the.program.FatalError( 'bzr update failed', output )


	def has_changes( self ):
		"""Check if the branch has any local modifications.
		"""

		# bzr status
		p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:
			raise the.program.FatalError( 'bzr status failed', output )
		files = self.parse_file_blocks( output )
		return True if len( files ) else False


	def get_conflicts( self ):
		"""Return a list of files that have conflicts.
		"""

		# bzr status
		p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
				   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
		output = p.communicate()[ 0 ]
		if p.returncode > 0:
			raise the.program.FatalError( 'bzr status failed', output )
		files = self.parse_file_blocks( output )
		return files['conflicts'] if 'conflicts' in files else None


	def parse_file_blocks( self, output ):
		res = dict()
		current = None
		buf = StringIO.StringIO( output )
		for line in buf:
			matches = re.search( '^([a-z ]+):$', line, re.I )
			if matches:
				current = matches.group( 1 )
				continue
			if current:
				matches = re.search( '^  ([^ ].*)$', line )
				if matches:
					if not current in res:
						res[ current ] = list()
					res[ current ].append( matches.group( 1 ) )
					continue
			if re.search( '^[0-9]+ shelf exists', line ): continue
			if re.search( '^working tree is out of date', line ): continue
			raise self.ParseError( "unrecognised line: %s" % line )
		return res


	class ParseError( Exception ):
		pass