git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] make hg-to-git compatible with python2.x and 3.x
@ 2018-02-15 11:18 Hervé Beraud
  2018-02-15 22:06 ` Junio C Hamano
  2018-02-20 16:52 ` Hervé Beraud
  0 siblings, 2 replies; 11+ messages in thread
From: Hervé Beraud @ 2018-02-15 11:18 UTC (permalink / raw)
  To: git

---
 contrib/hg-to-git/hg-to-git.py | 140 ++++++++++++++++++++++++-----------------
 1 file changed, 83 insertions(+), 57 deletions(-)

diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index de3f81667ed97..9b0842c3883dc 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -18,14 +18,19 @@
     along with this program; if not, see <http://www.gnu.org/licenses/>.
 """
 
-import os, os.path, sys
-import tempfile, pickle, getopt
+import os
+import os.path
+import sys
+import tempfile
+import pickle
+import getopt
+import textwrap
 import re
 
 if sys.hexversion < 0x02030000:
-   # The behavior of the pickle module changed significantly in 2.3
-   sys.stderr.write("hg-to-git.py: requires Python 2.3 or later.\n")
-   sys.exit(1)
+    # The behavior of the pickle module changed significantly in 2.3
+    sys.stderr.write("hg-to-git.py: requires Python 2.3 or later.\n")
+    sys.exit(1)
 
 # Maps hg version -> git version
 hgvers = {}
@@ -38,29 +43,31 @@
 # Number of new changesets converted from hg
 hgnewcsets = 0
 
-#------------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
+
 
 def usage():
 
-        print """\
-%s: [OPTIONS] <hgprj>
+    print(textwrap.dedent("""\
+        {0}: [OPTIONS] <hgprj>
 
-options:
-    -s, --gitstate=FILE: name of the state to be saved/read
-                         for incrementals
-    -n, --nrepack=INT:   number of changesets that will trigger
-                         a repack (default=0, -1 to deactivate)
-    -v, --verbose:       be verbose
+        options:
+            -s, --gitstate=FILE: name of the state to be saved/read
+                                 for incrementals
+            -n, --nrepack=INT:   number of changesets that will trigger
+                                 a repack (default=0, -1 to deactivate)
+            -v, --verbose:       be verbose
 
-required:
-    hgprj:  name of the HG project to import (directory)
-""" % sys.argv[0]
+        required:
+            hgprj:  name of the HG project to import (directory)
+    """).format(sys.argv[0]))
+
+# -----------------------------------------------------------------------------
 
-#------------------------------------------------------------------------------
 
 def getgitenv(user, date):
     env = ''
-    elems = re.compile('(.*?)\s+<(.*)>').match(user)
+    elems = re.compile(r'(.*?)\s+<(.*)>').match(user)
     if elems:
         env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
         env += 'export GIT_COMMITTER_NAME="%s" ;' % elems.group(1)
@@ -76,14 +83,16 @@ def getgitenv(user, date):
     env += 'export GIT_COMMITTER_DATE="%s" ;' % date
     return env
 
-#------------------------------------------------------------------------------
+# -----------------------------------------------------------------------------
+
 
 state = ''
 opt_nrepack = 0
 verbose = False
 
 try:
-    opts, args = getopt.getopt(sys.argv[1:], 's:t:n:v', ['gitstate=', 'tempdir=', 'nrepack=', 'verbose'])
+    options = ['gitstate=', 'tempdir=', 'nrepack=', 'verbose']
+    opts, args = getopt.getopt(sys.argv[1:], 's:t:n:v', options)
     for o, a in opts:
         if o in ('-s', '--gitstate'):
             state = a
@@ -94,7 +103,7 @@ def getgitenv(user, date):
             verbose = True
     if len(args) != 1:
         raise Exception('params')
-except:
+except Exception:
     usage()
     sys.exit(1)
 
@@ -104,37 +113,38 @@ 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 = os.popen(
+        'hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
     prnts = map(lambda x: x[:x.find(':')], prnts)
     if prnts[0] != '':
         parent = prnts[0].strip()
     else:
         parent = str(cset - 1)
-    hgchildren[parent] += ( str(cset), )
+    hgchildren[parent] += (str(cset), )
     if len(prnts) > 1:
         mparent = prnts[1].strip()
-        hgchildren[mparent] += ( str(cset), )
+        hgchildren[mparent] += (str(cset), )
     else:
         mparent = None
 
@@ -148,59 +158,65 @@ def getgitenv(user, date):
             hgbranch[str(cset)] = hgbranch[parent]
     else:
         # Normal changesets
-        # For first children, take the parent branch, for the others create a new branch
+        # For first children, take the parent branch,
+        # for the others create a new branch
         if hgchildren[parent][0] == str(cset):
             hgbranch[str(cset)] = hgbranch[parent]
         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
 
     # get info
-    log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
+    log_data = os.popen(
+        'hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset
+    ).readlines()
     tag = log_data[0].strip()
     date = log_data[1].strip()
     user = log_data[2].strip()
     parent = hgparents[str(cset)][0]
     mparent = hgparents[str(cset)][1]
 
-    #get comment
+    # get comment
     (fdcomment, filecomment) = tempfile.mkstemp()
-    csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
+    csetcomment = os.popen(
+        'hg log -r %d --template "{desc}"' % cset).read().strip()
     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)]
-            os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
+            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,11 +225,18 @@ def getgitenv(user, date):
             otherbranch = hgbranch[mparent]
         else:
             otherbranch = hgbranch[parent]
-        print 'merging', otherbranch, 'into', hgbranch[str(cset)]
-        os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
+        print('merging', otherbranch, 'into', hgbranch[str(cset)])
+        mrg = 'git merge --no-commit -s ours "" %s %s' % (
+            hgbranch[str(cset)], otherbranch)
+        os.system(getgitenv(user, date) + mrg)
 
     # remove everything except .git and .hg directories
-    os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
+    xargs_rm = 'xargs rm -rf'
+    ignore_path = r'\( -path "./.hg" -o -path "./.git" \)'
+    rm_all = r'find . {0} -prune -o ! -name "." -print | {1}'.format(
+        ignore_path,
+        xargs_rm)
+    os.system(rm_all)
 
     # repopulate with checkouted files
     os.system('hg update -C %d' % cset)
@@ -221,10 +244,13 @@ def getgitenv(user, date):
     # add new files
     os.system('git ls-files -x .hg --others | git update-index --add --stdin')
     # delete removed files
-    os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
+    rm_f = 'git ls-files -x .hg --deleted | git update-index --remove --stdin'
+    os.system(rm_f)
 
     # commit
-    os.system(getgitenv(user, date) + 'git commit --allow-empty --allow-empty-message -a -F %s' % filecomment)
+    commit = 'git commit --allow-empty --allow-empty-message -a -F {}'.format(
+        filecomment)
+    os.system(getgitenv(user, date) + commit)
     os.unlink(filecomment)
 
     # tag
@@ -233,12 +259,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 +273,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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] make hg-to-git compatible with python2.x and 3.x
  2018-02-15 11:18 [PATCH] make hg-to-git compatible with python2.x and 3.x Hervé Beraud
@ 2018-02-15 22:06 ` Junio C Hamano
  2018-02-20 16:52 ` Hervé Beraud
  1 sibling, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2018-02-15 22:06 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

Hervé Beraud <herveberaud.pro@gmail.com> writes:

> ---

Thanks for posting, but this is way under-justified, even for
something in contrib/ area.  

Are all changes in this patch necessary to "make it compatible with
both Python 2 and 3", or are some parts do not contribute directly
to the objective but are good changes to suit your personal taste,
match BCP styles, etc.?  I am guessing it is the latter (e.g. with
Python 3, you cannot use print without invoking it as a function,
but os.system() can still accept a literal command line string, so
introducing variable "rm_f" that gets assigned a literal string only
once to pass it to os.system() does not have anything to do with the
Python2to3 transition), and if that is the case, it makes sense to
split this into at least 2 patches, i.e. prelimiary clean-up,
followed by changes required to run it with Python3.  It also is OK
to make it a 3-patch series, in which case you would limit the
preliminary clean-up to minimum uncontroversial changes and follow
up with an optional update to suit personal taste at the very end.

Also, the submission needs to be signed-off for us to be able to
use.  Please see Documentation/SubmittingPatches for details.

Thanks.

>  contrib/hg-to-git/hg-to-git.py | 140 ++++++++++++++++++++++++-----------------
>  1 file changed, 83 insertions(+), 57 deletions(-)
>
> diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
> ...
>      os.system('git ls-files -x .hg --others | git update-index --add --stdin')
>      # delete removed files
> -    os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
> +    rm_f = 'git ls-files -x .hg --deleted | git update-index --remove --stdin'
> +    os.system(rm_f)
>  ... 
> @@ -247,7 +273,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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] make hg-to-git compatible with python2.x and 3.x
  2018-02-15 11:18 [PATCH] make hg-to-git compatible with python2.x and 3.x Hervé Beraud
  2018-02-15 22:06 ` Junio C Hamano
@ 2018-02-20 16:52 ` Hervé Beraud
  2018-02-20 19:14   ` Junio C Hamano
  2019-09-17 13:41   ` [PATCH] Rewrite feature to render " Hervé Beraud
  1 sibling, 2 replies; 11+ messages in thread
From: Hervé Beraud @ 2018-02-20 16:52 UTC (permalink / raw)
  To: git

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(-)

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)
 

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] make hg-to-git compatible with python2.x and 3.x
  2018-02-20 16:52 ` Hervé Beraud
@ 2018-02-20 19:14   ` Junio C Hamano
  2018-02-20 19:19     ` Junio C Hamano
  2019-09-17 13:41   ` [PATCH] Rewrite feature to render " Hervé Beraud
  1 sibling, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2018-02-20 19:14 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

Hervé Beraud <herveberaud.pro@gmail.com> writes:

> 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(-)

I think you shrunk the scope of the change, but I feel that it is
still a bit under-explained.  Let me try to write a proposed commit
log message and ask you to see if I understood the idea behind the
changes correctly:

    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.

Hopefully this would also serve as an illustration of the kind of
things we want in our log message.

I am not sure about the change from map(lambda ...) to list
comprehension, though.  Not that I have a preference for or against
(I am not a Python person), but I do not know if this is a change
necessary to run with Python 3 or if it is merely more preferred to
use list comprehension.

Thanks.

> 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)
>  
>
> --
> https://github.com/git/git/pull/458

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] make hg-to-git compatible with python2.x and 3.x
  2018-02-20 19:14   ` Junio C Hamano
@ 2018-02-20 19:19     ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2018-02-20 19:19 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

>     - map(lambda ..., collection) is not liked; use list comprehension.
> ...
> I am not sure about the change from map(lambda ...) to list
> comprehension, though.  Not that I have a preference for or against
> (I am not a Python person), but I do not know if this is a change
> necessary to run with Python 3 or if it is merely more preferred to
> use list comprehension.
> ...
>> -    prnts = map(lambda x: x[:x.find(':')], prnts)
>> +    prnts = [x[:x.find(':')] for x in prnts]

Well, I should have dug this myself a bit more [*1*] before hitting
[SEND].  This change is required because map() no longer returns a
list.  So the bullet item in the proposed commit log message in my
earlier message should be updated to say that instead--it was unclear
to me so I phrased as if it was a mere preference, but it actually
is a required change.


[Footnote]

*1* Well, it would have been ideal if the original patch had enough
    information from the beginning to make this kind of "digging"
    unnecessary ;-)





^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] Rewrite feature to render hg-to-git compatible with python2.x and 3.x
  2018-02-20 16:52 ` Hervé Beraud
  2018-02-20 19:14   ` Junio C Hamano
@ 2019-09-17 13:41   ` Hervé Beraud
  2019-09-17 22:35     ` Junio C Hamano
  2019-09-18  8:33     ` [PATCH v2] hg-to-git: make it compatible with both python3 and python2 Hervé Beraud
  1 sibling, 2 replies; 11+ messages in thread
From: Hervé Beraud @ 2019-09-17 13:41 UTC (permalink / raw)
  To: git

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(-)

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)
 

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] Rewrite feature to render hg-to-git compatible with python2.x and 3.x
  2019-09-17 13:41   ` [PATCH] Rewrite feature to render " Hervé Beraud
@ 2019-09-17 22:35     ` Junio C Hamano
  2019-09-17 22:40       ` Junio C Hamano
  2019-09-18  8:33     ` [PATCH v2] hg-to-git: make it compatible with both python3 and python2 Hervé Beraud
  1 sibling, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2019-09-17 22:35 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Rewrite feature to render hg-to-git compatible with python2.x and 3.x
  2019-09-17 22:35     ` Junio C Hamano
@ 2019-09-17 22:40       ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2019-09-17 22:40 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] hg-to-git: make it compatible with both python3 and python2
  2019-09-17 13:41   ` [PATCH] Rewrite feature to render " Hervé Beraud
  2019-09-17 22:35     ` Junio C Hamano
@ 2019-09-18  8:33     ` Hervé Beraud
  2019-09-18 17:21       ` Junio C Hamano
  2019-09-18 17:55       ` [PATCH v3] " Hervé Beraud
  1 sibling, 2 replies; 11+ messages in thread
From: Hervé Beraud @ 2019-09-18  8:33 UTC (permalink / raw)
  To: git

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(-)

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] hg-to-git: make it compatible with both python3 and python2
  2019-09-18  8:33     ` [PATCH v2] hg-to-git: make it compatible with both python3 and python2 Hervé Beraud
@ 2019-09-18 17:21       ` Junio C Hamano
  2019-09-18 17:55       ` [PATCH v3] " Hervé Beraud
  1 sibling, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2019-09-18 17:21 UTC (permalink / raw)
  To: Hervé Beraud; +Cc: git

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] hg-to-git: make it compatible with both python3 and python2
  2019-09-18  8:33     ` [PATCH v2] hg-to-git: make it compatible with both python3 and python2 Hervé Beraud
  2019-09-18 17:21       ` Junio C Hamano
@ 2019-09-18 17:55       ` Hervé Beraud
  1 sibling, 0 replies; 11+ messages in thread
From: Hervé Beraud @ 2019-09-18 17:55 UTC (permalink / raw)
  To: git

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

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

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

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-09-18 17:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-15 11:18 [PATCH] make hg-to-git compatible with python2.x and 3.x Hervé Beraud
2018-02-15 22:06 ` Junio C Hamano
2018-02-20 16:52 ` Hervé Beraud
2018-02-20 19:14   ` Junio C Hamano
2018-02-20 19:19     ` Junio C Hamano
2019-09-17 13:41   ` [PATCH] Rewrite feature to render " Hervé Beraud
2019-09-17 22:35     ` Junio C Hamano
2019-09-17 22:40       ` Junio C Hamano
2019-09-18  8:33     ` [PATCH v2] hg-to-git: make it compatible with both python3 and python2 Hervé Beraud
2019-09-18 17:21       ` Junio C Hamano
2019-09-18 17:55       ` [PATCH v3] " Hervé Beraud

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).