From a28e76b7b3df25949c3be091715db05c7a08c2b3 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 6 Jul 2020 15:24:30 -0700 Subject: [PATCH] Allow "hg arc-ls-markers" to run under Python 2 or Python 3 Summary: Ref T13546. See PHI1805. Currently, the "arc-ls-markers" extension doesn't run under Python 3: - some stuff needs "b'...'" to mark it as a byte string; - "dict.iteritems()" is gone in Python 3, and "mercurial.pycompat" isn't always available; - in Python 3, "json" refuses to print byte strings; and - the compiler caching behavior in Python 3 has changed. Try to get these things working in the same way under Python 2 and Python 3. Test Plan: Ran this command (with `python` as Python 2, locally): ``` $ python /usr/local/bin/hg --config 'extensions.arc-hg=/Users/epriestley/dev/core/lib/arcanist/support/hg/arc-hg.py' arc-ls-markers -- ``` ...and this command: ``` $ python3 /usr/local/bin/hg --config 'extensions.arc-hg=/Users/epriestley/dev/core/lib/arcanist/support/hg/arc-hg.py' arc-ls-markers -- ``` ..and saw the same output in both cases (previously, `python3 ...` fataled in various ways). Maniphest Tasks: T13546 Differential Revision: https://secure.phabricator.com/D21392 --- .gitignore | 1 + support/hg/arc-hg.py | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d40646a4..0e13f861 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ # Python extension compiled files. /support/hg/arc-hg.pyc +/support/hg/__pycache__/ diff --git a/support/hg/arc-hg.py b/support/hg/arc-hg.py index a11cef73..94e4d3b1 100644 --- a/support/hg/arc-hg.py +++ b/support/hg/arc-hg.py @@ -1,4 +1,14 @@ from __future__ import absolute_import +import sys + +is_python_3 = sys.version_info[0] >= 3 + +if is_python_3: + def arc_items(dict): + return dict.items() +else: + def arc_items(dict): + return dict.iteritems() import os import json @@ -19,8 +29,8 @@ cmdtable = {} command = registrar.command(cmdtable) @command( - "arc-ls-markers", - [('', 'output', '', + b'arc-ls-markers', + [(b'', b'output', b'', _('file to output refs to'), _('FILE')), ] + cmdutil.remoteopts, _('[--output FILENAME] [SOURCE]')) @@ -42,6 +52,16 @@ def lsmarkers(ui, repo, source=None, **opts): else: markers = remotemarkers(ui, repo, source, opts) + for m in markers: + if m['name'] != None: + m['name'] = m['name'].decode('utf-8') + + if m['node'] != None: + m['node'] = m['node'].decode('utf-8') + + if m['description'] != None: + m['description'] = m['description'].decode('utf-8') + json_opts = { 'indent': 2, 'sort_keys': True, @@ -54,14 +74,15 @@ def lsmarkers(ui, repo, source=None, **opts): with open(output_file, 'w+') as f: json.dump(markers, f, **json_opts) else: - print json.dumps(markers, output_file, **json_opts) + json_data = json.dumps(markers, **json_opts) + print(json_data) return 0 def localmarkers(ui, repo): markers = [] - active_node = repo['.'].node() + active_node = repo[b'.'].node() all_heads = set(repo.heads()) current_name = repo.dirstate.branch() saw_current = False @@ -117,7 +138,7 @@ def localmarkers(ui, repo): bookmarks = repo._bookmarks active_bookmark = repo._activebookmark - for bookmark_name, bookmark_node in bookmarks.iteritems(): + for bookmark_name, bookmark_node in arc_items(bookmarks): is_active = (active_bookmark == bookmark_name) description = repo[bookmark_node].description()