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