hg-to-git: make it compatible with both python3 and python2

Python 2 is EOL at the end of 2019, many distros and systems now come with python 3 as their default version. 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 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hervé Beraud committed Sep 18, 2019 at 08:33 UTC d17ae00c97d3edbe548a0aaa64ac37bfc9440809
1 file changed +25 -25
contrib/hg-to-git/hg-to-git.py
+25 -25
@@ -42,7 +42,7 @@ hgnewcsets = 0
42
43 def usage():
44
45 - print """\
45 + print("""\
46 %s: [OPTIONS] <hgprj>
47
48 options:
@@ -54,7 +54,7 @@ options:
54
55 required:
56 hgprj: name of the HG project to import (directory)
57 -""" % sys.argv[0]
57 +""" % sys.argv[0])
58
59 #------------------------------------------------------------------------------
60
@@ -104,22 +104,22 @@ os.chdir(hgprj)
104 if state:
105 if os.path.exists(state):
106 if verbose:
107 - print 'State does exist, reading'
107 + print('State does exist, reading')
108 f = open(state, 'r')
109 hgvers = pickle.load(f)
110 else:
111 - print 'State does not exist, first run'
111 + print('State does not exist, first run')
112
113 sock = os.popen('hg tip --template "{rev}"')
114 tip = sock.read()
115 if sock.close():
116 sys.exit(1)
117 if verbose:
118 - print 'tip is', tip
118 + print('tip is', tip)
119
120 # Calculate the branches
121 if verbose:
122 - print 'analysing the branches...'
122 + print('analysing the branches...')
123 hgchildren["0"] = ()
124 hgparents["0"] = (None, None)
125 hgbranch["0"] = "master"
@@ -154,15 +154,15 @@ for cset in range(1, int(tip) + 1):
154 else:
155 hgbranch[str(cset)] = "branch-" + str(cset)
156
157 -if not hgvers.has_key("0"):
158 - print 'creating repository'
157 +if "0" not in hgvers:
158 + print('creating repository')
159 os.system('git init')
160
161 # loop through every hg changeset
162 for cset in range(int(tip) + 1):
163
164 # incremental, already seen
165 - if hgvers.has_key(str(cset)):
165 + if str(cset) in hgvers:
166 continue
167 hgnewcsets += 1
168
@@ -180,27 +180,27 @@ for cset in range(int(tip) + 1):
180 os.write(fdcomment, csetcomment)
181 os.close(fdcomment)
182
183 - print '-----------------------------------------'
184 - print 'cset:', cset
185 - print 'branch:', hgbranch[str(cset)]
186 - print 'user:', user
187 - print 'date:', date
188 - print 'comment:', csetcomment
183 + print('-----------------------------------------')
184 + print('cset:', cset)
185 + print('branch:', hgbranch[str(cset)])
186 + print('user:', user)
187 + print('date:', date)
188 + print('comment:', csetcomment)
189 if parent:
190 - print 'parent:', parent
190 + print('parent:', parent)
191 if mparent:
192 - print 'mparent:', mparent
192 + print('mparent:', mparent)
193 if tag:
194 - print 'tag:', tag
195 - print '-----------------------------------------'
194 + print('tag:', tag)
195 + print('-----------------------------------------')
196
197 # checkout the parent if necessary
198 if cset != 0:
199 if hgbranch[str(cset)] == "branch-" + str(cset):
200 - print 'creating new branch', hgbranch[str(cset)]
200 + print('creating new branch', hgbranch[str(cset)])
201 os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
202 else:
203 - print 'checking out branch', hgbranch[str(cset)]
203 + print('checking out branch', hgbranch[str(cset)])
204 os.system('git checkout %s' % hgbranch[str(cset)])
205
206 # merge
@@ -209,7 +209,7 @@ for cset in range(int(tip) + 1):
209 otherbranch = hgbranch[mparent]
210 else:
211 otherbranch = hgbranch[parent]
212 - print 'merging', otherbranch, 'into', hgbranch[str(cset)]
212 + print('merging', otherbranch, 'into', hgbranch[str(cset)])
213 os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
214
215 # remove everything except .git and .hg directories
@@ -233,12 +233,12 @@ for cset in range(int(tip) + 1):
233
234 # delete branch if not used anymore...
235 if mparent and len(hgchildren[str(cset)]):
236 - print "Deleting unused branch:", otherbranch
236 + print("Deleting unused branch:", otherbranch)
237 os.system('git branch -d %s' % otherbranch)
238
239 # retrieve and record the version
240 vvv = os.popen('git show --quiet --pretty=format:%H').read()
241 - print 'record', cset, '->', vvv
241 + print('record', cset, '->', vvv)
242 hgvers[str(cset)] = vvv
243
244 if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
@@ -247,7 +247,7 @@ if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
247 # write the state for incrementals
248 if state:
249 if verbose:
250 - print 'Writing state'
250 + print('Writing state')
251 f = open(state, 'w')
252 pickle.dump(hgvers, f)
253