Show More
@@ -1,303 +1,326 | |||
|
1 | 1 | # GNU Arch support for the convert extension |
|
2 | 2 | |
|
3 | 3 | from common import NoRepo, commandline, commit, converter_source |
|
4 | 4 | from mercurial.i18n import _ |
|
5 | 5 | from mercurial import util |
|
6 | 6 | import os, shutil, tempfile, stat, locale |
|
7 | 7 | from email.Parser import Parser |
|
8 | 8 | |
|
9 | 9 | class gnuarch_source(converter_source, commandline): |
|
10 | 10 | |
|
11 | 11 | class gnuarch_rev: |
|
12 | 12 | def __init__(self, rev): |
|
13 | 13 | self.rev = rev |
|
14 | 14 | self.summary = '' |
|
15 | 15 | self.date = None |
|
16 | 16 | self.author = '' |
|
17 | 17 | self.continuationof = None |
|
18 | 18 | self.add_files = [] |
|
19 | 19 | self.mod_files = [] |
|
20 | 20 | self.del_files = [] |
|
21 | 21 | self.ren_files = {} |
|
22 | 22 | self.ren_dirs = {} |
|
23 | 23 | |
|
24 | 24 | def __init__(self, ui, path, rev=None): |
|
25 | 25 | super(gnuarch_source, self).__init__(ui, path, rev=rev) |
|
26 | 26 | |
|
27 | 27 | if not os.path.exists(os.path.join(path, '{arch}')): |
|
28 | 28 | raise NoRepo(_("%s does not look like a GNU Arch repo") % path) |
|
29 | 29 | |
|
30 | 30 | # Could use checktool, but we want to check for baz or tla. |
|
31 | 31 | self.execmd = None |
|
32 | 32 | if util.find_exe('baz'): |
|
33 | 33 | self.execmd = 'baz' |
|
34 | 34 | else: |
|
35 | 35 | if util.find_exe('tla'): |
|
36 | 36 | self.execmd = 'tla' |
|
37 | 37 | else: |
|
38 | 38 | raise util.Abort(_('cannot find a GNU Arch tool')) |
|
39 | 39 | |
|
40 | 40 | commandline.__init__(self, ui, self.execmd) |
|
41 | 41 | |
|
42 | 42 | self.path = os.path.realpath(path) |
|
43 | 43 | self.tmppath = None |
|
44 | 44 | |
|
45 | 45 | self.treeversion = None |
|
46 | 46 | self.lastrev = None |
|
47 | 47 | self.changes = {} |
|
48 | 48 | self.parents = {} |
|
49 | 49 | self.tags = {} |
|
50 | 50 | self.modecache = {} |
|
51 | 51 | self.catlogparser = Parser() |
|
52 | 52 | self.locale = locale.getpreferredencoding() |
|
53 | 53 | self.archives = [] |
|
54 | 54 | |
|
55 | 55 | def before(self): |
|
56 | 56 | # Get registered archives |
|
57 | 57 | self.archives = [i.rstrip('\n') |
|
58 | 58 | for i in self.runlines0('archives', '-n')] |
|
59 | 59 | |
|
60 | 60 | if self.execmd == 'tla': |
|
61 | 61 | output = self.run0('tree-version', self.path) |
|
62 | 62 | else: |
|
63 | 63 | output = self.run0('tree-version', '-d', self.path) |
|
64 | 64 | self.treeversion = output.strip() |
|
65 | 65 | |
|
66 | self.ui.status(_('analyzing tree version %s...\n') % self.treeversion) | |
|
67 | ||
|
68 | 66 | # Get name of temporary directory |
|
69 | 67 | version = self.treeversion.split('/') |
|
70 | 68 | self.tmppath = os.path.join(tempfile.gettempdir(), |
|
71 | 69 | 'hg-%s' % version[1]) |
|
72 | 70 | |
|
73 | 71 | # Generate parents dictionary |
|
74 | child = [] | |
|
75 |
|
|
|
76 | self.checkexit(status, 'archive registered?') | |
|
77 | for l in output: | |
|
78 | rev = l.strip() | |
|
79 | self.changes[rev] = self.gnuarch_rev(rev) | |
|
72 | self.parents[None] = [] | |
|
73 | treeversion = self.treeversion | |
|
74 | child = None | |
|
75 | while treeversion: | |
|
76 | self.ui.status(_('analyzing tree version %s...\n') % treeversion) | |
|
77 | ||
|
78 | archive = treeversion.split('/')[0] | |
|
79 | if archive not in self.archives: | |
|
80 | self.ui.status(_('tree analysis stopped because it points to an unregistered archive %s...\n') % archive) | |
|
81 | break | |
|
82 | ||
|
83 | # Get the complete list of revisions for that tree version | |
|
84 | output, status = self.runlines('revisions', '-r', '-f', treeversion) | |
|
85 | self.checkexit(status, 'failed retrieveing revisions for %s' % treeversion) | |
|
86 | ||
|
87 | # No new iteration unless a revision has a continuation-of header | |
|
88 | treeversion = None | |
|
89 | ||
|
90 | for l in output: | |
|
91 | rev = l.strip() | |
|
92 | self.changes[rev] = self.gnuarch_rev(rev) | |
|
93 | self.parents[rev] = [] | |
|
80 | 94 | |
|
81 | # Read author, date and summary | |
|
82 | catlog, status = self.run('cat-log', '-d', self.path, rev) | |
|
83 | if status: | |
|
84 | catlog = self.run0('cat-archive-log', rev) | |
|
85 | self._parsecatlog(catlog, rev) | |
|
95 | # Read author, date and summary | |
|
96 | catlog, status = self.run('cat-log', '-d', self.path, rev) | |
|
97 | if status: | |
|
98 | catlog = self.run0('cat-archive-log', rev) | |
|
99 | self._parsecatlog(catlog, rev) | |
|
100 | ||
|
101 | # Populate the parents map | |
|
102 | self.parents[child].append(rev) | |
|
86 | 103 | |
|
87 | self.parents[rev] = child | |
|
88 | child = [rev] | |
|
89 | if rev == self.rev: | |
|
90 | break | |
|
91 | self.parents[None] = child | |
|
104 | # Keep track of the current revision as the child of the next | |
|
105 | # revision scanned | |
|
106 | child = rev | |
|
107 | ||
|
108 | # Check if we have to follow the usual incremental history | |
|
109 | # or if we have to 'jump' to a different treeversion given | |
|
110 | # by the continuation-of header. | |
|
111 | if self.changes[rev].continuationof: | |
|
112 | treeversion = '--'.join(self.changes[rev].continuationof.split('--')[:-1]) | |
|
113 | break | |
|
114 | ||
|
115 | # If we reached a base-0 revision w/o any continuation-of | |
|
116 | # header, it means the tree history ends here. | |
|
117 | if rev[-6:] == 'base-0': | |
|
118 | break | |
|
92 | 119 | |
|
93 | 120 | def after(self): |
|
94 | 121 | self.ui.debug(_('cleaning up %s\n') % self.tmppath) |
|
95 | 122 | shutil.rmtree(self.tmppath, ignore_errors=True) |
|
96 | 123 | |
|
97 | 124 | def getheads(self): |
|
98 | 125 | return self.parents[None] |
|
99 | 126 | |
|
100 | 127 | def getfile(self, name, rev): |
|
101 | 128 | if rev != self.lastrev: |
|
102 | 129 | raise util.Abort(_('internal calling inconsistency')) |
|
103 | 130 | |
|
104 | 131 | # Raise IOError if necessary (i.e. deleted files). |
|
105 | 132 | if not os.path.exists(os.path.join(self.tmppath, name)): |
|
106 | 133 | raise IOError |
|
107 | 134 | |
|
108 | 135 | data, mode = self._getfile(name, rev) |
|
109 | 136 | self.modecache[(name, rev)] = mode |
|
110 | 137 | |
|
111 | 138 | return data |
|
112 | 139 | |
|
113 | 140 | def getmode(self, name, rev): |
|
114 | 141 | return self.modecache[(name, rev)] |
|
115 | 142 | |
|
116 | 143 | def getchanges(self, rev): |
|
117 | 144 | self.modecache = {} |
|
118 | 145 | self._update(rev) |
|
119 | 146 | changes = [] |
|
120 | 147 | copies = {} |
|
121 | 148 | |
|
122 | 149 | for f in self.changes[rev].add_files: |
|
123 | 150 | changes.append((f, rev)) |
|
124 | 151 | |
|
125 | 152 | for f in self.changes[rev].mod_files: |
|
126 | 153 | changes.append((f, rev)) |
|
127 | 154 | |
|
128 | 155 | for f in self.changes[rev].del_files: |
|
129 | 156 | changes.append((f, rev)) |
|
130 | 157 | |
|
131 | 158 | for src in self.changes[rev].ren_files: |
|
132 | 159 | to = self.changes[rev].ren_files[src] |
|
133 | 160 | changes.append((src, rev)) |
|
134 | 161 | changes.append((to, rev)) |
|
135 | 162 | copies[src] = to |
|
136 | 163 | |
|
137 | 164 | for src in self.changes[rev].ren_dirs: |
|
138 | 165 | to = self.changes[rev].ren_dirs[src] |
|
139 | 166 | chgs, cps = self._rendirchanges(src, to); |
|
140 | 167 | changes += [(f, rev) for f in chgs] |
|
141 | 168 | for c in cps: |
|
142 | 169 | copies[c] = cps[c] |
|
143 | 170 | |
|
144 | 171 | self.lastrev = rev |
|
145 | 172 | return util.sort(changes), copies |
|
146 | 173 | |
|
147 | 174 | def getcommit(self, rev): |
|
148 | 175 | changes = self.changes[rev] |
|
149 | 176 | return commit(author = changes.author, date = changes.date, |
|
150 | 177 | desc = changes.summary, parents = self.parents[rev], rev=rev) |
|
151 | 178 | |
|
152 | 179 | def gettags(self): |
|
153 | 180 | return self.tags |
|
154 | 181 | |
|
155 | 182 | def _execute(self, cmd, *args, **kwargs): |
|
156 | 183 | cmdline = [self.execmd, cmd] |
|
157 | 184 | cmdline += args |
|
158 | 185 | cmdline = [util.shellquote(arg) for arg in cmdline] |
|
159 | 186 | cmdline += ['>', util.nulldev, '2>', util.nulldev] |
|
160 | 187 | cmdline = util.quotecommand(' '.join(cmdline)) |
|
161 | 188 | self.ui.debug(cmdline, '\n') |
|
162 | 189 | return os.system(cmdline) |
|
163 | 190 | |
|
164 | 191 | def _update(self, rev): |
|
165 | if rev[-6:] == 'base-0': | |
|
166 | # Initialise 'base-0' revision | |
|
192 | self.ui.debug(_('applying revision %s...\n') % rev) | |
|
193 | changeset, status = self.runlines('replay', '-d', self.tmppath, | |
|
194 | rev) | |
|
195 | if status: | |
|
196 | # Something went wrong while merging (baz or tla | |
|
197 | # issue?), get latest revision and try from there | |
|
198 | shutil.rmtree(self.tmppath, ignore_errors=True) | |
|
167 | 199 | self._obtainrevision(rev) |
|
168 | 200 | else: |
|
169 | self.ui.debug(_('applying revision %s...\n') % rev) | |
|
170 | changeset, status = self.runlines('replay', '-d', self.tmppath, | |
|
171 |
|
|
|
172 | if status: | |
|
173 | # Something went wrong while merging (baz or tla | |
|
174 | # issue?), get latest revision and try from there | |
|
175 | shutil.rmtree(self.tmppath, ignore_errors=True) | |
|
176 | self._obtainrevision(rev) | |
|
177 | else: | |
|
178 | old_rev = self.parents[rev][0] | |
|
179 | self.ui.debug(_('computing changeset between %s and %s...\n') | |
|
180 | % (old_rev, rev)) | |
|
181 | self._parsechangeset(changeset, rev) | |
|
201 | old_rev = self.parents[rev][0] | |
|
202 | self.ui.debug(_('computing changeset between %s and %s...\n') | |
|
203 | % (old_rev, rev)) | |
|
204 | self._parsechangeset(changeset, rev) | |
|
182 | 205 | |
|
183 | 206 | def _getfile(self, name, rev): |
|
184 | 207 | mode = os.lstat(os.path.join(self.tmppath, name)).st_mode |
|
185 | 208 | if stat.S_ISLNK(mode): |
|
186 | 209 | data = os.readlink(os.path.join(self.tmppath, name)) |
|
187 | 210 | mode = mode and 'l' or '' |
|
188 | 211 | else: |
|
189 | 212 | data = open(os.path.join(self.tmppath, name), 'rb').read() |
|
190 | 213 | mode = (mode & 0111) and 'x' or '' |
|
191 | 214 | return data, mode |
|
192 | 215 | |
|
193 | 216 | def _exclude(self, name): |
|
194 | 217 | exclude = [ '{arch}', '.arch-ids', '.arch-inventory' ] |
|
195 | 218 | for exc in exclude: |
|
196 | 219 | if name.find(exc) != -1: |
|
197 | 220 | return True |
|
198 | 221 | return False |
|
199 | 222 | |
|
200 | 223 | def _readcontents(self, path): |
|
201 | 224 | files = [] |
|
202 | 225 | contents = os.listdir(path) |
|
203 | 226 | while len(contents) > 0: |
|
204 | 227 | c = contents.pop() |
|
205 | 228 | p = os.path.join(path, c) |
|
206 | 229 | # os.walk could be used, but here we avoid internal GNU |
|
207 | 230 | # Arch files and directories, thus saving a lot time. |
|
208 | 231 | if not self._exclude(p): |
|
209 | 232 | if os.path.isdir(p): |
|
210 | 233 | contents += [os.path.join(c, f) for f in os.listdir(p)] |
|
211 | 234 | else: |
|
212 | 235 | files.append(c) |
|
213 | 236 | return files |
|
214 | 237 | |
|
215 | 238 | def _rendirchanges(self, src, dest): |
|
216 | 239 | changes = [] |
|
217 | 240 | copies = {} |
|
218 | 241 | files = self._readcontents(os.path.join(self.tmppath, dest)) |
|
219 | 242 | for f in files: |
|
220 | 243 | s = os.path.join(src, f) |
|
221 | 244 | d = os.path.join(dest, f) |
|
222 | 245 | changes.append(s) |
|
223 | 246 | changes.append(d) |
|
224 | 247 | copies[s] = d |
|
225 | 248 | return changes, copies |
|
226 | 249 | |
|
227 | 250 | def _obtainrevision(self, rev): |
|
228 | 251 | self.ui.debug(_('obtaining revision %s...\n') % rev) |
|
229 | 252 | output = self._execute('get', rev, self.tmppath) |
|
230 | 253 | self.checkexit(output) |
|
231 | 254 | self.ui.debug(_('analysing revision %s...\n') % rev) |
|
232 | 255 | files = self._readcontents(self.tmppath) |
|
233 | 256 | self.changes[rev].add_files += files |
|
234 | 257 | |
|
235 | 258 | def _stripbasepath(self, path): |
|
236 | 259 | if path.startswith('./'): |
|
237 | 260 | return path[2:] |
|
238 | 261 | return path |
|
239 | 262 | |
|
240 | 263 | def _parsecatlog(self, data, rev): |
|
241 | 264 | try: |
|
242 | 265 | catlog = self.catlogparser.parsestr(data) |
|
243 | 266 | self.changes[rev].date = util.datestr( |
|
244 | 267 | util.strdate(catlog['Standard-date'], |
|
245 | 268 | '%Y-%m-%d %H:%M:%S')) |
|
246 | 269 | self.changes[rev].author = catlog['Creator'] |
|
247 | 270 | self.changes[rev].summary = catlog['Summary'] |
|
248 | 271 | if catlog.has_key('Continuation-of'): |
|
249 | 272 | self.changes[rev].continuationof = catlog['Continuation-of'] |
|
250 | 273 | except Exception, err: |
|
251 | 274 | raise util.Abort(_('could not parse cat-log of %s') % rev) |
|
252 | 275 | |
|
253 | 276 | def _parsechangeset(self, data, rev): |
|
254 | 277 | for l in data: |
|
255 | 278 | l = l.strip() |
|
256 | 279 | # Added file (ignore added directory) |
|
257 | 280 | if l.startswith('A') and not l.startswith('A/'): |
|
258 | 281 | file = self._stripbasepath(l[1:].strip()) |
|
259 | 282 | if not self._exclude(file): |
|
260 | 283 | self.changes[rev].add_files.append(file) |
|
261 | 284 | # Deleted file (ignore deleted directory) |
|
262 | 285 | elif l.startswith('D') and not l.startswith('D/'): |
|
263 | 286 | file = self._stripbasepath(l[1:].strip()) |
|
264 | 287 | if not self._exclude(file): |
|
265 | 288 | self.changes[rev].del_files.append(file) |
|
266 | 289 | # Modified binary file |
|
267 | 290 | elif l.startswith('Mb'): |
|
268 | 291 | file = self._stripbasepath(l[2:].strip()) |
|
269 | 292 | if not self._exclude(file): |
|
270 | 293 | self.changes[rev].mod_files.append(file) |
|
271 | 294 | # Modified link |
|
272 | 295 | elif l.startswith('M->'): |
|
273 | 296 | file = self._stripbasepath(l[3:].strip()) |
|
274 | 297 | if not self._exclude(file): |
|
275 | 298 | self.changes[rev].mod_files.append(file) |
|
276 | 299 | # Modified file |
|
277 | 300 | elif l.startswith('M'): |
|
278 | 301 | file = self._stripbasepath(l[1:].strip()) |
|
279 | 302 | if not self._exclude(file): |
|
280 | 303 | self.changes[rev].mod_files.append(file) |
|
281 | 304 | # Renamed file (or link) |
|
282 | 305 | elif l.startswith('=>'): |
|
283 | 306 | files = l[2:].strip().split(' ') |
|
284 | 307 | if len(files) == 1: |
|
285 | 308 | files = l[2:].strip().split('\t') |
|
286 | 309 | src = self._stripbasepath(files[0]) |
|
287 | 310 | dst = self._stripbasepath(files[1]) |
|
288 | 311 | if not self._exclude(src) and not self._exclude(dst): |
|
289 | 312 | self.changes[rev].ren_files[src] = dst |
|
290 | 313 | # Conversion from file to link or from link to file (modified) |
|
291 | 314 | elif l.startswith('ch'): |
|
292 | 315 | file = self._stripbasepath(l[2:].strip()) |
|
293 | 316 | if not self._exclude(file): |
|
294 | 317 | self.changes[rev].mod_files.append(file) |
|
295 | 318 | # Renamed directory |
|
296 | 319 | elif l.startswith('/>'): |
|
297 | 320 | dirs = l[2:].strip().split(' ') |
|
298 | 321 | if len(dirs) == 1: |
|
299 | 322 | dirs = l[2:].strip().split('\t') |
|
300 | 323 | src = self._stripbasepath(dirs[0]) |
|
301 | 324 | dst = self._stripbasepath(dirs[1]) |
|
302 | 325 | if not self._exclude(src) and not self._exclude(dst): |
|
303 | 326 | self.changes[rev].ren_dirs[src] = dst |
General Comments 0
You need to be logged in to leave comments.
Login now