/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-03-08 00:40:42 UTC
  • Revision ID: tim@ed.am-20140308004042-j5rco14bjp3teegj
updated .bzrignore

Show diffs side-by-side

added added

removed removed

26
26
from stdhome import the
27
27
 
28
28
 
29
 
class BzrVcs( Vcs ):
 
29
class VcsBzr( Vcs ):
30
30
 
31
31
 
32
32
        def __init__( self, dir ):
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
                """
60
45
                os.mkdir( self.dir )
61
46
 
62
47
                # bzr init
63
 
                try:
64
 
                        self.run( [ 'bzr', 'init', '.' ] )
65
 
                except self.VcsError as e:
 
48
                p = Popen( [ 'bzr', 'init', '.' ], cwd = self.dir,
 
49
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
50
                output = p.communicate()[ 0 ]
 
51
                if p.returncode > 0:
66
52
 
67
53
                        # attempt to clean-up dir
68
54
                        try:
70
56
                        except OSError:
71
57
                                pass
72
58
 
73
 
                        raise
 
59
                        raise self.VcsError( 'bzr init failed', output )
74
60
 
75
61
 
76
62
        def checkout( self, url ):
83
69
                os.mkdir( self.dir )
84
70
 
85
71
                # bzr co
86
 
                try:
87
 
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
88
 
                except self.VcsError as e:
 
72
                p = Popen( [ 'bzr', 'co', url, '.' ], cwd = self.dir,
 
73
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
74
                output = p.communicate()[ 0 ]
 
75
                if p.returncode > 0:
89
76
 
90
77
                        # attempt to clean-up dir
91
78
                        try:
93
80
                        except OSError:
94
81
                                pass
95
82
 
96
 
                        raise
97
 
 
98
 
 
99
 
        def get_revno( self ):
100
 
                """Obtain some sort of revision identifier
101
 
                """
102
 
 
103
 
                # bzr revno
104
 
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
105
 
 
106
 
                # parse revno
107
 
                buf = StringIO.StringIO( output )
108
 
                return buf.readline().rstrip()
109
 
 
110
 
 
111
 
        def revert( self, revno = None ):
 
83
                        raise self.VcsError( 'bzr checkout failed', output )
 
84
 
 
85
 
 
86
        def revert( self ):
112
87
                """Revert the branch so that there are no outstanding changes or unknown files.
113
 
                If a revno is supplied, then the repository is reverted to that
114
 
                revision.
115
88
                """
116
89
 
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
90
                # bzr revert
141
 
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
 
91
                p = Popen( [ 'bzr', 'revert', '--no-backup' ], cwd = self.dir,
 
92
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
93
                output = p.communicate()[ 0 ]
 
94
                if p.returncode > 0:
 
95
                        raise self.VcsError( 'bzr revert failed', output )
142
96
 
143
97
                # bzr st
144
 
                output = self.run( [ 'bzr', 'status' ] )
 
98
                p = Popen( [ 'bzr', 'st' ], cwd = self.dir,
 
99
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
100
                output = p.communicate()[ 0 ]
 
101
                if p.returncode > 0:
 
102
                        raise self.VcsError( 'bzr status failed', output )
145
103
                files = self.parse_file_blocks( output )
146
104
 
147
105
                # remove unknown files
148
106
                if 'unknown' in files:
149
107
                        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
156
108
                                full_file = os.path.join( self.dir, file )
157
 
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
 
109
                                if os.path.isfile( full_file ):
158
110
                                        os.unlink( full_file )
159
 
                                elif os.path.isdir( full_file ):
 
111
                                elif os.full_file.isdir( full_file ):
160
112
                                        shutil.rmtree( full_file )
161
113
                                else:
162
114
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
163
115
 
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:
166
 
 
167
 
                        # bzr update
168
 
                        self.run( [ 'bzr', 'update', '-r', revno ] )
169
 
 
170
116
 
171
117
        def update( self ):
172
118
                """Update the branch, pulling down any upstream changes and merging them.  This
174
120
                operation.
175
121
                """
176
122
 
177
 
#               WARNING: the following might cause bzr to ask for your ssh password more than
178
 
#               once during an update!!!
179
 
#
180
 
#               # get revno
181
 
#               revno = self.get_revno()
182
 
#
183
 
#               # update to current revision (pull in history without updating tree)
184
 
#               self.run( [ 'bzr', 'update', '-r', revno ] )
185
 
#
186
 
#               # get log output
187
 
#               next_revno = str( int( revno ) + 1 )
188
 
#               output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
189
 
#
190
 
#               # parse output
191
 
#               keep_files = list()
192
 
#               buf = StringIO.StringIO( output )
193
 
#               in_message = False
194
 
#               for line in buf:
195
 
#                       line = line.rstrip( '\n' )
196
 
#                       if line.lower() == 'message:':
197
 
#                               in_message = True
198
 
#                       elif in_message:
199
 
#                               if line[ : 2 ] != '  ':
200
 
#                                       in_message = False
201
 
#                               else:
202
 
#                                       line = line[ 2 : ]
203
 
#
204
 
#                                       # process directives
205
 
#                                       if line[ : 6 ].lower() == 'keep: ':
206
 
#                                               file = line[ 6 : ]
207
 
#                                               if file in rename_files: file = rename_files[ file ]
208
 
#                                               keep_files.append( file )
209
 
#                                       elif line[ : 8 ].lower() == 'rename: ':
210
 
#                                               rename_from = line[ 8 : ]
211
 
#                                       elif line[ : 4 ].lower() == 'to: ':
212
 
#                                               if rename_from in rename_files:
213
 
#                                                       rename_from = rename_files[ rename_from ]
214
 
#                                               rename_files[ line[ 4 : ] ] = rename_from
215
 
 
216
 
                # bzr update properly
217
 
                output = self.run( [ 'bzr', 'update' ] )
 
123
                # bzr update
 
124
                p = Popen( [ 'bzr', 'update' ], cwd = self.dir,
 
125
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
126
                output = p.communicate()[ 0 ]
 
127
                if p.returncode > 0:
 
128
                        raise self.VcsError( 'bzr update failed', output )
218
129
 
219
130
                # parse output (see logic in report() in bzrlib/delta.py)
220
131
                files = list()
222
133
                for line in buf:
223
134
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
224
135
                        line = line.rstrip()
 
136
                        if the.verbose: print '  %s' % line
225
137
 
226
138
                        # renames show before and after file names
227
139
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
243
155
                                continue
244
156
 
245
157
                        raise RuntimeError(
246
 
                                'failed to parse bzr update output line:\n%s' % line )
 
158
                                'failed to parse bzr update output line:\n%' % line )
247
159
 
248
160
                return files
249
161
 
253
165
                """
254
166
 
255
167
                # bzr status
256
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
257
 
 
258
 
                # parse output
 
168
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
169
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
170
                output = p.communicate()[ 0 ]
 
171
                if p.returncode > 0:
 
172
                        raise self.VcsError( 'bzr status failed', output )
259
173
                files = self.parse_file_blocks( output )
260
174
                return True if len( files ) else False
261
175
 
265
179
                """
266
180
 
267
181
                # bzr status
268
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
269
 
 
270
 
                # parse output
 
182
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
183
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
184
                output = p.communicate()[ 0 ]
 
185
                if p.returncode > 0:
 
186
                        raise self.VcsError( 'bzr status failed', output )
271
187
                files = self.parse_file_blocks( output )
272
188
                return files['conflicts'] if 'conflicts' in files else None
273
189
 
274
190
 
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
 
        def run( self, cmd ):
293
 
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
294
 
                p = Popen( cmd, cwd = self.dir,
295
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
296
 
                output = p.communicate()[ 0 ]
297
 
                if p.returncode > 0:
298
 
                        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
 
                return output
304
 
 
305
 
 
306
191
        def parse_file_blocks( self, output ):
307
192
                res = dict()
308
193
                current = None
319
204
                                                res[ current ] = list()
320
205
                                        res[ current ].append( matches.group( 1 ) )
321
206
                                        continue
322
 
                        if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
 
207
                        if re.search( '^[0-9]+ shelf exists', line ): continue
323
208
                        if re.search( '^working tree is out of date', line ): continue
324
209
                        raise self.ParseError( "unrecognised line: %s" % line )
325
210
                return res