/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-09-09 19:13:33 UTC
  • Revision ID: tim@ed.am-20140909191333-0b7hrzonxce8mgy3
attempt to allow arguments before main command by treating the first thing that
doesn't look like an option as the command

Show diffs side-by-side

added added

removed removed

26
26
from stdhome import the
27
27
 
28
28
 
29
 
class VcsBzr( Vcs ):
 
29
class BzrVcs( Vcs ):
30
30
 
31
31
 
32
32
        def __init__( self, dir ):
45
45
                os.mkdir( self.dir )
46
46
 
47
47
                # bzr init
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:
 
48
                try:
 
49
                        self.run( [ 'bzr', 'init', '.' ] )
 
50
                except self.VcsError as e:
52
51
 
53
52
                        # attempt to clean-up dir
54
53
                        try:
56
55
                        except OSError:
57
56
                                pass
58
57
 
59
 
                        raise self.VcsError( 'bzr init failed', output )
 
58
                        raise
60
59
 
61
60
 
62
61
        def checkout( self, url ):
69
68
                os.mkdir( self.dir )
70
69
 
71
70
                # bzr co
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:
 
71
                try:
 
72
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
 
73
                except self.VcsError as e:
76
74
 
77
75
                        # attempt to clean-up dir
78
76
                        try:
80
78
                        except OSError:
81
79
                                pass
82
80
 
83
 
                        raise self.VcsError( 'bzr checkout failed', output )
84
 
 
85
 
 
86
 
        def revert( self ):
 
81
                        raise
 
82
 
 
83
 
 
84
        def get_revno( self ):
 
85
                """Obtain some sort of revision identifier
 
86
                """
 
87
 
 
88
                # bzr revert
 
89
                output = self.run( [ 'bzr', 'revno', '--tree' ] )
 
90
 
 
91
                # parse revno
 
92
                buf = StringIO.StringIO( output )
 
93
                return buf.readline().rstrip()
 
94
 
 
95
 
 
96
        def revert( self, revno = None ):
87
97
                """Revert the branch so that there are no outstanding changes or unknown files.
 
98
                If a revno is supplied, then the repository is reverted to that
 
99
                revision.
88
100
                """
89
101
 
 
102
                # bzr st
 
103
                output = self.run( [ 'bzr', 'status' ] )
 
104
                files = self.parse_file_blocks( output )
 
105
 
 
106
                # remove kind changed files (or they can cause `bzr revert` to break in
 
107
                # strange situations, like when a directory has been replaced with a
 
108
                # symlink to a non-existant file)
 
109
                if 'kind changed' in files:
 
110
                        for file in files[ 'kind changed' ]:
 
111
                                matches = re.match( r'(.+?)[/@+]? \([^)]+\)', file )
 
112
                                if not matches:
 
113
                                        raise RunTimeError(
 
114
                                                'failed to parse bzr kind change: %s' % file )
 
115
                                file = matches.group( 1 )
 
116
                                if the.verbose >= 2: print "removing (kind changed): " + file
 
117
                                full_file = os.path.join( self.dir, file )
 
118
                                if os.path.isfile( full_file ) or os.path.islink( full_file ):
 
119
                                        os.unlink( full_file )
 
120
                                elif os.path.isdir( full_file ):
 
121
                                        shutil.rmtree( full_file )
 
122
                                else:
 
123
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
 
124
 
90
125
                # bzr revert
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 )
 
126
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
96
127
 
97
128
                # bzr st
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 )
 
129
                output = self.run( [ 'bzr', 'status' ] )
103
130
                files = self.parse_file_blocks( output )
104
131
 
105
132
                # remove unknown files
106
133
                if 'unknown' in files:
107
134
                        for file in files[ 'unknown' ]:
 
135
                                if the.verbose >= 2: print "removing (unknown): " + file
108
136
                                full_file = os.path.join( self.dir, file )
109
137
                                if os.path.isfile( full_file ):
110
138
                                        os.unlink( full_file )
111
 
                                elif os.full_file.isdir( full_file ):
 
139
                                elif os.path.isdir( full_file ):
112
140
                                        shutil.rmtree( full_file )
113
141
                                else:
114
142
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
115
143
 
 
144
                # if a revision identifier has been given, ensure we're updated to that
 
145
                if revno is not None and self.get_revno() != revno:
 
146
 
 
147
                        # bzr update
 
148
                        self.run( [ 'bzr', 'update', '-r', revno ] )
 
149
 
116
150
 
117
151
        def update( self ):
118
152
                """Update the branch, pulling down any upstream changes and merging them.  This
120
154
                operation.
121
155
                """
122
156
 
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 )
 
157
#               WARNING: the following might cause bzr to ask for your ssh password more than
 
158
#               once during an update!!!
 
159
#
 
160
#               # get revno
 
161
#               revno = self.get_revno()
 
162
#
 
163
#               # update to current revision (pull in history without updating tree)
 
164
#               self.run( [ 'bzr', 'update', '-r', revno ] )
 
165
#
 
166
#               # get log output
 
167
#               next_revno = str( int( revno ) + 1 )
 
168
#               output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
 
169
#
 
170
#               # parse output
 
171
#               keep_files = list()
 
172
#               buf = StringIO.StringIO( output )
 
173
#               in_message = False
 
174
#               for line in buf:
 
175
#                       line = line.rstrip( '\n' )
 
176
#                       if line.lower() == 'message:':
 
177
#                               in_message = True
 
178
#                       elif in_message:
 
179
#                               if line[ : 2 ] != '  ':
 
180
#                                       in_message = False
 
181
#                               else:
 
182
#                                       line = line[ 2 : ]
 
183
#
 
184
#                                       # process directives
 
185
#                                       if line[ : 6 ].lower() == 'keep: ':
 
186
#                                               file = line[ 6 : ]
 
187
#                                               if file in rename_files: file = rename_files[ file ]
 
188
#                                               keep_files.append( file )
 
189
#                                       elif line[ : 8 ].lower() == 'rename: ':
 
190
#                                               rename_from = line[ 8 : ]
 
191
#                                       elif line[ : 4 ].lower() == 'to: ':
 
192
#                                               if rename_from in rename_files:
 
193
#                                                       rename_from = rename_files[ rename_from ]
 
194
#                                               rename_files[ line[ 4 : ] ] = rename_from
 
195
 
 
196
                # bzr update properly
 
197
                output = self.run( [ 'bzr', 'update' ] )
129
198
 
130
199
                # parse output (see logic in report() in bzrlib/delta.py)
131
200
                files = list()
133
202
                for line in buf:
134
203
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
135
204
                        line = line.rstrip()
136
 
                        if the.verbose: print '  %s' % line
 
205
                        if the.verbose >= 2: print '  %s' % line
137
206
 
138
207
                        # renames show before and after file names
139
208
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
155
224
                                continue
156
225
 
157
226
                        raise RuntimeError(
158
 
                                'failed to parse bzr update output line:\n%' % line )
 
227
                                'failed to parse bzr update output line:\n%s' % line )
159
228
 
160
229
                return files
161
230
 
165
234
                """
166
235
 
167
236
                # bzr status
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 )
 
237
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
 
238
 
 
239
                # parse output
173
240
                files = self.parse_file_blocks( output )
174
241
                return True if len( files ) else False
175
242
 
179
246
                """
180
247
 
181
248
                # bzr status
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 )
 
249
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
 
250
 
 
251
                # parse output
187
252
                files = self.parse_file_blocks( output )
188
253
                return files['conflicts'] if 'conflicts' in files else None
189
254
 
190
255
 
 
256
        def add( self, files ):
 
257
                """Make sure files are added to version control.
 
258
                @param files a list of relative filenames
 
259
                """
 
260
 
 
261
                # bzr add
 
262
                self.run( [ 'bzr', 'add', '-N' ] + files )
 
263
 
 
264
 
 
265
        def commit( self ):
 
266
                """Commit changes to the repo.
 
267
                """
 
268
 
 
269
                # bzr commit
 
270
                self.run( [ 'bzr', 'commit', '-m', '' ] )
 
271
 
 
272
 
 
273
        def run( self, cmd ):
 
274
                if the.verbose >= 2: print 'exec: %s' % ' '.join( cmd )
 
275
                p = Popen( cmd, cwd = self.dir,
 
276
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
277
                output = p.communicate()[ 0 ]
 
278
                if p.returncode > 0:
 
279
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
 
280
                return output
 
281
 
 
282
 
191
283
        def parse_file_blocks( self, output ):
192
284
                res = dict()
193
285
                current = None
204
296
                                                res[ current ] = list()
205
297
                                        res[ current ].append( matches.group( 1 ) )
206
298
                                        continue
207
 
                        if re.search( '^[0-9]+ shelf exists', line ): continue
 
299
                        if re.search( '^[0-9]+ shel(?:f|ves) exists?', line ): continue
208
300
                        if re.search( '^working tree is out of date', line ): continue
209
301
                        raise self.ParseError( "unrecognised line: %s" % line )
210
302
                return res