/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/vcs/bzr.py

  • Committer: Tim Marston
  • Date: 2016-05-22 16:40:51 UTC
  • Revision ID: tim@ed.am-20160522164051-h2n54oaxg25c7x7c
updated todo

Show diffs side-by-side

added added

removed removed

1
 
# brz.py
 
1
# bzr.py
2
2
#
3
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
4
4
#
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import io
23
 
import os
24
 
import re
25
 
import shutil
26
 
import subprocess
 
22
import subprocess, os, re, shutil
27
23
from subprocess import Popen
28
 
 
 
24
import StringIO
 
25
from vcs import Vcs
29
26
from stdhome import the
30
27
 
31
 
from .vcs import Vcs
32
 
 
33
 
 
34
 
class BrzVcs( Vcs ):
 
28
 
 
29
class BzrVcs( Vcs ):
35
30
 
36
31
 
37
32
        def __init__( self, dir ):
41
36
                """
42
37
 
43
38
                self.dir = dir
44
 
                self.ignored_files = [ '.bzr', '.bzrignore' ]
45
39
 
46
40
 
47
41
        def has_authority( self ):
65
59
                # the directory shouldn't exist
66
60
                os.mkdir( self.dir )
67
61
 
68
 
                # brz init
 
62
                # bzr init
69
63
                try:
70
 
                        self.run( [ 'brz', 'init', '.' ] )
 
64
                        self.run( [ 'bzr', 'init', '.' ] )
71
65
                except self.VcsError as e:
72
66
 
73
67
                        # attempt to clean-up dir
88
82
                # the directory shouldn't exist
89
83
                os.mkdir( self.dir )
90
84
 
91
 
                # brz co
 
85
                # bzr co
92
86
                try:
93
 
                        self.run( [ 'brz', 'checkout', url, '.' ] )
 
87
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
94
88
                except self.VcsError as e:
95
89
 
96
90
                        # attempt to clean-up dir
106
100
                """Obtain some sort of revision identifier
107
101
                """
108
102
 
109
 
                # brz revno
110
 
                output = self.run( [ 'brz', 'revno', '--tree' ] )
 
103
                # bzr revno
 
104
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
111
105
 
112
106
                # parse revno
113
 
                buf = io.StringIO( output )
 
107
                buf = StringIO.StringIO( output )
114
108
                return buf.readline().rstrip()
115
109
 
116
110
 
120
114
                revision.
121
115
                """
122
116
 
123
 
                # brz st
124
 
                output = self.run( [ 'brz', 'status' ] )
 
117
                # bzr st
 
118
                output = self.run( [ 'bzr', 'status' ] )
125
119
                files = self.parse_file_blocks( output )
126
120
 
127
 
                # remove kind changed files (or they can cause `brz revert` to break in
 
121
                # remove kind changed files (or they can cause `bzr revert` to break in
128
122
                # strange situations, like when a directory has been replaced with a
129
123
                # symlink to a non-existant file)
130
124
                if 'kind changed' in files:
132
126
                                matches = re.search( '^(.+?)[/@+]? \([^)]+\)$', file )
133
127
                                if not matches:
134
128
                                        raise RunTimeError(
135
 
                                                'failed to parse brz kind change: %s' % file )
 
129
                                                'failed to parse bzr kind change: %s' % file )
136
130
                                file = matches.group( 1 )
137
 
                                if the.verbose >= 2: print("removing (kind changed): " + file)
 
131
                                if the.verbose >= 2: print "removing (kind changed): " + file
138
132
                                full_file = os.path.join( self.dir, file )
139
133
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
140
134
                                        os.unlink( full_file )
143
137
                                else:
144
138
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
145
139
 
146
 
                # brz revert
147
 
                self.run( [ 'brz', 'revert', '--no-backup' ] )
 
140
                # bzr revert
 
141
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
148
142
 
149
 
                # brz st
150
 
                output = self.run( [ 'brz', 'status' ] )
 
143
                # bzr st
 
144
                output = self.run( [ 'bzr', 'status' ] )
151
145
                files = self.parse_file_blocks( output )
152
146
 
153
147
                # remove unknown files
156
150
                                matches = re.search( r'^(.+?)[/@+]?$', file )
157
151
                                if not matches:
158
152
                                        raise RunTimeError(
159
 
                                                'failed to parse brz unknowns: %s' % file )
 
153
                                                'failed to parse bzr unknowns: %s' % file )
160
154
                                file = matches.group( 1 )
161
 
                                if the.verbose >= 2: print("removing (unknown): " + file)
 
155
                                if the.verbose >= 2: print "removing (unknown): " + file
162
156
                                full_file = os.path.join( self.dir, file )
163
157
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
164
158
                                        os.unlink( full_file )
170
164
                # if a revision identifier has been given, ensure we're updated to that
171
165
                if revno is not None and self.get_revno() != revno:
172
166
 
173
 
                        # brz update
174
 
                        self.run( [ 'brz', 'update', '-r', revno ] )
 
167
                        # bzr update
 
168
                        self.run( [ 'bzr', 'update', '-r', revno ] )
175
169
 
176
170
 
177
171
        def update( self ):
180
174
                operation.
181
175
                """
182
176
 
183
 
#               WARNING: the following might cause brz to ask for your ssh password more than
 
177
#               WARNING: the following might cause bzr to ask for your ssh password more than
184
178
#               once during an update!!!
185
179
#
186
180
#               # get revno
187
181
#               revno = self.get_revno()
188
182
#
189
183
#               # update to current revision (pull in history without updating tree)
190
 
#               self.run( [ 'brz', 'update', '-r', revno ] )
 
184
#               self.run( [ 'bzr', 'update', '-r', revno ] )
191
185
#
192
186
#               # get log output
193
187
#               next_revno = str( int( revno ) + 1 )
194
 
#               output = self.run( [ 'brz', 'log', '-r', next_revno + '..' ] )
 
188
#               output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
195
189
#
196
190
#               # parse output
197
191
#               keep_files = list()
219
213
#                                                       rename_from = rename_files[ rename_from ]
220
214
#                                               rename_files[ line[ 4 : ] ] = rename_from
221
215
 
222
 
                # brz update properly
223
 
                output = self.run( [ 'brz', 'update' ] )
 
216
                # bzr update properly
 
217
                output = self.run( [ 'bzr', 'update' ] )
224
218
 
225
219
                # parse output (see logic in report() in bzrlib/delta.py)
226
220
                files = list()
227
 
                buf = io.StringIO( output )
 
221
                buf = StringIO.StringIO( output )
228
222
                for line in buf:
229
223
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
230
224
                        line = line.rstrip()
249
243
                                continue
250
244
 
251
245
                        raise RuntimeError(
252
 
                                'failed to parse brz update output line:\n%s' % line )
 
246
                                'failed to parse bzr update output line:\n%s' % line )
253
247
 
254
248
                return files
255
249
 
256
250
 
257
 
        def status( self ):
258
 
                """Get a list of any local modifications.  This method returns a list of files
259
 
                which are modified.
260
 
 
261
 
                """
262
 
 
263
 
                # bzr status
264
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
265
 
 
266
 
                # parse output
267
 
                return self.parse_file_blocks( output )
268
 
 
269
 
 
270
251
        def has_changes( self ):
271
252
                """Check if the branch has any local modifications.
272
253
                """
273
254
 
274
 
                # brz status
275
 
                output = self.run( [ 'brz', 'status', '--no-pending' ] )
 
255
                # bzr status
 
256
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
276
257
 
277
258
                # parse output
278
259
                files = self.parse_file_blocks( output )
283
264
                """Return a list of files that have conflicts.
284
265
                """
285
266
 
286
 
                # brz status
287
 
                output = self.run( [ 'brz', 'status', '--no-pending' ] )
 
267
                # bzr status
 
268
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
288
269
 
289
270
                # parse output
290
271
                files = self.parse_file_blocks( output )
296
277
                @param files a list of relative filenames
297
278
                """
298
279
 
299
 
                # brz add
300
 
                self.run( [ 'brz', 'add', '-N' ] + files )
 
280
                # bzr add
 
281
                self.run( [ 'bzr', 'add', '-N' ] + files )
301
282
 
302
283
 
303
284
        def commit( self ):
304
285
                """Commit changes to the repo.
305
286
                """
306
287
 
307
 
                # brz commit
308
 
                try:
309
 
                        self.run( [ 'brz', 'commit', '-m', '' ] )
310
 
                except self.VcsError as e:
311
 
                        if re.search( 'Working tree is out of date', e.output ):
312
 
                                raise the.program.FatalError(
313
 
                                        'you must update your files first.\n' +
314
 
                                        'Hint: see "%s update --help"' % the.program.name );
315
 
                        else:
316
 
                                raise e
 
288
                # bzr commit
 
289
                self.run( [ 'bzr', 'commit', '-m', '' ] )
317
290
 
318
291
 
319
292
        def run( self, cmd ):
320
 
                if the.verbose >= 2: print('exec: %s' % ' '.join( cmd ))
 
293
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
321
294
                p = Popen( cmd, cwd = self.dir,
322
295
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
323
 
                output = p.communicate()[ 0 ].decode()
 
296
                output = p.communicate()[ 0 ]
324
297
                if p.returncode > 0:
325
298
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
326
299
                if the.verbose >= 2:
327
300
                        verbose_output = output.rstrip()
328
301
                        if len( verbose_output ):
329
 
                                print(re.sub( '(^|\n)', '\\1  : ', verbose_output ))
 
302
                                print re.sub( '(^|\n)', '\\1  : ', verbose_output )
330
303
                return output
331
304
 
332
305
 
333
306
        def parse_file_blocks( self, output ):
334
307
                res = dict()
335
308
                current = None
336
 
                buf = io.StringIO( output )
 
309
                buf = StringIO.StringIO( output )
337
310
                for line in buf:
338
311
                        matches = re.search( '^([a-z ]+):$', line, re.I )
339
312
                        if matches: