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