/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-12-13 21:40:34 UTC
  • Revision ID: tim@ed.am-20161213214034-nd5t7ztnlrjd627i
fix add command and generic filename expansion/resolution to expend to
homedir-relative filename and absolute filename based on original filename, as
specified, rather than a fully, symlink-resolved filename.  So, e.g., if ~/bob
was a symlink to ~/fred, then ~/bob/a would resolve to the relative filename
bob/a, becuase it is inside the homedir (it would resolve to fred/a otherwise)

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