##// END OF EJS Templates
convert: monotone use absolute_import
timeless -
r28372:74d03766 default
parent child Browse files
Show More
@@ -1,365 +1,371 b''
1 1 # monotone.py - monotone support for the convert extension
2 2 #
3 3 # Copyright 2008, 2009 Mikkel Fahnoe Jorgensen <mikkel@dvide.com> and
4 4 # others
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 from __future__ import absolute_import
8 9
9 import os, re
10 from mercurial import util, error
11 from common import NoRepo, commit, converter_source, checktool
12 from common import commandline
10 import os
11 import re
12
13 from mercurial import (
14 error,
15 util,
16 )
13 17 from mercurial.i18n import _
14 18
15 class monotone_source(converter_source, commandline):
19 from . import common
20
21 class monotone_source(common.converter_source, common.commandline):
16 22 def __init__(self, ui, path=None, revs=None):
17 converter_source.__init__(self, ui, path, revs)
23 common.converter_source.__init__(self, ui, path, revs)
18 24 if revs and len(revs) > 1:
19 25 raise error.Abort(_('monotone source does not support specifying '
20 26 'multiple revs'))
21 commandline.__init__(self, ui, 'mtn')
27 common.commandline.__init__(self, ui, 'mtn')
22 28
23 29 self.ui = ui
24 30 self.path = path
25 31 self.automatestdio = False
26 32 self.revs = revs
27 33
28 norepo = NoRepo(_("%s does not look like a monotone repository")
29 % path)
34 norepo = common.NoRepo(_("%s does not look like a monotone repository")
35 % path)
30 36 if not os.path.exists(os.path.join(path, '_MTN')):
31 37 # Could be a monotone repository (SQLite db file)
32 38 try:
33 39 f = file(path, 'rb')
34 40 header = f.read(16)
35 41 f.close()
36 42 except IOError:
37 43 header = ''
38 44 if header != 'SQLite format 3\x00':
39 45 raise norepo
40 46
41 47 # regular expressions for parsing monotone output
42 48 space = r'\s*'
43 49 name = r'\s+"((?:\\"|[^"])*)"\s*'
44 50 value = name
45 51 revision = r'\s+\[(\w+)\]\s*'
46 52 lines = r'(?:.|\n)+'
47 53
48 54 self.dir_re = re.compile(space + "dir" + name)
49 55 self.file_re = re.compile(space + "file" + name +
50 56 "content" + revision)
51 57 self.add_file_re = re.compile(space + "add_file" + name +
52 58 "content" + revision)
53 59 self.patch_re = re.compile(space + "patch" + name +
54 60 "from" + revision + "to" + revision)
55 61 self.rename_re = re.compile(space + "rename" + name + "to" + name)
56 62 self.delete_re = re.compile(space + "delete" + name)
57 63 self.tag_re = re.compile(space + "tag" + name + "revision" +
58 64 revision)
59 65 self.cert_re = re.compile(lines + space + "name" + name +
60 66 "value" + value)
61 67
62 68 attr = space + "file" + lines + space + "attr" + space
63 69 self.attr_execute_re = re.compile(attr + '"mtn:execute"' +
64 70 space + '"true"')
65 71
66 72 # cached data
67 73 self.manifest_rev = None
68 74 self.manifest = None
69 75 self.files = None
70 76 self.dirs = None
71 77
72 checktool('mtn', abort=False)
78 common.checktool('mtn', abort=False)
73 79
74 80 def mtnrun(self, *args, **kwargs):
75 81 if self.automatestdio:
76 82 return self.mtnrunstdio(*args, **kwargs)
77 83 else:
78 84 return self.mtnrunsingle(*args, **kwargs)
79 85
80 86 def mtnrunsingle(self, *args, **kwargs):
81 87 kwargs['d'] = self.path
82 88 return self.run0('automate', *args, **kwargs)
83 89
84 90 def mtnrunstdio(self, *args, **kwargs):
85 91 # Prepare the command in automate stdio format
86 92 command = []
87 93 for k, v in kwargs.iteritems():
88 94 command.append("%s:%s" % (len(k), k))
89 95 if v:
90 96 command.append("%s:%s" % (len(v), v))
91 97 if command:
92 98 command.insert(0, 'o')
93 99 command.append('e')
94 100
95 101 command.append('l')
96 102 for arg in args:
97 103 command += "%s:%s" % (len(arg), arg)
98 104 command.append('e')
99 105 command = ''.join(command)
100 106
101 107 self.ui.debug("mtn: sending '%s'\n" % command)
102 108 self.mtnwritefp.write(command)
103 109 self.mtnwritefp.flush()
104 110
105 111 return self.mtnstdioreadcommandoutput(command)
106 112
107 113 def mtnstdioreadpacket(self):
108 114 read = None
109 115 commandnbr = ''
110 116 while read != ':':
111 117 read = self.mtnreadfp.read(1)
112 118 if not read:
113 119 raise error.Abort(_('bad mtn packet - no end of commandnbr'))
114 120 commandnbr += read
115 121 commandnbr = commandnbr[:-1]
116 122
117 123 stream = self.mtnreadfp.read(1)
118 124 if stream not in 'mewptl':
119 125 raise error.Abort(_('bad mtn packet - bad stream type %s') % stream)
120 126
121 127 read = self.mtnreadfp.read(1)
122 128 if read != ':':
123 129 raise error.Abort(_('bad mtn packet - no divider before size'))
124 130
125 131 read = None
126 132 lengthstr = ''
127 133 while read != ':':
128 134 read = self.mtnreadfp.read(1)
129 135 if not read:
130 136 raise error.Abort(_('bad mtn packet - no end of packet size'))
131 137 lengthstr += read
132 138 try:
133 139 length = long(lengthstr[:-1])
134 140 except TypeError:
135 141 raise error.Abort(_('bad mtn packet - bad packet size %s')
136 142 % lengthstr)
137 143
138 144 read = self.mtnreadfp.read(length)
139 145 if len(read) != length:
140 146 raise error.Abort(_("bad mtn packet - unable to read full packet "
141 147 "read %s of %s") % (len(read), length))
142 148
143 149 return (commandnbr, stream, length, read)
144 150
145 151 def mtnstdioreadcommandoutput(self, command):
146 152 retval = []
147 153 while True:
148 154 commandnbr, stream, length, output = self.mtnstdioreadpacket()
149 155 self.ui.debug('mtn: read packet %s:%s:%s\n' %
150 156 (commandnbr, stream, length))
151 157
152 158 if stream == 'l':
153 159 # End of command
154 160 if output != '0':
155 161 raise error.Abort(_("mtn command '%s' returned %s") %
156 162 (command, output))
157 163 break
158 164 elif stream in 'ew':
159 165 # Error, warning output
160 166 self.ui.warn(_('%s error:\n') % self.command)
161 167 self.ui.warn(output)
162 168 elif stream == 'p':
163 169 # Progress messages
164 170 self.ui.debug('mtn: ' + output)
165 171 elif stream == 'm':
166 172 # Main stream - command output
167 173 retval.append(output)
168 174
169 175 return ''.join(retval)
170 176
171 177 def mtnloadmanifest(self, rev):
172 178 if self.manifest_rev == rev:
173 179 return
174 180 self.manifest = self.mtnrun("get_manifest_of", rev).split("\n\n")
175 181 self.manifest_rev = rev
176 182 self.files = {}
177 183 self.dirs = {}
178 184
179 185 for e in self.manifest:
180 186 m = self.file_re.match(e)
181 187 if m:
182 188 attr = ""
183 189 name = m.group(1)
184 190 node = m.group(2)
185 191 if self.attr_execute_re.match(e):
186 192 attr += "x"
187 193 self.files[name] = (node, attr)
188 194 m = self.dir_re.match(e)
189 195 if m:
190 196 self.dirs[m.group(1)] = True
191 197
192 198 def mtnisfile(self, name, rev):
193 199 # a non-file could be a directory or a deleted or renamed file
194 200 self.mtnloadmanifest(rev)
195 201 return name in self.files
196 202
197 203 def mtnisdir(self, name, rev):
198 204 self.mtnloadmanifest(rev)
199 205 return name in self.dirs
200 206
201 207 def mtngetcerts(self, rev):
202 208 certs = {"author":"<missing>", "date":"<missing>",
203 209 "changelog":"<missing>", "branch":"<missing>"}
204 210 certlist = self.mtnrun("certs", rev)
205 211 # mtn < 0.45:
206 212 # key "test@selenic.com"
207 213 # mtn >= 0.45:
208 214 # key [ff58a7ffb771907c4ff68995eada1c4da068d328]
209 215 certlist = re.split('\n\n key ["\[]', certlist)
210 216 for e in certlist:
211 217 m = self.cert_re.match(e)
212 218 if m:
213 219 name, value = m.groups()
214 220 value = value.replace(r'\"', '"')
215 221 value = value.replace(r'\\', '\\')
216 222 certs[name] = value
217 223 # Monotone may have subsecond dates: 2005-02-05T09:39:12.364306
218 224 # and all times are stored in UTC
219 225 certs["date"] = certs["date"].split('.')[0] + " UTC"
220 226 return certs
221 227
222 228 # implement the converter_source interface:
223 229
224 230 def getheads(self):
225 231 if not self.revs:
226 232 return self.mtnrun("leaves").splitlines()
227 233 else:
228 234 return self.revs
229 235
230 236 def getchanges(self, rev, full):
231 237 if full:
232 238 raise error.Abort(_("convert from monotone does not support "
233 239 "--full"))
234 240 revision = self.mtnrun("get_revision", rev).split("\n\n")
235 241 files = {}
236 242 ignoremove = {}
237 243 renameddirs = []
238 244 copies = {}
239 245 for e in revision:
240 246 m = self.add_file_re.match(e)
241 247 if m:
242 248 files[m.group(1)] = rev
243 249 ignoremove[m.group(1)] = rev
244 250 m = self.patch_re.match(e)
245 251 if m:
246 252 files[m.group(1)] = rev
247 253 # Delete/rename is handled later when the convert engine
248 254 # discovers an IOError exception from getfile,
249 255 # but only if we add the "from" file to the list of changes.
250 256 m = self.delete_re.match(e)
251 257 if m:
252 258 files[m.group(1)] = rev
253 259 m = self.rename_re.match(e)
254 260 if m:
255 261 toname = m.group(2)
256 262 fromname = m.group(1)
257 263 if self.mtnisfile(toname, rev):
258 264 ignoremove[toname] = 1
259 265 copies[toname] = fromname
260 266 files[toname] = rev
261 267 files[fromname] = rev
262 268 elif self.mtnisdir(toname, rev):
263 269 renameddirs.append((fromname, toname))
264 270
265 271 # Directory renames can be handled only once we have recorded
266 272 # all new files
267 273 for fromdir, todir in renameddirs:
268 274 renamed = {}
269 275 for tofile in self.files:
270 276 if tofile in ignoremove:
271 277 continue
272 278 if tofile.startswith(todir + '/'):
273 279 renamed[tofile] = fromdir + tofile[len(todir):]
274 280 # Avoid chained moves like:
275 281 # d1(/a) => d3/d1(/a)
276 282 # d2 => d3
277 283 ignoremove[tofile] = 1
278 284 for tofile, fromfile in renamed.items():
279 285 self.ui.debug (_("copying file in renamed directory "
280 286 "from '%s' to '%s'")
281 287 % (fromfile, tofile), '\n')
282 288 files[tofile] = rev
283 289 copies[tofile] = fromfile
284 290 for fromfile in renamed.values():
285 291 files[fromfile] = rev
286 292
287 293 return (files.items(), copies, set())
288 294
289 295 def getfile(self, name, rev):
290 296 if not self.mtnisfile(name, rev):
291 297 return None, None
292 298 try:
293 299 data = self.mtnrun("get_file_of", name, r=rev)
294 300 except Exception:
295 301 return None, None
296 302 self.mtnloadmanifest(rev)
297 303 node, attr = self.files.get(name, (None, ""))
298 304 return data, attr
299 305
300 306 def getcommit(self, rev):
301 307 extra = {}
302 308 certs = self.mtngetcerts(rev)
303 309 if certs.get('suspend') == certs["branch"]:
304 310 extra['close'] = 1
305 return commit(
311 return common.commit(
306 312 author=certs["author"],
307 313 date=util.datestr(util.strdate(certs["date"], "%Y-%m-%dT%H:%M:%S")),
308 314 desc=certs["changelog"],
309 315 rev=rev,
310 316 parents=self.mtnrun("parents", rev).splitlines(),
311 317 branch=certs["branch"],
312 318 extra=extra)
313 319
314 320 def gettags(self):
315 321 tags = {}
316 322 for e in self.mtnrun("tags").split("\n\n"):
317 323 m = self.tag_re.match(e)
318 324 if m:
319 325 tags[m.group(1)] = m.group(2)
320 326 return tags
321 327
322 328 def getchangedfiles(self, rev, i):
323 329 # This function is only needed to support --filemap
324 330 # ... and we don't support that
325 331 raise NotImplementedError
326 332
327 333 def before(self):
328 334 # Check if we have a new enough version to use automate stdio
329 335 version = 0.0
330 336 try:
331 337 versionstr = self.mtnrunsingle("interface_version")
332 338 version = float(versionstr)
333 339 except Exception:
334 340 raise error.Abort(_("unable to determine mtn automate interface "
335 341 "version"))
336 342
337 343 if version >= 12.0:
338 344 self.automatestdio = True
339 345 self.ui.debug("mtn automate version %s - using automate stdio\n" %
340 346 version)
341 347
342 348 # launch the long-running automate stdio process
343 349 self.mtnwritefp, self.mtnreadfp = self._run2('automate', 'stdio',
344 350 '-d', self.path)
345 351 # read the headers
346 352 read = self.mtnreadfp.readline()
347 353 if read != 'format-version: 2\n':
348 354 raise error.Abort(_('mtn automate stdio header unexpected: %s')
349 355 % read)
350 356 while read != '\n':
351 357 read = self.mtnreadfp.readline()
352 358 if not read:
353 359 raise error.Abort(_("failed to reach end of mtn automate "
354 360 "stdio headers"))
355 361 else:
356 362 self.ui.debug("mtn automate version %s - not using automate stdio "
357 363 "(automate >= 12.0 - mtn >= 0.46 is needed)\n" % version)
358 364
359 365 def after(self):
360 366 if self.automatestdio:
361 367 self.mtnwritefp.close()
362 368 self.mtnwritefp = None
363 369 self.mtnreadfp.close()
364 370 self.mtnreadfp = None
365 371
@@ -1,151 +1,150 b''
1 1 #require test-repo
2 2
3 3 $ cd "$TESTDIR"/..
4 4
5 5 $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs python contrib/check-py3-compat.py
6 6 contrib/check-code.py not using absolute_import
7 7 contrib/check-code.py requires print_function
8 8 contrib/debugshell.py not using absolute_import
9 9 contrib/hgfixes/fix_bytes.py not using absolute_import
10 10 contrib/hgfixes/fix_bytesmod.py not using absolute_import
11 11 contrib/hgfixes/fix_leftover_imports.py not using absolute_import
12 12 contrib/import-checker.py not using absolute_import
13 13 contrib/import-checker.py requires print_function
14 14 contrib/memory.py not using absolute_import
15 15 contrib/perf.py not using absolute_import
16 16 contrib/python-hook-examples.py not using absolute_import
17 17 contrib/revsetbenchmarks.py not using absolute_import
18 18 contrib/revsetbenchmarks.py requires print_function
19 19 contrib/showstack.py not using absolute_import
20 20 contrib/synthrepo.py not using absolute_import
21 21 contrib/win32/hgwebdir_wsgi.py not using absolute_import
22 22 doc/check-seclevel.py not using absolute_import
23 23 doc/gendoc.py not using absolute_import
24 24 doc/hgmanpage.py not using absolute_import
25 25 hgext/__init__.py not using absolute_import
26 26 hgext/color.py not using absolute_import
27 27 hgext/convert/__init__.py not using absolute_import
28 28 hgext/convert/bzr.py not using absolute_import
29 29 hgext/convert/common.py not using absolute_import
30 30 hgext/convert/convcmd.py not using absolute_import
31 31 hgext/convert/cvs.py not using absolute_import
32 hgext/convert/monotone.py not using absolute_import
33 32 hgext/convert/subversion.py not using absolute_import
34 33 hgext/convert/transport.py not using absolute_import
35 34 hgext/eol.py not using absolute_import
36 35 hgext/extdiff.py not using absolute_import
37 36 hgext/factotum.py not using absolute_import
38 37 hgext/fetch.py not using absolute_import
39 38 hgext/gpg.py not using absolute_import
40 39 hgext/graphlog.py not using absolute_import
41 40 hgext/hgcia.py not using absolute_import
42 41 hgext/hgk.py not using absolute_import
43 42 hgext/highlight/__init__.py not using absolute_import
44 43 hgext/highlight/highlight.py not using absolute_import
45 44 hgext/histedit.py not using absolute_import
46 45 hgext/largefiles/__init__.py not using absolute_import
47 46 hgext/largefiles/basestore.py not using absolute_import
48 47 hgext/largefiles/lfcommands.py not using absolute_import
49 48 hgext/largefiles/lfutil.py not using absolute_import
50 49 hgext/largefiles/localstore.py not using absolute_import
51 50 hgext/largefiles/overrides.py not using absolute_import
52 51 hgext/largefiles/proto.py not using absolute_import
53 52 hgext/largefiles/remotestore.py not using absolute_import
54 53 hgext/largefiles/reposetup.py not using absolute_import
55 54 hgext/largefiles/uisetup.py not using absolute_import
56 55 hgext/largefiles/wirestore.py not using absolute_import
57 56 hgext/mq.py not using absolute_import
58 57 hgext/notify.py not using absolute_import
59 58 hgext/patchbomb.py not using absolute_import
60 59 hgext/purge.py not using absolute_import
61 60 hgext/rebase.py not using absolute_import
62 61 hgext/record.py not using absolute_import
63 62 hgext/relink.py not using absolute_import
64 63 hgext/schemes.py not using absolute_import
65 64 hgext/share.py not using absolute_import
66 65 hgext/shelve.py not using absolute_import
67 66 hgext/strip.py not using absolute_import
68 67 hgext/transplant.py not using absolute_import
69 68 hgext/win32mbcs.py not using absolute_import
70 69 hgext/win32text.py not using absolute_import
71 70 i18n/check-translation.py not using absolute_import
72 71 i18n/polib.py not using absolute_import
73 72 setup.py not using absolute_import
74 73 tests/filterpyflakes.py requires print_function
75 74 tests/generate-working-copy-states.py requires print_function
76 75 tests/get-with-headers.py requires print_function
77 76 tests/heredoctest.py requires print_function
78 77 tests/hypothesishelpers.py not using absolute_import
79 78 tests/hypothesishelpers.py requires print_function
80 79 tests/killdaemons.py not using absolute_import
81 80 tests/md5sum.py not using absolute_import
82 81 tests/mockblackbox.py not using absolute_import
83 82 tests/printenv.py not using absolute_import
84 83 tests/readlink.py not using absolute_import
85 84 tests/readlink.py requires print_function
86 85 tests/revlog-formatv0.py not using absolute_import
87 86 tests/run-tests.py not using absolute_import
88 87 tests/seq.py not using absolute_import
89 88 tests/seq.py requires print_function
90 89 tests/silenttestrunner.py not using absolute_import
91 90 tests/silenttestrunner.py requires print_function
92 91 tests/sitecustomize.py not using absolute_import
93 92 tests/svn-safe-append.py not using absolute_import
94 93 tests/svnxml.py not using absolute_import
95 94 tests/test-ancestor.py requires print_function
96 95 tests/test-atomictempfile.py not using absolute_import
97 96 tests/test-batching.py not using absolute_import
98 97 tests/test-batching.py requires print_function
99 98 tests/test-bdiff.py not using absolute_import
100 99 tests/test-bdiff.py requires print_function
101 100 tests/test-context.py not using absolute_import
102 101 tests/test-context.py requires print_function
103 102 tests/test-demandimport.py not using absolute_import
104 103 tests/test-demandimport.py requires print_function
105 104 tests/test-dispatch.py not using absolute_import
106 105 tests/test-dispatch.py requires print_function
107 106 tests/test-doctest.py not using absolute_import
108 107 tests/test-duplicateoptions.py not using absolute_import
109 108 tests/test-duplicateoptions.py requires print_function
110 109 tests/test-filecache.py not using absolute_import
111 110 tests/test-filecache.py requires print_function
112 111 tests/test-filelog.py not using absolute_import
113 112 tests/test-filelog.py requires print_function
114 113 tests/test-hg-parseurl.py not using absolute_import
115 114 tests/test-hg-parseurl.py requires print_function
116 115 tests/test-hgweb-auth.py not using absolute_import
117 116 tests/test-hgweb-auth.py requires print_function
118 117 tests/test-hgwebdir-paths.py not using absolute_import
119 118 tests/test-hybridencode.py not using absolute_import
120 119 tests/test-hybridencode.py requires print_function
121 120 tests/test-lrucachedict.py not using absolute_import
122 121 tests/test-lrucachedict.py requires print_function
123 122 tests/test-manifest.py not using absolute_import
124 123 tests/test-minirst.py not using absolute_import
125 124 tests/test-minirst.py requires print_function
126 125 tests/test-parseindex2.py not using absolute_import
127 126 tests/test-parseindex2.py requires print_function
128 127 tests/test-pathencode.py not using absolute_import
129 128 tests/test-pathencode.py requires print_function
130 129 tests/test-propertycache.py not using absolute_import
131 130 tests/test-propertycache.py requires print_function
132 131 tests/test-revlog-ancestry.py not using absolute_import
133 132 tests/test-revlog-ancestry.py requires print_function
134 133 tests/test-run-tests.py not using absolute_import
135 134 tests/test-simplemerge.py not using absolute_import
136 135 tests/test-status-inprocess.py not using absolute_import
137 136 tests/test-status-inprocess.py requires print_function
138 137 tests/test-symlink-os-yes-fs-no.py not using absolute_import
139 138 tests/test-trusted.py not using absolute_import
140 139 tests/test-trusted.py requires print_function
141 140 tests/test-ui-color.py not using absolute_import
142 141 tests/test-ui-color.py requires print_function
143 142 tests/test-ui-config.py not using absolute_import
144 143 tests/test-ui-config.py requires print_function
145 144 tests/test-ui-verbosity.py not using absolute_import
146 145 tests/test-ui-verbosity.py requires print_function
147 146 tests/test-url.py not using absolute_import
148 147 tests/test-url.py requires print_function
149 148 tests/test-walkrepo.py requires print_function
150 149 tests/test-wireproto.py requires print_function
151 150 tests/tinyproxy.py requires print_function
General Comments 0
You need to be logged in to leave comments. Login now