Rewrite feature to render hg-to-git compatible with python2.x and 3.x
diff mbox series

Message ID 0102016d3f74d202-d5b32dd4-0098-4ad0-8ac7-5fde254f7796-000000@eu-west-1.amazonses.com
State New
Headers show
Series
  • Rewrite feature to render hg-to-git compatible with python2.x and 3.x
Related show

Commit Message

Hervé Beraud Sept. 17, 2019, 1:41 p.m. UTC
Rewrite features that are no longer supported (or recommended)
in Python 3 in the script so that it can be used with both
Python 2 and 3, namely:

- print is not a statement; use print() function instead.
- dict.has_key(key) is no more; use "key in dict" instead.
- map(lambda ..., collection) is not liked; use list comprehension.

These changes introduce a syntaxe compatible with the both versions of
python.

Python 2 is EOL at the end of 2019, the major part of distros
and systems now come with python 3  is the default version so
these address the situation to render this script compatible with
the latest versions of python.

Signed-off-by: Hervé Beraud <herveberaud.pro@gmail.com>
---
 contrib/hg-to-git/hg-to-git.py | 52 +++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 26 deletions(-)


--
https://github.com/git/git/pull/458

Comments

Junio C Hamano Sept. 17, 2019, 10:35 p.m. UTC | #1
Hervé Beraud <herveberaud.pro@gmail.com> writes:

> Rewrite features that are no longer supported (or recommended)
> in Python 3 in the script so that it can be used with both
> Python 2 and 3, namely:
>
> - print is not a statement; use print() function instead.
> - dict.has_key(key) is no more; use "key in dict" instead.
> - map(lambda ..., collection) is not liked; use list comprehension.

The first two are must haves (iow, without them the code would not
work with Py3), but the last one sounds like a mere preference.  Is
there some authoritative source we can quote (e.g. "PEPx says that
we should prefer X over Y")?

> These changes introduce a syntaxe compatible with the both versions of
> python.
>
> Python 2 is EOL at the end of 2019, the major part of distros
> and systems now come with python 3  is the default version so
> these address the situation to render this script compatible with
> the latest versions of python.

The explanation looks vaguely familiar.  I would have placed the
background first, and then description of what is done next, perhaps
like:

    Python 2 is EOL at the end of 2019, many distros and systems now
    come with python 3 as their default version.

    Rewrite features that are no longer supported (or recommended)
    in Python 3 in the script so that it can be used with both
    Python 2 and 3, namely:

    - print is not a statement; use print() function instead.
    - dict.has_key(key) is no more; use "key in dict" instead.
    - map(lambda ..., collection) is not liked; use list comprehension.

to make it even easier to read without unnecessary repetition, but
what you wrote is clear enough already.

The patch text looked clearly done.  Nice.
Junio C Hamano Sept. 17, 2019, 10:40 p.m. UTC | #2
Junio C Hamano <gitster@pobox.com> writes:

One more thing.

> Subject: Re: [PATCH] Rewrite feature to render hg-to-git compatible with python2.x and 3.x

Our commit titles typically begin with <area>: prefix, e.g.

    Subject: hg-to-git: make it compatibile with both Python 3 and Python 2

or something like that.

Patch
diff mbox series

diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index de3f81667ed97..8fa7698df5c20 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -42,7 +42,7 @@ 
 
 def usage():
 
-        print """\
+        print("""\
 %s: [OPTIONS] <hgprj>
 
 options:
@@ -54,7 +54,7 @@  def usage():
 
 required:
     hgprj:  name of the HG project to import (directory)
-""" % sys.argv[0]
+""" % sys.argv[0])
 
 #------------------------------------------------------------------------------
 
@@ -104,29 +104,29 @@  def getgitenv(user, date):
 if state:
     if os.path.exists(state):
         if verbose:
-            print 'State does exist, reading'
+            print('State does exist, reading')
         f = open(state, 'r')
         hgvers = pickle.load(f)
     else:
-        print 'State does not exist, first run'
+        print('State does not exist, first run')
 
 sock = os.popen('hg tip --template "{rev}"')
 tip = sock.read()
 if sock.close():
     sys.exit(1)
 if verbose:
-    print 'tip is', tip
+    print('tip is', tip)
 
 # Calculate the branches
 if verbose:
-    print 'analysing the branches...'
+    print('analysing the branches...')
 hgchildren["0"] = ()
 hgparents["0"] = (None, None)
 hgbranch["0"] = "master"
 for cset in range(1, int(tip) + 1):
     hgchildren[str(cset)] = ()
     prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
-    prnts = map(lambda x: x[:x.find(':')], prnts)
+    prnts = [x[:x.find(':')] for x in prnts]
     if prnts[0] != '':
         parent = prnts[0].strip()
     else:
@@ -154,15 +154,15 @@  def getgitenv(user, date):
         else:
             hgbranch[str(cset)] = "branch-" + str(cset)
 
-if not hgvers.has_key("0"):
-    print 'creating repository'
+if "0" not in hgvers:
+    print('creating repository')
     os.system('git init')
 
 # loop through every hg changeset
 for cset in range(int(tip) + 1):
 
     # incremental, already seen
-    if hgvers.has_key(str(cset)):
+    if str(cset) in hgvers:
         continue
     hgnewcsets += 1
 
@@ -180,27 +180,27 @@  def getgitenv(user, date):
     os.write(fdcomment, csetcomment)
     os.close(fdcomment)
 
-    print '-----------------------------------------'
-    print 'cset:', cset
-    print 'branch:', hgbranch[str(cset)]
-    print 'user:', user
-    print 'date:', date
-    print 'comment:', csetcomment
+    print('-----------------------------------------')
+    print('cset:', cset)
+    print('branch:', hgbranch[str(cset)])
+    print('user:', user)
+    print('date:', date)
+    print('comment:', csetcomment)
     if parent:
-	print 'parent:', parent
+	print('parent:', parent)
     if mparent:
-        print 'mparent:', mparent
+        print('mparent:', mparent)
     if tag:
-        print 'tag:', tag
-    print '-----------------------------------------'
+        print('tag:', tag)
+    print('-----------------------------------------')
 
     # checkout the parent if necessary
     if cset != 0:
         if hgbranch[str(cset)] == "branch-" + str(cset):
-            print 'creating new branch', hgbranch[str(cset)]
+            print('creating new branch', hgbranch[str(cset)])
             os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
         else:
-            print 'checking out branch', hgbranch[str(cset)]
+            print('checking out branch', hgbranch[str(cset)])
             os.system('git checkout %s' % hgbranch[str(cset)])
 
     # merge
@@ -209,7 +209,7 @@  def getgitenv(user, date):
             otherbranch = hgbranch[mparent]
         else:
             otherbranch = hgbranch[parent]
-        print 'merging', otherbranch, 'into', hgbranch[str(cset)]
+        print('merging', otherbranch, 'into', hgbranch[str(cset)])
         os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
 
     # remove everything except .git and .hg directories
@@ -233,12 +233,12 @@  def getgitenv(user, date):
 
     # delete branch if not used anymore...
     if mparent and len(hgchildren[str(cset)]):
-        print "Deleting unused branch:", otherbranch
+        print("Deleting unused branch:", otherbranch)
         os.system('git branch -d %s' % otherbranch)
 
     # retrieve and record the version
     vvv = os.popen('git show --quiet --pretty=format:%H').read()
-    print 'record', cset, '->', vvv
+    print('record', cset, '->', vvv)
     hgvers[str(cset)] = vvv
 
 if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
@@ -247,7 +247,7 @@  def getgitenv(user, date):
 # write the state for incrementals
 if state:
     if verbose:
-        print 'Writing state'
+        print('Writing state')
     f = open(state, 'w')
     pickle.dump(hgvers, f)