/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-02-16 00:26:53 UTC
  • Revision ID: tim@ed.am-20160216002653-oa8dgponknyislg3
added home directory change reporting to CopyOutWalker; added --quiet option to
update, resolve, revert and init commands; replace use of re.match with
re.search for clarity (and fixed related bug in FileMatcher); added BzrVcs.run
command output when verbose >= 2

Show diffs side-by-side

added added

removed removed

34
34
 
35
35
                @param dir the fully-qualified directory to work in.
36
36
                """
 
37
 
37
38
                self.dir = dir
38
39
 
39
40
 
85
86
                """Obtain some sort of revision identifier
86
87
                """
87
88
 
88
 
                # bzr revert
 
89
                # bzr revno
89
90
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
90
91
 
91
92
                # parse revno
99
100
                revision.
100
101
                """
101
102
 
 
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.search( '^(.+?)[/@+]? \([^)]+\)$', file )
 
113
                                if not matches:
 
114
                                        raise RunTimeError(
 
115
                                                'failed to parse bzr kind change: %s' % file )
 
116
                                file = matches.group( 1 )
 
117
                                if the.verbose >= 2: print "removing (kind changed): " + file
 
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
 
102
126
                # bzr revert
103
127
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
104
128
 
109
133
                # remove unknown files
110
134
                if 'unknown' in files:
111
135
                        for file in files[ 'unknown' ]:
 
136
                                matches = re.search( r'^(.+?)[/@+]?$', file )
 
137
                                if not matches:
 
138
                                        raise RunTimeError(
 
139
                                                'failed to parse bzr unknowns: %s' % file )
 
140
                                file = matches.group( 1 )
 
141
                                if the.verbose >= 2: print "removing (unknown): " + file
112
142
                                full_file = os.path.join( self.dir, file )
113
 
                                if os.path.isfile( full_file ):
 
143
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
114
144
                                        os.unlink( full_file )
115
 
                                elif os.full_file.isdir( full_file ):
 
145
                                elif os.path.isdir( full_file ):
116
146
                                        shutil.rmtree( full_file )
117
147
                                else:
118
148
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
119
149
 
120
 
                # if a revision identifyer has been given, update to that
121
 
                if revno is not None:
 
150
                # if a revision identifier has been given, ensure we're updated to that
 
151
                if revno is not None and self.get_revno() != revno:
122
152
 
123
153
                        # bzr update
124
154
                        self.run( [ 'bzr', 'update', '-r', revno ] )
178
208
                for line in buf:
179
209
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
180
210
                        line = line.rstrip()
181
 
                        if the.verbose > 1: print '  %s' % line
182
211
 
183
212
                        # renames show before and after file names
184
213
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
200
229
                                continue
201
230
 
202
231
                        raise RuntimeError(
203
 
                                'failed to parse bzr update output line:\n%' % line )
 
232
                                'failed to parse bzr update output line:\n%s' % line )
204
233
 
205
234
                return files
206
235
 
229
258
                return files['conflicts'] if 'conflicts' in files else None
230
259
 
231
260
 
 
261
        def add( self, files ):
 
262
                """Make sure files are added to version control.
 
263
                @param files a list of relative filenames
 
264
                """
 
265
 
 
266
                # bzr add
 
267
                self.run( [ 'bzr', 'add', '-N' ] + files )
 
268
 
 
269
 
 
270
        def commit( self ):
 
271
                """Commit changes to the repo.
 
272
                """
 
273
 
 
274
                # bzr commit
 
275
                self.run( [ 'bzr', 'commit', '-m', '' ] )
 
276
 
 
277
 
232
278
        def run( self, cmd ):
233
 
                if the.verbose > 1: print 'exec: %s' % ' '.join( cmd )
 
279
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
234
280
                p = Popen( cmd, cwd = self.dir,
235
281
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
236
282
                output = p.communicate()[ 0 ]
237
283
                if p.returncode > 0:
238
284
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
 
285
                if the.verbose >= 2:
 
286
                        print re.sub( '(^|\n)', '\\1  > ', output.rstrip() )
239
287
                return output
240
288
 
241
289
 
255
303
                                                res[ current ] = list()
256
304
                                        res[ current ].append( matches.group( 1 ) )
257
305
                                        continue
258
 
                        if re.search( '^[0-9]+ shelf exists', line ): continue
 
306
                        if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
259
307
                        if re.search( '^working tree is out of date', line ): continue
260
308
                        raise self.ParseError( "unrecognised line: %s" % line )
261
309
                return res