Mercurial > hg
comparison hgext/convert/common.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 | d98ec36be808 |
children | 687b865b95ad |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
20 error, | 20 error, |
21 phases, | 21 phases, |
22 pycompat, | 22 pycompat, |
23 util, | 23 util, |
24 ) | 24 ) |
25 from mercurial.utils import ( | 25 from mercurial.utils import procutil |
26 procutil, | |
27 ) | |
28 | 26 |
29 pickle = util.pickle | 27 pickle = util.pickle |
30 propertycache = util.propertycache | 28 propertycache = util.propertycache |
29 | |
31 | 30 |
32 def _encodeornone(d): | 31 def _encodeornone(d): |
33 if d is None: | 32 if d is None: |
34 return | 33 return |
35 return d.encode('latin1') | 34 return d.encode('latin1') |
36 | 35 |
36 | |
37 class _shlexpy3proxy(object): | 37 class _shlexpy3proxy(object): |
38 | |
39 def __init__(self, l): | 38 def __init__(self, l): |
40 self._l = l | 39 self._l = l |
41 | 40 |
42 def __iter__(self): | 41 def __iter__(self): |
43 return (_encodeornone(v) for v in self._l) | 42 return (_encodeornone(v) for v in self._l) |
50 return self._l.infile or '<unknown>' | 49 return self._l.infile or '<unknown>' |
51 | 50 |
52 @property | 51 @property |
53 def lineno(self): | 52 def lineno(self): |
54 return self._l.lineno | 53 return self._l.lineno |
54 | |
55 | 55 |
56 def shlexer(data=None, filepath=None, wordchars=None, whitespace=None): | 56 def shlexer(data=None, filepath=None, wordchars=None, whitespace=None): |
57 if data is None: | 57 if data is None: |
58 if pycompat.ispy3: | 58 if pycompat.ispy3: |
59 data = open(filepath, 'r', encoding=r'latin1') | 59 data = open(filepath, 'r', encoding=r'latin1') |
60 else: | 60 else: |
61 data = open(filepath, 'r') | 61 data = open(filepath, 'r') |
62 else: | 62 else: |
63 if filepath is not None: | 63 if filepath is not None: |
64 raise error.ProgrammingError( | 64 raise error.ProgrammingError( |
65 'shlexer only accepts data or filepath, not both') | 65 'shlexer only accepts data or filepath, not both' |
66 ) | |
66 if pycompat.ispy3: | 67 if pycompat.ispy3: |
67 data = data.decode('latin1') | 68 data = data.decode('latin1') |
68 l = shlex.shlex(data, infile=filepath, posix=True) | 69 l = shlex.shlex(data, infile=filepath, posix=True) |
69 if whitespace is not None: | 70 if whitespace is not None: |
70 l.whitespace_split = True | 71 l.whitespace_split = True |
79 l.wordchars += wordchars | 80 l.wordchars += wordchars |
80 if pycompat.ispy3: | 81 if pycompat.ispy3: |
81 return _shlexpy3proxy(l) | 82 return _shlexpy3proxy(l) |
82 return l | 83 return l |
83 | 84 |
85 | |
84 def encodeargs(args): | 86 def encodeargs(args): |
85 def encodearg(s): | 87 def encodearg(s): |
86 lines = base64.encodestring(s) | 88 lines = base64.encodestring(s) |
87 lines = [l.splitlines()[0] for l in lines] | 89 lines = [l.splitlines()[0] for l in lines] |
88 return ''.join(lines) | 90 return ''.join(lines) |
89 | 91 |
90 s = pickle.dumps(args) | 92 s = pickle.dumps(args) |
91 return encodearg(s) | 93 return encodearg(s) |
92 | 94 |
95 | |
93 def decodeargs(s): | 96 def decodeargs(s): |
94 s = base64.decodestring(s) | 97 s = base64.decodestring(s) |
95 return pickle.loads(s) | 98 return pickle.loads(s) |
96 | 99 |
100 | |
97 class MissingTool(Exception): | 101 class MissingTool(Exception): |
98 pass | 102 pass |
103 | |
99 | 104 |
100 def checktool(exe, name=None, abort=True): | 105 def checktool(exe, name=None, abort=True): |
101 name = name or exe | 106 name = name or exe |
102 if not procutil.findexe(exe): | 107 if not procutil.findexe(exe): |
103 if abort: | 108 if abort: |
104 exc = error.Abort | 109 exc = error.Abort |
105 else: | 110 else: |
106 exc = MissingTool | 111 exc = MissingTool |
107 raise exc(_('cannot find required "%s" tool') % name) | 112 raise exc(_('cannot find required "%s" tool') % name) |
108 | 113 |
114 | |
109 class NoRepo(Exception): | 115 class NoRepo(Exception): |
110 pass | 116 pass |
111 | 117 |
118 | |
112 SKIPREV = 'SKIP' | 119 SKIPREV = 'SKIP' |
113 | 120 |
121 | |
114 class commit(object): | 122 class commit(object): |
115 def __init__(self, author, date, desc, parents, branch=None, rev=None, | 123 def __init__( |
116 extra=None, sortkey=None, saverev=True, phase=phases.draft, | 124 self, |
117 optparents=None, ctx=None): | 125 author, |
126 date, | |
127 desc, | |
128 parents, | |
129 branch=None, | |
130 rev=None, | |
131 extra=None, | |
132 sortkey=None, | |
133 saverev=True, | |
134 phase=phases.draft, | |
135 optparents=None, | |
136 ctx=None, | |
137 ): | |
118 self.author = author or 'unknown' | 138 self.author = author or 'unknown' |
119 self.date = date or '0 0' | 139 self.date = date or '0 0' |
120 self.desc = desc | 140 self.desc = desc |
121 self.parents = parents # will be converted and used as parents | 141 self.parents = parents # will be converted and used as parents |
122 self.optparents = optparents or [] # will be used if already converted | 142 self.optparents = optparents or [] # will be used if already converted |
123 self.branch = branch | 143 self.branch = branch |
124 self.rev = rev | 144 self.rev = rev |
125 self.extra = extra or {} | 145 self.extra = extra or {} |
126 self.sortkey = sortkey | 146 self.sortkey = sortkey |
127 self.saverev = saverev | 147 self.saverev = saverev |
128 self.phase = phase | 148 self.phase = phase |
129 self.ctx = ctx # for hg to hg conversions | 149 self.ctx = ctx # for hg to hg conversions |
150 | |
130 | 151 |
131 class converter_source(object): | 152 class converter_source(object): |
132 """Conversion source interface""" | 153 """Conversion source interface""" |
133 | 154 |
134 def __init__(self, ui, repotype, path=None, revs=None): | 155 def __init__(self, ui, repotype, path=None, revs=None): |
144 def checkhexformat(self, revstr, mapname='splicemap'): | 165 def checkhexformat(self, revstr, mapname='splicemap'): |
145 """ fails if revstr is not a 40 byte hex. mercurial and git both uses | 166 """ fails if revstr is not a 40 byte hex. mercurial and git both uses |
146 such format for their revision numbering | 167 such format for their revision numbering |
147 """ | 168 """ |
148 if not re.match(br'[0-9a-fA-F]{40,40}$', revstr): | 169 if not re.match(br'[0-9a-fA-F]{40,40}$', revstr): |
149 raise error.Abort(_('%s entry %s is not a valid revision' | 170 raise error.Abort( |
150 ' identifier') % (mapname, revstr)) | 171 _('%s entry %s is not a valid revision' ' identifier') |
172 % (mapname, revstr) | |
173 ) | |
151 | 174 |
152 def before(self): | 175 def before(self): |
153 pass | 176 pass |
154 | 177 |
155 def after(self): | 178 def after(self): |
221 return s.decode(pycompat.sysstr(encoding)).encode("utf-8") | 244 return s.decode(pycompat.sysstr(encoding)).encode("utf-8") |
222 except UnicodeError: | 245 except UnicodeError: |
223 try: | 246 try: |
224 return s.decode("latin-1").encode("utf-8") | 247 return s.decode("latin-1").encode("utf-8") |
225 except UnicodeError: | 248 except UnicodeError: |
226 return s.decode(pycompat.sysstr(encoding), | 249 return s.decode(pycompat.sysstr(encoding), "replace").encode( |
227 "replace").encode("utf-8") | 250 "utf-8" |
251 ) | |
228 | 252 |
229 def getchangedfiles(self, rev, i): | 253 def getchangedfiles(self, rev, i): |
230 """Return the files changed by rev compared to parent[i]. | 254 """Return the files changed by rev compared to parent[i]. |
231 | 255 |
232 i is an index selecting one of the parents of rev. The return | 256 i is an index selecting one of the parents of rev. The return |
272 """revstr is a string that describes a revision in the given | 296 """revstr is a string that describes a revision in the given |
273 source control system. Return true if revstr has correct | 297 source control system. Return true if revstr has correct |
274 format. | 298 format. |
275 """ | 299 """ |
276 return True | 300 return True |
301 | |
277 | 302 |
278 class converter_sink(object): | 303 class converter_sink(object): |
279 """Conversion sink (target) interface""" | 304 """Conversion sink (target) interface""" |
280 | 305 |
281 def __init__(self, ui, repotype, path): | 306 def __init__(self, ui, repotype, path): |
299 """Path to a file that will contain lines | 324 """Path to a file that will contain lines |
300 srcauthor=dstauthor | 325 srcauthor=dstauthor |
301 mapping equivalent authors identifiers for each system.""" | 326 mapping equivalent authors identifiers for each system.""" |
302 return None | 327 return None |
303 | 328 |
304 def putcommit(self, files, copies, parents, commit, source, revmap, full, | 329 def putcommit( |
305 cleanp2): | 330 self, files, copies, parents, commit, source, revmap, full, cleanp2 |
331 ): | |
306 """Create a revision with all changed files listed in 'files' | 332 """Create a revision with all changed files listed in 'files' |
307 and having listed parents. 'commit' is a commit object | 333 and having listed parents. 'commit' is a commit object |
308 containing at a minimum the author, date, and message for this | 334 containing at a minimum the author, date, and message for this |
309 changeset. 'files' is a list of (path, version) tuples, | 335 changeset. 'files' is a list of (path, version) tuples, |
310 'copies' is a dictionary mapping destinations to sources, | 336 'copies' is a dictionary mapping destinations to sources, |
366 def hascommitforsplicemap(self, rev): | 392 def hascommitforsplicemap(self, rev): |
367 """This method is for the special needs for splicemap handling and not | 393 """This method is for the special needs for splicemap handling and not |
368 for general use. Returns True if the sink contains rev, aborts on some | 394 for general use. Returns True if the sink contains rev, aborts on some |
369 special cases.""" | 395 special cases.""" |
370 raise NotImplementedError | 396 raise NotImplementedError |
397 | |
371 | 398 |
372 class commandline(object): | 399 class commandline(object): |
373 def __init__(self, ui, command): | 400 def __init__(self, ui, command): |
374 self.ui = ui | 401 self.ui = ui |
375 self.command = command | 402 self.command = command |
401 cmdline = ' '.join(cmdline) | 428 cmdline = ' '.join(cmdline) |
402 return cmdline | 429 return cmdline |
403 | 430 |
404 def _run(self, cmd, *args, **kwargs): | 431 def _run(self, cmd, *args, **kwargs): |
405 def popen(cmdline): | 432 def popen(cmdline): |
406 p = subprocess.Popen(procutil.tonativestr(cmdline), | 433 p = subprocess.Popen( |
407 shell=True, bufsize=-1, | 434 procutil.tonativestr(cmdline), |
408 close_fds=procutil.closefds, | 435 shell=True, |
409 stdout=subprocess.PIPE) | 436 bufsize=-1, |
437 close_fds=procutil.closefds, | |
438 stdout=subprocess.PIPE, | |
439 ) | |
410 return p | 440 return p |
441 | |
411 return self._dorun(popen, cmd, *args, **kwargs) | 442 return self._dorun(popen, cmd, *args, **kwargs) |
412 | 443 |
413 def _run2(self, cmd, *args, **kwargs): | 444 def _run2(self, cmd, *args, **kwargs): |
414 return self._dorun(procutil.popen2, cmd, *args, **kwargs) | 445 return self._dorun(procutil.popen2, cmd, *args, **kwargs) |
415 | 446 |
416 def _run3(self, cmd, *args, **kwargs): | 447 def _run3(self, cmd, *args, **kwargs): |
417 return self._dorun(procutil.popen3, cmd, *args, **kwargs) | 448 return self._dorun(procutil.popen3, cmd, *args, **kwargs) |
418 | 449 |
419 def _dorun(self, openfunc, cmd, *args, **kwargs): | 450 def _dorun(self, openfunc, cmd, *args, **kwargs): |
420 cmdline = self._cmdline(cmd, *args, **kwargs) | 451 cmdline = self._cmdline(cmd, *args, **kwargs) |
421 self.ui.debug('running: %s\n' % (cmdline,)) | 452 self.ui.debug('running: %s\n' % (cmdline,)) |
422 self.prerun() | 453 self.prerun() |
423 try: | 454 try: |
424 return openfunc(cmdline) | 455 return openfunc(cmdline) |
492 yield fl | 523 yield fl |
493 | 524 |
494 def xargs(self, arglist, cmd, *args, **kwargs): | 525 def xargs(self, arglist, cmd, *args, **kwargs): |
495 for l in self._limit_arglist(arglist, cmd, *args, **kwargs): | 526 for l in self._limit_arglist(arglist, cmd, *args, **kwargs): |
496 self.run0(cmd, *(list(args) + l), **kwargs) | 527 self.run0(cmd, *(list(args) + l), **kwargs) |
528 | |
497 | 529 |
498 class mapfile(dict): | 530 class mapfile(dict): |
499 def __init__(self, ui, path): | 531 def __init__(self, ui, path): |
500 super(mapfile, self).__init__() | 532 super(mapfile, self).__init__() |
501 self.ui = ui | 533 self.ui = ui |
521 try: | 553 try: |
522 key, value = line.rsplit(' ', 1) | 554 key, value = line.rsplit(' ', 1) |
523 except ValueError: | 555 except ValueError: |
524 raise error.Abort( | 556 raise error.Abort( |
525 _('syntax error in %s(%d): key/value pair expected') | 557 _('syntax error in %s(%d): key/value pair expected') |
526 % (self.path, i + 1)) | 558 % (self.path, i + 1) |
559 ) | |
527 if key not in self: | 560 if key not in self: |
528 self.order.append(key) | 561 self.order.append(key) |
529 super(mapfile, self).__setitem__(key, value) | 562 super(mapfile, self).__setitem__(key, value) |
530 fp.close() | 563 fp.close() |
531 | 564 |
533 if self.fp is None: | 566 if self.fp is None: |
534 try: | 567 try: |
535 self.fp = open(self.path, 'ab') | 568 self.fp = open(self.path, 'ab') |
536 except IOError as err: | 569 except IOError as err: |
537 raise error.Abort( | 570 raise error.Abort( |
538 _('could not open map file %r: %s') % | 571 _('could not open map file %r: %s') |
539 (self.path, encoding.strtolocal(err.strerror))) | 572 % (self.path, encoding.strtolocal(err.strerror)) |
573 ) | |
540 self.fp.write(util.tonativeeol('%s %s\n' % (key, value))) | 574 self.fp.write(util.tonativeeol('%s %s\n' % (key, value))) |
541 self.fp.flush() | 575 self.fp.flush() |
542 super(mapfile, self).__setitem__(key, value) | 576 super(mapfile, self).__setitem__(key, value) |
543 | 577 |
544 def close(self): | 578 def close(self): |
545 if self.fp: | 579 if self.fp: |
546 self.fp.close() | 580 self.fp.close() |
547 self.fp = None | 581 self.fp = None |
548 | 582 |
583 | |
549 def makedatetimestamp(t): | 584 def makedatetimestamp(t): |
550 """Like dateutil.makedate() but for time t instead of current time""" | 585 """Like dateutil.makedate() but for time t instead of current time""" |
551 delta = (datetime.datetime.utcfromtimestamp(t) - | 586 delta = datetime.datetime.utcfromtimestamp( |
552 datetime.datetime.fromtimestamp(t)) | 587 t |
588 ) - datetime.datetime.fromtimestamp(t) | |
553 tz = delta.days * 86400 + delta.seconds | 589 tz = delta.days * 86400 + delta.seconds |
554 return t, tz | 590 return t, tz |