From 4c5ee48f47271d9808b6f0c6dc4803599aa812ae Mon Sep 17 00:00:00 2001 From: Chirayu Desai Date: Mon, 13 May 2013 13:48:43 +0530 Subject: [PATCH] Python3 support * Mostly involves changing print to be a function, and adding "from __future__ import print_function" for copatibility. Change-Id: I3129233726e0116a348753a2e2bb68806a08668c Signed-off-by: Chirayu Desai --- Documentation/licenses.py | 30 ++++++++++++++++-------------- contrib/check-valid-commit.py | 27 +++++++++++++++------------ contrib/trivial_rebase.py | 18 ++++++++++-------- lib/gwt/compiler.py | 14 ++++++++------ tools/download_jar.py | 10 ++++++---- tools/eclipse/gen_project.py | 6 ++++-- 6 files changed, 59 insertions(+), 46 deletions(-) diff --git a/Documentation/licenses.py b/Documentation/licenses.py index b05f0f898a..4e05e4159a 100755 --- a/Documentation/licenses.py +++ b/Documentation/licenses.py @@ -15,6 +15,8 @@ # # TODO(sop): Be more detailed: version, link to Maven Central +from __future__ import print_function + from collections import defaultdict, deque import re from shutil import copyfileobj @@ -51,9 +53,9 @@ while queue: continue licenses[dep].add(target) queue.extend(graph[target]) -used = sorted(licenses.iterkeys()) +used = sorted(licenses.keys()) -print """\ +print("""\ Gerrit Code Review - Licenses ============================= @@ -100,30 +102,30 @@ to be installed by the end-user. Licenses -------- -""" +""") for n in used: libs = sorted(licenses[n]) name = n[len('//lib:LICENSE-'):] - print - print '[[%s]]' % name - print name - print '~' * len(name) - print + print() + print('[[%s]]' % name) + print(name) + print('~' * len(name)) + print() for d in libs: if d.startswith('//lib:') or d.startswith('//lib/'): p = d[len('//lib:'):] else: p = d[d.index(':')+1:].lower() - print '* ' + p - print - print '----' + print('* ' + p) + print() + print('----') with open(n[2:].replace(':', '/')) as fd: copyfileobj(fd, stdout) - print '----' + print('----') -print """ +print(""" GERRIT ------ Part of link:index.html[Gerrit Code Review] -""" +""") diff --git a/contrib/check-valid-commit.py b/contrib/check-valid-commit.py index ca1785ecdb..150b310782 100755 --- a/contrib/check-valid-commit.py +++ b/contrib/check-valid-commit.py @@ -1,5 +1,8 @@ #!/usr/bin/env python -import commands + +from __future__ import print_function + +import subprocess import getopt import sys @@ -24,8 +27,8 @@ def main(): try: opts, args = getopt.getopt(sys.argv[1:], '', \ ['change=', 'project=', 'branch=', 'commit=', 'patchset=']) - except getopt.GetoptError, err: - print 'Error: %s' % (err) + except getopt.GetoptError as err: + print('Error: %s' % (err)) usage() sys.exit(-1) @@ -41,7 +44,7 @@ def main(): elif arg == '--patchset': patchset = value else: - print 'Error: option %s not recognized' % (arg) + print('Error: option %s not recognized' % (arg)) usage() sys.exit(-1) @@ -51,11 +54,11 @@ def main(): sys.exit(-1) command = 'git cat-file commit %s' % (commit) - status, output = commands.getstatusoutput(command) + status, output = subprocess.getstatusoutput(command) if status != 0: - print 'Error running \'%s\'. status: %s, output:\n\n%s' % \ - (command, status, output) + print('Error running \'%s\'. status: %s, output:\n\n%s' % \ + (command, status, output)) sys.exit(-1) commitMessage = output[(output.find('\n\n')+2):] @@ -74,21 +77,21 @@ def main(): passes(commit) def usage(): - print 'Usage:\n' - print sys.argv[0] + ' --change --project ' \ - + '--branch --commit --patchset ' + print('Usage:\n') + print(sys.argv[0] + ' --change --project ' \ + + '--branch --commit --patchset ') def fail( commit, message ): command = SSH_COMMAND + FAILURE_SCORE + ' -m \\\"' \ + _shell_escape( FAILURE_MESSAGE + '\n\n' + message) \ + '\\\" ' + commit - commands.getstatusoutput(command) + subprocess.getstatusoutput(command) sys.exit(1) def passes( commit ): command = SSH_COMMAND + PASS_SCORE + ' -m \\\"' \ + _shell_escape(PASS_MESSAGE) + ' \\\" ' + commit - commands.getstatusoutput(command) + subprocess.getstatusoutput(command) def _shell_escape(x): s = '' diff --git a/contrib/trivial_rebase.py b/contrib/trivial_rebase.py index 7764470430..e16c57d9fc 100755 --- a/contrib/trivial_rebase.py +++ b/contrib/trivial_rebase.py @@ -36,6 +36,8 @@ Documentation is available here: https://www.codeaurora.org/xwiki/bin/QAEP/Gerri """ +from __future__ import print_function + import argparse import json import re @@ -95,7 +97,7 @@ class TrivialRebase: try: process = subprocess.Popen(command, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out, std_err = process.communicate() - except OSError, e: + except OSError as e: raise self.CheckCallError(command, cwd, e.errno, None) if process.returncode: raise self.CheckCallError(command, cwd, process.returncode, std_out, std_err) @@ -107,9 +109,9 @@ class TrivialRebase: '--format', 'JSON', '-c', sql_query] try: (gsql_out, _gsql_stderr) = self.CheckCall(gsql_cmd) - except self.CheckCallError, e: - print "return code is %s" % e.retcode - print "stdout and stderr is\n%s%s" % (e.stdout, e.stderr) + except self.CheckCallError as e: + print("return code is %s" % e.retcode) + print("stdout and stderr is\n%s%s" % (e.stdout, e.stderr)) raise new_out = gsql_out.replace('}}\n', '}}\nsplit here\n') @@ -194,7 +196,7 @@ class TrivialRebase: prev_patch_id = self.GetPatchId(prev_revision) cur_patch_id = self.GetPatchId(self.commit) if prev_patch_id == '0' and cur_patch_id == '0': - print "commits %s and %s are both empty or merge commits" % (prev_revision, self.commit) + print("commits %s and %s are both empty or merge commits" % (prev_revision, self.commit)) return if cur_patch_id != prev_patch_id: # patch-ids don't match @@ -237,7 +239,7 @@ class TrivialRebase: gerrit_review_msg = ("\'Automatically re-added by Gerrit trivial rebase " "detection script.\'") - for acct, flags in self.acct_approvals.items(): + for acct, flags in list(self.acct_approvals.items()): gerrit_review_cmd = ['gerrit', 'review', '--project', self.project, '--message', gerrit_review_msg, flags, self.commit] email_addr = self.GetEmailFromAcctId(acct) @@ -246,5 +248,5 @@ class TrivialRebase: if __name__ == "__main__": try: TrivialRebase().Run() - except AssertionError, e: - print >> sys.stderr, e + except AssertionError as e: + print(e, file=sys.stderr) diff --git a/lib/gwt/compiler.py b/lib/gwt/compiler.py index 4fa3b11c6c..245f896502 100755 --- a/lib/gwt/compiler.py +++ b/lib/gwt/compiler.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function + from multiprocessing import cpu_count from os import environ, fchmod, makedirs, mkdir, path from subprocess import Popen, PIPE @@ -31,7 +33,7 @@ for a in argv[3:]: opt.append(a) if not outzip.endswith('.zip'): - print >>stderr, "%s must end with .zip" % outzip + print("%s must end with .zip" % outzip, file=stderr) exit(1) rebuild = outzip[:-4] + '.rebuild' @@ -56,13 +58,13 @@ cmd = [ gwt = Popen(cmd, stdout = PIPE, stderr = PIPE) out, err = gwt.communicate() if gwt.returncode != 0: - print >>stderr, out + err + print(out + err, file=stderr) exit(gwt.returncode) with open(rebuild, 'w') as fd: def shquote(s): return s.replace("'", "'\\''") - print >>fd, '#!/bin/sh' - print >>fd, "PATH='%s'" % shquote(environ['PATH']) - print >>fd, 'buck build "$1" || exit' - fchmod(fd.fileno(), 0755) + print('#!/bin/sh', file=fd) + print("PATH='%s'" % shquote(environ['PATH']), file=fd) + print('buck build "$1" || exit', file=fd) + fchmod(fd.fileno(), 0o755) diff --git a/tools/download_jar.py b/tools/download_jar.py index 14ebfa0cfd..6a4ed7e563 100755 --- a/tools/download_jar.py +++ b/tools/download_jar.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function + from hashlib import sha1 from optparse import OptionParser from os import link, makedirs, path @@ -64,18 +66,18 @@ if not path.exists(cache_ent): safe_mkdirs(path.dirname(cache_ent)) check_call(['curl', '-sfo', cache_ent, args.u]) except (OSError, CalledProcessError) as err: - print >>stderr, "error using curl: %s" % str(err) + print("error using curl: %s" % str(err), file=stderr) exit(1) if args.v: have = hashfile(cache_ent) if args.v != have: o = cache_ent[len(root_dir) + 1:] - print >>stderr, ( + print(( '%s:\n' + 'expected %s\n' + 'received %s\n' + - ' %s\n') % (args.u, args.v, have, o) + ' %s\n') % (args.u, args.v, have, o), file=stderr) exit(1) exclude = [] @@ -91,7 +93,7 @@ if args.exclude_java_sources: finally: zf.close() except (BadZipfile, LargeZipFile) as err: - print >>stderr, "error opening %s: %s" % (cache_ent, str(err)) + print("error opening %s: %s" % (cache_ent, str(err)), file=stderr) exit(1) safe_mkdirs(path.dirname(args.o)) diff --git a/tools/eclipse/gen_project.py b/tools/eclipse/gen_project.py index eb575cb5fd..53593c0f01 100755 --- a/tools/eclipse/gen_project.py +++ b/tools/eclipse/gen_project.py @@ -15,6 +15,8 @@ # # TODO(sop): Remove hack after Buck supports Eclipse +from __future__ import print_function + from os import path, symlink from sys import argv @@ -25,7 +27,7 @@ for _ in range(0, 3): p = path.join(ROOT, '.project') with open(p, 'w') as fd: - print >>fd, """\ + print("""\ gerrit @@ -38,5 +40,5 @@ with open(p, 'w') as fd: org.eclipse.jdt.core.javanature \ -""" +""", file=fd) symlink(p, OUT)