/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 ):
45
45
                os.mkdir( self.dir )
46
46
 
47
47
                # bzr init
48
 
                try:
49
 
                        self.run( [ 'bzr', 'init', '.' ] )
50
 
                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:
51
52
 
52
53
                        # attempt to clean-up dir
53
54
                        try:
55
56
                        except OSError:
56
57
                                pass
57
58
 
58
 
                        raise
 
59
                        raise self.VcsError( 'bzr init failed', output )
59
60
 
60
61
 
61
62
        def checkout( self, url ):
68
69
                os.mkdir( self.dir )
69
70
 
70
71
                # bzr co
71
 
                try:
72
 
                        self.run( [ 'bzr', 'checkout', url, '.' ] )
73
 
                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:
74
76
 
75
77
                        # attempt to clean-up dir
76
78
                        try:
78
80
                        except OSError:
79
81
                                pass
80
82
 
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 ):
 
83
                        raise self.VcsError( 'bzr checkout failed', output )
 
84
 
 
85
 
 
86
        def revert( self ):
97
87
                """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
88
                """
101
89
 
102
90
                # bzr revert
103
 
                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 )
104
96
 
105
97
                # bzr st
106
 
                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 )
107
103
                files = self.parse_file_blocks( output )
108
104
 
109
105
                # remove unknown files
117
113
                                else:
118
114
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
119
115
 
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
116
 
127
117
        def update( self ):
128
118
                """Update the branch, pulling down any upstream changes and merging them.  This
130
120
                operation.
131
121
                """
132
122
 
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' ] )
 
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 )
174
129
 
175
130
                # parse output (see logic in report() in bzrlib/delta.py)
176
131
                files = list()
178
133
                for line in buf:
179
134
                        if not re.search( '^[-R+ ?][K NMD!][* ] ', line ): continue
180
135
                        line = line.rstrip()
181
 
                        if the.verbose > 1: print '  %s' % line
 
136
                        if the.verbose: print '  %s' % line
182
137
 
183
138
                        # renames show before and after file names
184
139
                        matches = re.search( '^R.. (.*?)[/@+]? => (.*?)[/@+]?$', line )
210
165
                """
211
166
 
212
167
                # bzr status
213
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
214
 
 
215
 
                # 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 )
216
173
                files = self.parse_file_blocks( output )
217
174
                return True if len( files ) else False
218
175
 
222
179
                """
223
180
 
224
181
                # bzr status
225
 
                output = self.run( [ 'bzr', 'status', '--no-pending' ] )
226
 
 
227
 
                # 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 )
228
187
                files = self.parse_file_blocks( output )
229
188
                return files['conflicts'] if 'conflicts' in files else None
230
189
 
231
190
 
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
191
        def parse_file_blocks( self, output ):
243
192
                res = dict()
244
193
                current = None