[v2] hg-to-git: make it compatible with both python3 and python2
diff mbox series

Message ID 0102016d43812db5-722fe301-48f7-4b46-93e4-67dc2992629a-000000@eu-west-1.amazonses.com
State New
Headers show
Series
  • [v2] hg-to-git: make it compatible with both python3 and python2
Related show

Commit Message

Hervé Beraud Sept. 18, 2019, 8:33 a.m. UTC
Python 2 is EOL at the end of 2019, many distros
and systems now come with python 3 is the default version.

These changes introduce a syntaxe compatible with the both versions of
python and so with the nearly future python standard.

Introduced changes:
-------------------

Rewriting features that are no longer supported (or recommended)
in Python 3 in the hg-to-git script.py 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.

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


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

Comments

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

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

Grammo.  s/is the/as their/ or something like that to fix.

> These changes introduce a syntaxe compatible with the both versions of
> python and so with the nearly future python standard.
>
> Introduced changes:
> -------------------

Let's drop the above 5 lines.  A canonical form of log message for
us is to begin with a brief background to make readers realize what
is lacking in the current system (if needed -- and you've done a
good job to make the readers realize that we need to make sure we
work with Py3).  With readers' minds prepared to accept the need for
a change, then you give orders to the codebase to "be like so" in
imperative mood.  E.g.

    	Rewrite features used in hg-to-git that are no longer
    	supported in Python 3, in such a way that an updated code
    	can still be usable with Python 2.

	- print is not ...
	- dict.has_key(key) is no more; ...

You seem to have dropped the change from map() to list comprehention
in this iteration.  I am not into Python deeply enough to care
either way myself, and the original form with map() seems to work
with Python3 (evaluating it seems to result in a map object, instead
of a list, to lazily yield the values, but we are not printing the
result of map() directly with print(), so it should be OK) so from
my point of view, the fewer things we have to defend/justify, the
better ;-)

Will queue.  Thanks.

> Rewriting features that are no longer supported (or recommended)
> in Python 3 in the hg-to-git script.py 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.
>
> Signed-off-by: Hervé Beraud <herveberaud.pro@gmail.com>
> ---
>  contrib/hg-to-git/hg-to-git.py | 50 +++++++++++++++++-----------------
>  1 file changed, 25 insertions(+), 25 deletions(-)
>
> diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
> index de3f81667ed97..bb2822d4a5e17 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,22 +104,22 @@ 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"
> @@ -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)
>  
>
> --
> https://github.com/git/git/pull/458

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..bb2822d4a5e17 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,22 +104,22 @@  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"
@@ -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)