/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: 2014-04-05 19:22:21 UTC
  • Revision ID: tim@ed.am-20140405192221-yl8xgy3qorbahlfw
implemented CopyInWalker in terms of CopyBaseWalker, changed implementation of
the verbose operation print function for readability

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
 
 
38
37
                self.dir = dir
39
38
 
40
39
 
41
 
        def has_authority( self ):
42
 
                """Check that the directory is under this VCS's control.
43
 
                """
44
 
 
45
 
                return os.path.exists( os.path.join( self.dir, '.bzr' ) )
46
 
 
47
 
 
48
 
        def expand_repo_url( self, url ):
49
 
                """Convert a simple hostname in to an URL that the VCS can use.
50
 
                """
51
 
 
52
 
                return 'bzr+ssh://%s/%s/%s' % ( url, the.dir, the.repo.name )
53
 
 
54
 
 
55
40
        def init( self ):
56
41
                """Create a new, empty branch
57
42
                """
100
85
                """Obtain some sort of revision identifier
101
86
                """
102
87
 
103
 
                # bzr revno
 
88
                # bzr revert
104
89
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
105
90
 
106
91
                # parse revno
114
99
                revision.
115
100
                """
116
101
 
117
 
                # bzr st
118
 
                output = self.run( [ 'bzr', 'status' ] )
119
 
                files = self.parse_file_blocks( output )
120
 
 
121
 
                # remove kind changed files (or they can cause `bzr revert` to break in
122
 
                # strange situations, like when a directory has been replaced with a
123
 
                # symlink to a non-existant file)
124
 
                if 'kind changed' in files:
125
 
                        for file in files[ 'kind changed' ]:
126
 
                                matches = re.search( '^(.+?)[/@+]? \([^)]+\)$', file )
127
 
                                if not matches:
128
 
                                        raise RunTimeError(
129
 
                                                'failed to parse bzr kind change: %s' % file )
130
 
                                file = matches.group( 1 )
131
 
                                if the.verbose >= 2: print "removing (kind changed): " + file
132
 
                                full_file = os.path.join( self.dir, file )
133
 
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
134
 
                                        os.unlink( full_file )
135
 
                                elif os.path.isdir( full_file ):
136
 
                                        shutil.rmtree( full_file )
137
 
                                else:
138
 
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
139
 
 
140
 
                # bzr revert
 
102
                # bzr revert (run twice to handle a bug in bzr where reverting a
 
103
                # directory from a symlink can cause conflicts during initial revert)
 
104
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
141
105
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
142
106
 
143
107
                # bzr st
147
111
                # remove unknown files
148
112
                if 'unknown' in files:
149
113
                        for file in files[ 'unknown' ]:
150
 
                                matches = re.search( r'^(.+?)[/@+]?$', file )
151
 
                                if not matches:
152
 
                                        raise RunTimeError(
153
 
                                                'failed to parse bzr unknowns: %s' % file )
154
 
                                file = matches.group( 1 )
155
 
                                if the.verbose >= 2: print "removing (unknown): " + file
 
114
                                if the.verbose > 1: print "removing unknown: " + file
156
115
                                full_file = os.path.join( self.dir, file )
157
 
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
 
116
                                if os.path.isfile( full_file ):
158
117
                                        os.unlink( full_file )
159
118
                                elif os.path.isdir( full_file ):
160
119
                                        shutil.rmtree( full_file )
161
120
                                else:
162
121
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
163
122
 
164
 
                # if a revision identifier has been given, ensure we're updated to that
165
 
                if revno is not None and self.get_revno() != revno:
 
123
                # if a revision identifyer has been given, update to that
 
124
                if revno is not None:
166
125
 
167
126
                        # bzr update
168
127
                        self.run( [ 'bzr', 'update', '-r', revno ] )
222
181
                for line in buf:
223
182
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
224
183
                        line = line.rstrip()
 
184
                        if the.verbose > 1: print '  %s' % line
225
185
 
226
186
                        # renames show before and after file names
227
187
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
243
203
                                continue
244
204
 
245
205
                        raise RuntimeError(
246
 
                                'failed to parse bzr update output line:\n%s' % line )
 
206
                                'failed to parse bzr update output line:\n%' % line )
247
207
 
248
208
                return files
249
209
 
272
232
                return files['conflicts'] if 'conflicts' in files else None
273
233
 
274
234
 
275
 
        def add( self, files ):
276
 
                """Make sure files are added to version control.
277
 
                @param files a list of relative filenames
278
 
                """
279
 
 
280
 
                # bzr add
281
 
                self.run( [ 'bzr', 'add', '-N' ] + files )
282
 
 
283
 
 
284
 
        def commit( self ):
285
 
                """Commit changes to the repo.
286
 
                """
287
 
 
288
 
                # bzr commit
289
 
                self.run( [ 'bzr', 'commit', '-m', '' ] )
290
 
 
291
 
 
292
235
        def run( self, cmd ):
293
 
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
 
236
                if the.verbose > 1: print 'exec: %s' % ' '.join( cmd )
294
237
                p = Popen( cmd, cwd = self.dir,
295
238
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
296
239
                output = p.communicate()[ 0 ]
297
240
                if p.returncode > 0:
298
241
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
299
 
                if the.verbose >= 2:
300
 
                        verbose_output = output.rstrip()
301
 
                        if len( verbose_output ):
302
 
                                print re.sub( '(^|\n)', '\\1  : ', verbose_output )
303
242
                return output
304
243
 
305
244
 
319
258
                                                res[ current ] = list()
320
259
                                        res[ current ].append( matches.group( 1 ) )
321
260
                                        continue
322
 
                        if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
 
261
                        if re.search( '^[0-9]+ shelf exists', line ): continue
323
262
                        if re.search( '^working tree is out of date', line ): continue
324
263
                        raise self.ParseError( "unrecognised line: %s" % line )
325
264
                return res