/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: 2013-12-08 19:28:11 UTC
  • Revision ID: tim@ed.am-20131208192811-r20qj7cgmn4duw11
initial commit; basic app startup and initial command-line processing

Show diffs side-by-side

added added

removed removed

1
 
# bzr.py
2
 
#
3
 
# Copyright (C) 2014 Tim Marston <tim@edm.am>
4
 
#
5
 
# This file is part of stdhome (hereafter referred to as "this program").
6
 
# See http://ed.am/dev/stdhome for more information.
7
 
#
8
 
# This program is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License as published by
10
 
# the Free Software Foundation, either version 3 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# This program is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
# GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License
19
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 
21
 
 
22
 
import subprocess, os, re, shutil
23
 
from subprocess import Popen
24
 
import StringIO
25
 
from vcs import Vcs
26
 
from stdhome import the
27
 
 
28
 
 
29
 
class BzrVcs( Vcs ):
30
 
 
31
 
 
32
 
        def __init__( self, dir ):
33
 
                """Init class
34
 
 
35
 
                @param dir the fully-qualified directory to work in.
36
 
                """
37
 
                self.dir = dir
38
 
 
39
 
 
40
 
        def init( self ):
41
 
                """Create a new, empty branch
42
 
                """
43
 
 
44
 
                # the directory shouldn't exist
45
 
                os.mkdir( self.dir )
46
 
 
47
 
                # bzr init
48
 
                try:
49
 
                        self.run( [ 'bzr', 'init', '.' ] )
50
 
                except self.VcsError as e:
51
 
 
52
 
                        # attempt to clean-up dir
53
 
                        try:
54
 
                                shutil.rmtree( self.dir )
55
 
                        except OSError:
56
 
                                pass
57
 
 
58
 
                        raise
59
 
 
60
 
 
61
 
        def checkout( self, url ):
62
 
                """Checkout a new copy of a remote branch.
63
 
 
64
 
                @param url the remote repository URL
65
 
                """
66
 
 
67
 
                # the directory shouldn't exist
68
 
                os.mkdir( self.dir )
69
 
 
70
 
                # bzr co
71
 
                try:
72
 
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
73
 
                except self.VcsError as e:
74
 
 
75
 
                        # attempt to clean-up dir
76
 
                        try:
77
 
                                shutil.rmtree( self.dir )
78
 
                        except OSError:
79
 
                                pass
80
 
 
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 ):
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.
100
 
                """
101
 
 
102
 
                # bzr revert
103
 
                self.run( [ 'bzr', 'revert', '--no-backup' ] )
104
 
 
105
 
                # bzr st
106
 
                output = self.run( [ 'bzr', 'status' ] )
107
 
                files = self.parse_file_blocks( output )
108
 
 
109
 
                # remove unknown files
110
 
                if 'unknown' in files:
111
 
                        for file in files[ 'unknown' ]:
112
 
                                full_file = os.path.join( self.dir, file )
113
 
                                if os.path.isfile( full_file ):
114
 
                                        os.unlink( full_file )
115
 
                                elif os.full_file.isdir( full_file ):
116
 
                                        shutil.rmtree( full_file )
117
 
                                else:
118
 
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
119
 
 
120
 
                # if a revision identifyer has been given, update to that
121
 
                if revno is not None:
122
 
 
123
 
                        # bzr update
124
 
                        self.run( [ 'bzr', 'update', '-r', revno ] )
125
 
 
126
 
 
127
 
        def update( self ):
128
 
                """Update the branch, pulling down any upstream changes and merging them.  This
129
 
                method returns a list of the files that were modified as part of this
130
 
                operation.
131
 
                """
132
 
 
133
 
#               WARNING: the following might cause bzr to ask for your ssh password more than
134
 
#               once during an update!!!
135
 
#
136
 
#               # get revno
137
 
#               revno = self.get_revno()
138
 
#
139
 
#               # update to current revision (pull in history without updating tree)
140
 
#               self.run( [ 'bzr', 'update', '-r', revno ] )
141
 
#
142
 
#               # get log output
143
 
#               next_revno = str( int( revno ) + 1 )
144
 
#               output = self.run( [ 'bzr', 'log', '-r', next_revno + '..' ] )
145
 
#
146
 
#               # parse output
147
 
#               keep_files = list()
148
 
#               buf = StringIO.StringIO( output )
149
 
#               in_message = False
150
 
#               for line in buf:
151
 
#                       line = line.rstrip( '\n' )
152
 
#                       if line.lower() == 'message:':
153
 
#                               in_message = True
154
 
#                       elif in_message:
155
 
#                               if line[ : 2 ] != '  ':
156
 
#                                       in_message = False
157
 
#                               else:
158
 
#                                       line = line[ 2 : ]
159
 
#
160
 
#                                       # process directives
161
 
#                                       if line[ : 6 ].lower() == 'keep: ':
162
 
#                                               file = line[ 6 : ]
163
 
#                                               if file in rename_files: file = rename_files[ file ]
164
 
#                                               keep_files.append( file )
165
 
#                                       elif line[ : 8 ].lower() == 'rename: ':
166
 
#                                               rename_from = line[ 8 : ]
167
 
#                                       elif line[ : 4 ].lower() == 'to: ':
168
 
#                                               if rename_from in rename_files:
169
 
#                                                       rename_from = rename_files[ rename_from ]
170
 
#                                               rename_files[ line[ 4 : ] ] = rename_from
171
 
 
172
 
                # bzr update properly
173
 
                output = self.run( [ 'bzr', 'update' ] )
174
 
 
175
 
                # parse output (see logic in report() in bzrlib/delta.py)
176
 
                files = list()
177
 
                buf = StringIO.StringIO( output )
178
 
                for line in buf:
179
 
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
180
 
                        line = line.rstrip()
181
 
                        if the.verbose > 1: print '  %s' % line
182
 
 
183
 
                        # renames show before and after file names
184
 
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
185
 
                        if matches:
186
 
                                files.append( matches.group( 1 ) )
187
 
                                files.append( matches.group( 2 ) )
188
 
                                continue
189
 
 
190
 
                        # kind changes shows the same name twice
191
 
                        matches = re.search( '^.K. (.*?)[/@+]? => (.*?)[/@+]?$', line )
192
 
                        if matches:
193
 
                                files.append( matches.group( 1 ) )
194
 
                                continue
195
 
 
196
 
                        # other entries have only one filename
197
 
                        matches = re.search( '^... (.*?)[/@+]?$', line )
198
 
                        if matches:
199
 
                                files.append( matches.group( 1 ) )
200
 
                                continue
201
 
 
202
 
                        raise RuntimeError(
203
 
                                'failed to parse bzr update output line:\n%' % line )
204
 
 
205
 
                return files
206
 
 
207
 
 
208
 
        def has_changes( self ):
209
 
                """Check if the branch has any local modifications.
210
 
                """
211
 
 
212
 
                # bzr status
213
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
214
 
 
215
 
                # parse output
216
 
                files = self.parse_file_blocks( output )
217
 
                return True if len( files ) else False
218
 
 
219
 
 
220
 
        def get_conflicts( self ):
221
 
                """Return a list of files that have conflicts.
222
 
                """
223
 
 
224
 
                # bzr status
225
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
226
 
 
227
 
                # parse output
228
 
                files = self.parse_file_blocks( output )
229
 
                return files['conflicts'] if 'conflicts' in files else None
230
 
 
231
 
 
232
 
        def run( self, cmd ):
233
 
                if the.verbose > 1: print 'exec: %s' % ' '.join( cmd )
234
 
                p = Popen( cmd, cwd = self.dir,
235
 
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
236
 
                output = p.communicate()[ 0 ]
237
 
                if p.returncode > 0:
238
 
                        raise self.VcsError( ' '.join( cmd[ : 2 ] ), output )
239
 
                return output
240
 
 
241
 
 
242
 
        def parse_file_blocks( self, output ):
243
 
                res = dict()
244
 
                current = None
245
 
                buf = StringIO.StringIO( output )
246
 
                for line in buf:
247
 
                        matches = re.search( '^([a-z ]+):$', line, re.I )
248
 
                        if matches:
249
 
                                current = matches.group( 1 )
250
 
                                continue
251
 
                        if current:
252
 
                                matches = re.search( '^  ([^ ].*)$', line )
253
 
                                if matches:
254
 
                                        if not current in res:
255
 
                                                res[ current ] = list()
256
 
                                        res[ current ].append( matches.group( 1 ) )
257
 
                                        continue
258
 
                        if re.search( '^[0-9]+ shelf exists', line ): continue
259
 
                        if re.search( '^working tree is out of date', line ): continue
260
 
                        raise self.ParseError( "unrecognised line: %s" % line )
261
 
                return res
262
 
 
263
 
 
264
 
        class ParseError( Exception ):
265
 
                pass