From a52db496a5a8ac3ffd8598bf8bbe7e120b63d36b Mon Sep 17 00:00:00 2001 From: David Ripton Date: Tue, 8 Oct 2013 11:13:45 -0400 Subject: [PATCH] Use print function rather than print statement This is for Python 3 compatibility, since Python 3 does not support the print statement, only the print function. (Python 2.6 and 2.7 support both, depending on whether a __future__ import is used.) Paths containing "xen" were left alone, because of comments in a couple of the files indicating that the XenServer code needed to be compatible with Python 2.4. Fixes bug 1226943 Change-Id: I23b804a4d99500b4acf81dd19645ab06dfcc9f1c --- .../openstack/compute/contrib/coverage_ext.py | 11 ++++--- .../openstack/compute/plugins/v3/coverage.py | 11 ++++--- nova/cmd/manage.py | 2 +- nova/wsgi.py | 2 +- tools/config/analyze_opts.py | 7 ++-- tools/db/schema_diff.py | 9 ++++-- tools/install_venv.py | 4 ++- tools/lintstack.py | 32 ++++++++++--------- tools/regression_tester.py | 16 ++++++---- 9 files changed, 54 insertions(+), 40 deletions(-) diff --git a/nova/api/openstack/compute/contrib/coverage_ext.py b/nova/api/openstack/compute/contrib/coverage_ext.py index a69ef9367352..d2aa25439c9a 100644 --- a/nova/api/openstack/compute/contrib/coverage_ext.py +++ b/nova/api/openstack/compute/contrib/coverage_ext.py @@ -102,6 +102,7 @@ class CoverageController(object): def _start_coverage_telnet(self, tn, service): data_file = os.path.join(self.data_path, '.nova-coverage.%s' % str(service)) + tn.write('from __future__ import print_function\n') tn.write('import sys\n') tn.write('from coverage import coverage\n') tn.write("coverInst = coverage(data_file='%s') " @@ -109,7 +110,7 @@ class CoverageController(object): "else coverInst\n" % data_file) tn.write('coverInst.skipModules = sys.modules.keys()\n') tn.write("coverInst.start()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) def _start_coverage(self, req, body): @@ -147,7 +148,7 @@ class CoverageController(object): def _stop_coverage_telnet(self, tn): tn.write("coverInst.stop()\n") tn.write("coverInst.save()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) def _check_coverage(self): @@ -170,14 +171,14 @@ class CoverageController(object): if xml: execute = str("coverInst.xml_report(outfile='%s')\n" % path) tn.write(execute) - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) else: execute = str("output = open('%s', 'w')\n" % path) tn.write(execute) tn.write("coverInst.report(file=output)\n") tn.write("output.close()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) tn.close() @@ -244,7 +245,7 @@ class CoverageController(object): def _reset_coverage_telnet(self, tn): tn.write("coverInst.erase()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) def _reset_coverage(self, req): diff --git a/nova/api/openstack/compute/plugins/v3/coverage.py b/nova/api/openstack/compute/plugins/v3/coverage.py index 43763ffab9e3..6c431111c147 100644 --- a/nova/api/openstack/compute/plugins/v3/coverage.py +++ b/nova/api/openstack/compute/plugins/v3/coverage.py @@ -117,6 +117,7 @@ class CoverageController(wsgi.Controller): def _start_coverage_telnet(self, tn, service): data_file = os.path.join(self.data_path, '.nova-coverage.%s' % str(service)) + tn.write('from __future__ import print_function\n') tn.write('import sys\n') tn.write('from coverage import coverage\n') tn.write("coverInst = coverage(data_file='%s') " @@ -124,7 +125,7 @@ class CoverageController(wsgi.Controller): "else coverInst\n" % data_file) tn.write('coverInst.skipModules = sys.modules.keys()\n') tn.write("coverInst.start()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) def _check_coverage_module_loaded(self): @@ -172,7 +173,7 @@ class CoverageController(wsgi.Controller): def _stop_coverage_telnet(self, tn): tn.write("coverInst.stop()\n") tn.write("coverInst.save()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) def _check_coverage(self): @@ -199,14 +200,14 @@ class CoverageController(wsgi.Controller): if xml: execute = str("coverInst.xml_report(outfile='%s')\n" % path) tn.write(execute) - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) else: execute = str("output = open('%s', 'w')\n" % path) tn.write(execute) tn.write("coverInst.report(file=output)\n") tn.write("output.close()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) tn.close() @@ -277,7 +278,7 @@ class CoverageController(wsgi.Controller): def _reset_coverage_telnet(self, tn): tn.write("coverInst.erase()\n") - tn.write("print 'finished'\n") + tn.write("print('finished')\n") tn.expect([re.compile('finished')]) @extensions.expected_errors(503) diff --git a/nova/cmd/manage.py b/nova/cmd/manage.py index 06e6d8c1eac7..9f6bf54578b1 100644 --- a/nova/cmd/manage.py +++ b/nova/cmd/manage.py @@ -1097,7 +1097,7 @@ class AgentBuildCommands(object): agent_build.version, agent_build.md5hash)) print(' %s' % agent_build.url) - print + print() @args('--os', metavar='', help='os') @args('--architecture', dest='architecture', diff --git a/nova/wsgi.py b/nova/wsgi.py index e5566300d6e9..9a4ab90d5a1a 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -379,7 +379,7 @@ class Debug(Middleware): print(('*' * 40) + ' RESPONSE HEADERS') for (key, value) in resp.headers.iteritems(): print(key, '=', value) - print + print() resp.app_iter = self.print_generator(resp.app_iter) diff --git a/tools/config/analyze_opts.py b/tools/config/analyze_opts.py index 84294c9d31aa..c91f4dedafec 100755 --- a/tools/config/analyze_opts.py +++ b/tools/config/analyze_opts.py @@ -21,6 +21,9 @@ find_unused_options.py Compare the nova.conf file with the nova.conf.sample file to find any unused options or default values in nova.conf ''' + +from __future__ import print_function + import argparse import os import sys @@ -74,7 +77,7 @@ if __name__ == '__main__': for k, v in sorted(conf_file_options.items()): if k not in sample_conf_file_options: - print "Unused:", k + print("Unused:", k) for k, v in sorted(conf_file_options.items()): if k in sample_conf_file_options and v == sample_conf_file_options[k]: - print "Default valued:", k + print("Default valued:", k) diff --git a/tools/db/schema_diff.py b/tools/db/schema_diff.py index d2fd493914c6..8fd9aad829b2 100755 --- a/tools/db/schema_diff.py +++ b/tools/db/schema_diff.py @@ -31,6 +31,9 @@ Run like: ./tools/db/schema_diff.py mysql master:latest my_branch:82 """ + +from __future__ import print_function + import datetime import glob import os @@ -191,19 +194,19 @@ def git_has_uncommited_changes(): def die(msg): - print >> sys.stderr, "ERROR: %s" % msg + print("ERROR: %s" % msg, file=sys.stderr) sys.exit(1) def usage(msg=None): if msg: - print >> sys.stderr, "ERROR: %s" % msg + print("ERROR: %s" % msg, file=sys.stderr) prog = "schema_diff.py" args = ["", "", ""] - print >> sys.stderr, "usage: %s %s" % (prog, ' '.join(args)) + print("usage: %s %s" % (prog, ' '.join(args)), file=sys.stderr) sys.exit(1) diff --git a/tools/install_venv.py b/tools/install_venv.py index 8d30676df5f5..85d0c2794a97 100644 --- a/tools/install_venv.py +++ b/tools/install_venv.py @@ -19,6 +19,8 @@ # License for the specific language governing permissions and limitations # under the License. +from __future__ import print_function + import os import sys @@ -44,7 +46,7 @@ def print_help(venv, root): Also, make test will automatically use the virtualenv. """ - print help % (venv, root) + print(help % (venv, root)) def main(argv): diff --git a/tools/lintstack.py b/tools/lintstack.py index cda5f0b3d323..0240a43dc0db 100755 --- a/tools/lintstack.py +++ b/tools/lintstack.py @@ -18,6 +18,8 @@ """pylint error checking.""" +from __future__ import print_function + import cStringIO as StringIO import json import re @@ -114,9 +116,9 @@ class ErrorKeys(object): @classmethod def print_json(cls, errors, output=sys.stdout): - print >>output, "# automatically generated by tools/lintstack.py" + print("# automatically generated by tools/lintstack.py", file=output) for i in sorted(errors.keys()): - print >>output, json.dumps(i) + print(json.dumps(i), file=output) @classmethod def from_file(cls, filename): @@ -139,7 +141,7 @@ def run_pylint(): def generate_error_keys(msg=None): - print "Generating", KNOWN_PYLINT_EXCEPTIONS_FILE + print("Generating", KNOWN_PYLINT_EXCEPTIONS_FILE) if msg is None: msg = run_pylint() errors = LintOutput.from_msg_to_dict(msg) @@ -148,30 +150,30 @@ def generate_error_keys(msg=None): def validate(newmsg=None): - print "Loading", KNOWN_PYLINT_EXCEPTIONS_FILE + print("Loading", KNOWN_PYLINT_EXCEPTIONS_FILE) known = ErrorKeys.from_file(KNOWN_PYLINT_EXCEPTIONS_FILE) if newmsg is None: - print "Running pylint. Be patient..." + print("Running pylint. Be patient...") newmsg = run_pylint() errors = LintOutput.from_msg_to_dict(newmsg) - print "Unique errors reported by pylint: was %d, now %d." \ - % (len(known), len(errors)) + print("Unique errors reported by pylint: was %d, now %d." + % (len(known), len(errors))) passed = True for err_key, err_list in errors.items(): for err in err_list: if err_key not in known: - print err.lintoutput - print + print(err.lintoutput) + print() passed = False if passed: - print "Congrats! pylint check passed." + print("Congrats! pylint check passed.") redundant = known - set(errors.keys()) if redundant: - print "Extra credit: some known pylint exceptions disappeared." + print("Extra credit: some known pylint exceptions disappeared.") for i in sorted(redundant): - print json.dumps(i) - print "Consider regenerating the exception file if you will." + print(json.dumps(i)) + print("Consider regenerating the exception file if you will.") else: print ("Please fix the errors above. If you believe they are false" " positives, run 'tools/lintstack.py generate' to overwrite.") @@ -179,10 +181,10 @@ def validate(newmsg=None): def usage(): - print """Usage: tools/lintstack.py [generate|validate] + print("""Usage: tools/lintstack.py [generate|validate] To generate pylint_exceptions file: tools/lintstack.py generate To validate the current commit: tools/lintstack.py - """ + """) def main(): diff --git a/tools/regression_tester.py b/tools/regression_tester.py index aabd0d794088..89a5a4dd2929 100755 --- a/tools/regression_tester.py +++ b/tools/regression_tester.py @@ -32,6 +32,8 @@ Due to the risk of false positives, the results from this need some human interpretation. """ +from __future__ import print_function + import optparse import string import subprocess @@ -39,12 +41,12 @@ import sys def run(cmd, fail_ok=False): - print "running: %s" % cmd + print("running: %s" % cmd) obj = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) obj.wait() if obj.returncode != 0 and not fail_ok: - print "The above command terminated with an error." + print("The above command terminated with an error.") sys.exit(obj.returncode) return obj.stdout.read() @@ -95,13 +97,13 @@ def main(): run("git checkout %s" % original_branch) run("git branch -D %s" % new_branch) - print expect_failure - print "" - print "*******************************" + print(expect_failure) + print("") + print("*******************************") if test_works: - print "FOUND a regression test" + print("FOUND a regression test") else: - print "NO regression test" + print("NO regression test") sys.exit(1)