comparison hgext/convert/monotone.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents d26bfbf419f9
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
17 ) 17 )
18 from mercurial.utils import dateutil 18 from mercurial.utils import dateutil
19 19
20 from . import common 20 from . import common
21 21
22
22 class monotone_source(common.converter_source, common.commandline): 23 class monotone_source(common.converter_source, common.commandline):
23 def __init__(self, ui, repotype, path=None, revs=None): 24 def __init__(self, ui, repotype, path=None, revs=None):
24 common.converter_source.__init__(self, ui, repotype, path, revs) 25 common.converter_source.__init__(self, ui, repotype, path, revs)
25 if revs and len(revs) > 1: 26 if revs and len(revs) > 1:
26 raise error.Abort(_('monotone source does not support specifying ' 27 raise error.Abort(
27 'multiple revs')) 28 _(
29 'monotone source does not support specifying '
30 'multiple revs'
31 )
32 )
28 common.commandline.__init__(self, ui, 'mtn') 33 common.commandline.__init__(self, ui, 'mtn')
29 34
30 self.ui = ui 35 self.ui = ui
31 self.path = path 36 self.path = path
32 self.automatestdio = False 37 self.automatestdio = False
33 self.revs = revs 38 self.revs = revs
34 39
35 norepo = common.NoRepo(_("%s does not look like a monotone repository") 40 norepo = common.NoRepo(
36 % path) 41 _("%s does not look like a monotone repository") % path
42 )
37 if not os.path.exists(os.path.join(path, '_MTN')): 43 if not os.path.exists(os.path.join(path, '_MTN')):
38 # Could be a monotone repository (SQLite db file) 44 # Could be a monotone repository (SQLite db file)
39 try: 45 try:
40 f = open(path, 'rb') 46 f = open(path, 'rb')
41 header = f.read(16) 47 header = f.read(16)
44 header = '' 50 header = ''
45 if header != 'SQLite format 3\x00': 51 if header != 'SQLite format 3\x00':
46 raise norepo 52 raise norepo
47 53
48 # regular expressions for parsing monotone output 54 # regular expressions for parsing monotone output
49 space = br'\s*' 55 space = br'\s*'
50 name = br'\s+"((?:\\"|[^"])*)"\s*' 56 name = br'\s+"((?:\\"|[^"])*)"\s*'
51 value = name 57 value = name
52 revision = br'\s+\[(\w+)\]\s*' 58 revision = br'\s+\[(\w+)\]\s*'
53 lines = br'(?:.|\n)+' 59 lines = br'(?:.|\n)+'
54 60
55 self.dir_re = re.compile(space + "dir" + name) 61 self.dir_re = re.compile(space + "dir" + name)
56 self.file_re = re.compile(space + "file" + name + 62 self.file_re = re.compile(space + "file" + name + "content" + revision)
57 "content" + revision) 63 self.add_file_re = re.compile(
58 self.add_file_re = re.compile(space + "add_file" + name + 64 space + "add_file" + name + "content" + revision
59 "content" + revision) 65 )
60 self.patch_re = re.compile(space + "patch" + name + 66 self.patch_re = re.compile(
61 "from" + revision + "to" + revision) 67 space + "patch" + name + "from" + revision + "to" + revision
62 self.rename_re = re.compile(space + "rename" + name + "to" + name) 68 )
63 self.delete_re = re.compile(space + "delete" + name) 69 self.rename_re = re.compile(space + "rename" + name + "to" + name)
64 self.tag_re = re.compile(space + "tag" + name + "revision" + 70 self.delete_re = re.compile(space + "delete" + name)
65 revision) 71 self.tag_re = re.compile(space + "tag" + name + "revision" + revision)
66 self.cert_re = re.compile(lines + space + "name" + name + 72 self.cert_re = re.compile(
67 "value" + value) 73 lines + space + "name" + name + "value" + value
74 )
68 75
69 attr = space + "file" + lines + space + "attr" + space 76 attr = space + "file" + lines + space + "attr" + space
70 self.attr_execute_re = re.compile(attr + '"mtn:execute"' + 77 self.attr_execute_re = re.compile(
71 space + '"true"') 78 attr + '"mtn:execute"' + space + '"true"'
79 )
72 80
73 # cached data 81 # cached data
74 self.manifest_rev = None 82 self.manifest_rev = None
75 self.manifest = None 83 self.manifest = None
76 self.files = None 84 self.files = None
77 self.dirs = None 85 self.dirs = None
78 86
79 common.checktool('mtn', abort=False) 87 common.checktool('mtn', abort=False)
80 88
81 def mtnrun(self, *args, **kwargs): 89 def mtnrun(self, *args, **kwargs):
82 if self.automatestdio: 90 if self.automatestdio:
138 raise error.Abort(_('bad mtn packet - no end of packet size')) 146 raise error.Abort(_('bad mtn packet - no end of packet size'))
139 lengthstr += read 147 lengthstr += read
140 try: 148 try:
141 length = pycompat.long(lengthstr[:-1]) 149 length = pycompat.long(lengthstr[:-1])
142 except TypeError: 150 except TypeError:
143 raise error.Abort(_('bad mtn packet - bad packet size %s') 151 raise error.Abort(
144 % lengthstr) 152 _('bad mtn packet - bad packet size %s') % lengthstr
153 )
145 154
146 read = self.mtnreadfp.read(length) 155 read = self.mtnreadfp.read(length)
147 if len(read) != length: 156 if len(read) != length:
148 raise error.Abort(_("bad mtn packet - unable to read full packet " 157 raise error.Abort(
149 "read %s of %s") % (len(read), length)) 158 _(
159 "bad mtn packet - unable to read full packet "
160 "read %s of %s"
161 )
162 % (len(read), length)
163 )
150 164
151 return (commandnbr, stream, length, read) 165 return (commandnbr, stream, length, read)
152 166
153 def mtnstdioreadcommandoutput(self, command): 167 def mtnstdioreadcommandoutput(self, command):
154 retval = [] 168 retval = []
155 while True: 169 while True:
156 commandnbr, stream, length, output = self.mtnstdioreadpacket() 170 commandnbr, stream, length, output = self.mtnstdioreadpacket()
157 self.ui.debug('mtn: read packet %s:%s:%d\n' % 171 self.ui.debug(
158 (commandnbr, stream, length)) 172 'mtn: read packet %s:%s:%d\n' % (commandnbr, stream, length)
173 )
159 174
160 if stream == 'l': 175 if stream == 'l':
161 # End of command 176 # End of command
162 if output != '0': 177 if output != '0':
163 raise error.Abort(_("mtn command '%s' returned %s") % 178 raise error.Abort(
164 (command, output)) 179 _("mtn command '%s' returned %s") % (command, output)
180 )
165 break 181 break
166 elif stream in 'ew': 182 elif stream in 'ew':
167 # Error, warning output 183 # Error, warning output
168 self.ui.warn(_('%s error:\n') % self.command) 184 self.ui.warn(_('%s error:\n') % self.command)
169 self.ui.warn(output) 185 self.ui.warn(output)
205 def mtnisdir(self, name, rev): 221 def mtnisdir(self, name, rev):
206 self.mtnloadmanifest(rev) 222 self.mtnloadmanifest(rev)
207 return name in self.dirs 223 return name in self.dirs
208 224
209 def mtngetcerts(self, rev): 225 def mtngetcerts(self, rev):
210 certs = {"author":"<missing>", "date":"<missing>", 226 certs = {
211 "changelog":"<missing>", "branch":"<missing>"} 227 "author": "<missing>",
228 "date": "<missing>",
229 "changelog": "<missing>",
230 "branch": "<missing>",
231 }
212 certlist = self.mtnrun("certs", rev) 232 certlist = self.mtnrun("certs", rev)
213 # mtn < 0.45: 233 # mtn < 0.45:
214 # key "test@selenic.com" 234 # key "test@selenic.com"
215 # mtn >= 0.45: 235 # mtn >= 0.45:
216 # key [ff58a7ffb771907c4ff68995eada1c4da068d328] 236 # key [ff58a7ffb771907c4ff68995eada1c4da068d328]
235 else: 255 else:
236 return self.revs 256 return self.revs
237 257
238 def getchanges(self, rev, full): 258 def getchanges(self, rev, full):
239 if full: 259 if full:
240 raise error.Abort(_("convert from monotone does not support " 260 raise error.Abort(
241 "--full")) 261 _("convert from monotone does not support " "--full")
262 )
242 revision = self.mtnrun("get_revision", rev).split("\n\n") 263 revision = self.mtnrun("get_revision", rev).split("\n\n")
243 files = {} 264 files = {}
244 ignoremove = {} 265 ignoremove = {}
245 renameddirs = [] 266 renameddirs = []
246 copies = {} 267 copies = {}
276 renamed = {} 297 renamed = {}
277 for tofile in self.files: 298 for tofile in self.files:
278 if tofile in ignoremove: 299 if tofile in ignoremove:
279 continue 300 continue
280 if tofile.startswith(todir + '/'): 301 if tofile.startswith(todir + '/'):
281 renamed[tofile] = fromdir + tofile[len(todir):] 302 renamed[tofile] = fromdir + tofile[len(todir) :]
282 # Avoid chained moves like: 303 # Avoid chained moves like:
283 # d1(/a) => d3/d1(/a) 304 # d1(/a) => d3/d1(/a)
284 # d2 => d3 305 # d2 => d3
285 ignoremove[tofile] = 1 306 ignoremove[tofile] = 1
286 for tofile, fromfile in renamed.items(): 307 for tofile, fromfile in renamed.items():
287 self.ui.debug( 308 self.ui.debug(
288 "copying file in renamed directory from '%s' to '%s'" 309 "copying file in renamed directory from '%s' to '%s'"
289 % (fromfile, tofile), '\n') 310 % (fromfile, tofile),
311 '\n',
312 )
290 files[tofile] = rev 313 files[tofile] = rev
291 copies[tofile] = fromfile 314 copies[tofile] = fromfile
292 for fromfile in renamed.values(): 315 for fromfile in renamed.values():
293 files[fromfile] = rev 316 files[fromfile] = rev
294 317
316 date=dateutil.datestr(dateutil.strdate(certs["date"], dateformat)), 339 date=dateutil.datestr(dateutil.strdate(certs["date"], dateformat)),
317 desc=certs["changelog"], 340 desc=certs["changelog"],
318 rev=rev, 341 rev=rev,
319 parents=self.mtnrun("parents", rev).splitlines(), 342 parents=self.mtnrun("parents", rev).splitlines(),
320 branch=certs["branch"], 343 branch=certs["branch"],
321 extra=extra) 344 extra=extra,
345 )
322 346
323 def gettags(self): 347 def gettags(self):
324 tags = {} 348 tags = {}
325 for e in self.mtnrun("tags").split("\n\n"): 349 for e in self.mtnrun("tags").split("\n\n"):
326 m = self.tag_re.match(e) 350 m = self.tag_re.match(e)
337 # Check if we have a new enough version to use automate stdio 361 # Check if we have a new enough version to use automate stdio
338 try: 362 try:
339 versionstr = self.mtnrunsingle("interface_version") 363 versionstr = self.mtnrunsingle("interface_version")
340 version = float(versionstr) 364 version = float(versionstr)
341 except Exception: 365 except Exception:
342 raise error.Abort(_("unable to determine mtn automate interface " 366 raise error.Abort(
343 "version")) 367 _("unable to determine mtn automate interface " "version")
368 )
344 369
345 if version >= 12.0: 370 if version >= 12.0:
346 self.automatestdio = True 371 self.automatestdio = True
347 self.ui.debug("mtn automate version %f - using automate stdio\n" % 372 self.ui.debug(
348 version) 373 "mtn automate version %f - using automate stdio\n" % version
374 )
349 375
350 # launch the long-running automate stdio process 376 # launch the long-running automate stdio process
351 self.mtnwritefp, self.mtnreadfp = self._run2('automate', 'stdio', 377 self.mtnwritefp, self.mtnreadfp = self._run2(
352 '-d', self.path) 378 'automate', 'stdio', '-d', self.path
379 )
353 # read the headers 380 # read the headers
354 read = self.mtnreadfp.readline() 381 read = self.mtnreadfp.readline()
355 if read != 'format-version: 2\n': 382 if read != 'format-version: 2\n':
356 raise error.Abort(_('mtn automate stdio header unexpected: %s') 383 raise error.Abort(
357 % read) 384 _('mtn automate stdio header unexpected: %s') % read
385 )
358 while read != '\n': 386 while read != '\n':
359 read = self.mtnreadfp.readline() 387 read = self.mtnreadfp.readline()
360 if not read: 388 if not read:
361 raise error.Abort(_("failed to reach end of mtn automate " 389 raise error.Abort(
362 "stdio headers")) 390 _(
391 "failed to reach end of mtn automate "
392 "stdio headers"
393 )
394 )
363 else: 395 else:
364 self.ui.debug("mtn automate version %s - not using automate stdio " 396 self.ui.debug(
365 "(automate >= 12.0 - mtn >= 0.46 is needed)\n" % version) 397 "mtn automate version %s - not using automate stdio "
398 "(automate >= 12.0 - mtn >= 0.46 is needed)\n" % version
399 )
366 400
367 def after(self): 401 def after(self):
368 if self.automatestdio: 402 if self.automatestdio:
369 self.mtnwritefp.close() 403 self.mtnwritefp.close()
370 self.mtnwritefp = None 404 self.mtnwritefp = None