threads / patch / 50857

patch, 3 partsMake hg-to-git contrib script Python 3 compatible

Subject: [PATCH 0/3] Make hg-to-git contrib script Python 3 compatible

## tl;dr

4 messages between Apr 1, 2019 and Apr 1, 2019. Diffs are folded; open one to read it.

replies: 3people: 1as markdown or json

Jelle van der Waa· Apr 1, 2019, 18:52 UTC · lore

This patch series makes hg-to-git Python 3 compatible and was tested that it still works with Python 2.7. I'm not sure which Python version in minimally supported for these Git contrib scripts.

Jelle van der Waa· Apr 1, 2019, 18:52 UTC · re: Jelle van der Waa · lore

[PATCH 1/3] contrib: hg-to-git: make print python 3 compatible

From: Jelle van der Waa <jelle@vdwaa.nl>

Python 3 made print a function, keep Python 2 compatibility with a future import of print_function.

Signed-off-by: Jelle van der Waa <jelle@vdwaa.nl>
---
 contrib/hg-to-git/hg-to-git.py | 47 +++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 23 deletions(-)
Show changes to contrib/hg-to-git/hg-to-git.py +24 −23
diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index de3f81667e..f898d6f23e 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -17,6 +17,7 @@
     You should have received a copy of the GNU General Public License
     along with this program; if not, see <http://www.gnu.org/licenses/>.
 """
+from __future__ import print_function
 
 import os, os.path, sys
 import tempfile, pickle, getopt
@@ -42,7 +43,7 @@
 
 def usage():
 
-        print """\
+        print("""\
 %s: [OPTIONS] <hgprj>
 
 options:
@@ -54,7 +55,7 @@ def usage():
 
 required:
     hgprj:  name of the HG project to import (directory)
-""" % sys.argv[0]
+""" % sys.argv[0])
 
 #------------------------------------------------------------------------------
 
@@ -104,22 +105,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"
@@ -155,7 +156,7 @@ def getgitenv(user, date):
             hgbranch[str(cset)] = "branch-" + str(cset)
 
 if not hgvers.has_key("0"):
-    print 'creating repository'
+    print('creating repository')
     os.system('git init')
 
 # loop through every hg changeset
@@ -180,27 +181,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 +210,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 +234,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 +248,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)
 
-- 
2.21.0
Jelle van der Waa· Apr 1, 2019, 18:52 UTC · re: Jelle van der Waa · lore

[PATCH 3/3] contrib: hg-to-git: convert data to bytes for os.write

From: Jelle van der Waa <jelle@vdwaa.nl>

In Python 3 os.write wants bytes instead of a string, decode the str in to bytes.

Signed-off-by: Jelle van der Waa <jelle@vdwaa.nl>
---
 contrib/hg-to-git/hg-to-git.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to contrib/hg-to-git/hg-to-git.py +1 −1
diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index d504624c63..26c7300011 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -178,7 +178,7 @@ def getgitenv(user, date):
     #get comment
     (fdcomment, filecomment) = tempfile.mkstemp()
     csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
-    os.write(fdcomment, csetcomment)
+    os.write(fdcomment, csetcomment.encode())
     os.close(fdcomment)
 
     print('-----------------------------------------')
-- 
2.21.0
Jelle van der Waa· Apr 1, 2019, 18:52 UTC · re: Jelle van der Waa · lore

[PATCH 2/3] contrib: hg-to-git: has_key is removed in Python 3

From: Jelle van der Waa <jelle@vdwaa.nl>

has_key was deprecated in Python 2 and finally removed in Python 3 in favor of 'foo' in bar.

Signed-off-by: Jelle van der Waa <jelle@vdwaa.nl>
---
 contrib/hg-to-git/hg-to-git.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to contrib/hg-to-git/hg-to-git.py +2 −2
diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index f898d6f23e..d504624c63 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -155,7 +155,7 @@ def getgitenv(user, date):
         else:
             hgbranch[str(cset)] = "branch-" + str(cset)
 
-if not hgvers.has_key("0"):
+if "0" not in hgvers:
     print('creating repository')
     os.system('git init')
 
@@ -163,7 +163,7 @@ def getgitenv(user, date):
 for cset in range(int(tip) + 1):
 
     # incremental, already seen
-    if hgvers.has_key(str(cset)):
+    if str(cset) in hgvers:
         continue
     hgnewcsets += 1
 
-- 
2.21.0

← back to recent threads