From 2f266e0cec10e32db8781a8fb788b0432c33d6d9 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 3 Jan 2018 17:35:59 +0100 Subject: [PATCH] Improve python3 compatibility Use proper print() function to make scripts compatible with both python 2 and 3. This paves the way to deprecating python 2 support altogether. Change-Id: I80e5850a8978d78cda793e2192ef4bd3fd54a121 --- contrib/jenkins.sh | 2 +- scripts/osmodumpdoc.py | 18 +++++++++--------- scripts/osmotestconfig.py | 23 +++++++++++------------ scripts/osmotestvty.py | 12 ++++++------ setup.py | 5 ++++- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh index 9734549..ac67d85 100755 --- a/contrib/jenkins.sh +++ b/contrib/jenkins.sh @@ -16,7 +16,7 @@ done rm -rf ./build PY3=python3 -PY3_LIST="osmopy scripts/osmo_ctrl.py scripts/osmo_interact_ctrl.py scripts/osmo_interact_vty.py scripts/osmo_verify_transcript_ctrl.py scripts/osmo_verify_transcript_vty.py scripts/soap.py scripts/twisted_ipa.py" +PY3_LIST="osmopy scripts/osmo_ctrl.py scripts/osmodumpdoc.py scripts/osmotestvty.py scripts/osmotestconfig.py scripts/osmo_interact_ctrl.py scripts/osmo_interact_vty.py scripts/osmo_verify_transcript_ctrl.py scripts/osmo_verify_transcript_vty.py scripts/soap.py scripts/twisted_ipa.py" $PY3 ./setup.py install $PY3 tests/test_py3.py for f in $PY3_LIST diff --git a/scripts/osmodumpdoc.py b/scripts/osmodumpdoc.py index 2464b05..d71edc8 100644 --- a/scripts/osmodumpdoc.py +++ b/scripts/osmodumpdoc.py @@ -4,7 +4,7 @@ # Fixes may need to be applied to both. """Start the process and dump the documentation to the doc dir.""" - +from __future__ import print_function import subprocess import time import os @@ -21,7 +21,7 @@ def dump_doc(name, port, filename): out = open(filename, 'w') out.write(xml) out.close() - print 'generated %r' % filename + print('generated %r' % filename) """Dump the config of all the apps. @@ -40,22 +40,22 @@ def dump_configs(apps, configs, confpath): for app in apps: appname = app[3] - print "Starting app for %s" % appname + print("Starting app for %s" % appname) proc = None cmd = [app[1], "-c", os.path.join(confpath, configs[appname][0])] - print 'cd', os.path.abspath(os.path.curdir), ';', ' '.join(cmd) + print('cd', os.path.abspath(os.path.curdir), ';', ' '.join(cmd)) try: proc = subprocess.Popen(cmd, stdin=None, stdout=None) except OSError as e: # Probably a missing binary - print >> sys.stderr, e - print >> sys.stderr, "Skipping app %s" % appname + print(e, file=sys.stderr) + print("Skipping app %s" % appname, file=sys.stderr) failures += 1 else: try: dump_doc(app[2], app[0], 'doc/%s_vty_reference.xml' % appname) successes += 1 except IOError: # Generally a socket issue - print >> sys.stderr, "%s: couldn't connect, skipping" % appname + print("%s: couldn't connect, skipping" % appname, file=sys.stderr) failures += 1 finally: osmoutil.end_proc(proc) @@ -90,7 +90,7 @@ if __name__ == '__main__': num_fails, num_sucs = dump_configs( osmoappdesc.apps, osmoappdesc.app_configs, confpath) if num_fails > 0: - print >> sys.stderr, "Warning: Skipped %s apps" % num_fails + print("Warning: Skipped %s apps" % num_fails, file=sys.stderr) if 0 == num_sucs: - print >> sys.stderr, "Nothing run, wrong working dir? Set with -w" + print("Nothing run, wrong working dir? Set with -w", file=sys.stderr) sys.exit(num_fails) diff --git a/scripts/osmotestconfig.py b/scripts/osmotestconfig.py index 2132c43..f227504 100644 --- a/scripts/osmotestconfig.py +++ b/scripts/osmotestconfig.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +from __future__ import print_function import os import os.path import time @@ -52,7 +52,7 @@ def test_config_atest(app_desc, config, run_test, verbose=True): try: cmd = app_desc[1].split(' ') + [ "-c", config] if verbose: - print "Verifying %s, test %s" % (' '.join(cmd), run_test.__name__) + print("Verifying %s, test %s" % (' '.join(cmd), run_test.__name__)) proc = osmoutil.popen_devnull(cmd) end = app_desc[2] @@ -61,10 +61,10 @@ def test_config_atest(app_desc, config, run_test, verbose=True): ret = run_test(vty) except IOError as se: - print >> sys.stderr, "Failed to verify %s" % ' '.join(cmd) - print >> sys.stderr, "Current directory: %s" % os.getcwd() - print >> sys.stderr, "Error was %s" % se - print >> sys.stderr, "Config was\n%s" % open(config).read() + print("Failed to verify %s" % ' '.join(cmd), file=sys.stderr) + print("Current directory: %s" % os.getcwd(), file=sys.stderr) + print("Error was %s" % se, file=sys.stderr) + print("Config was\n%s" % open(config).read(), file=sys.stderr) raise se finally: @@ -125,9 +125,8 @@ def verify_doc(vty): all_errs.append(err_lines) - print >> sys.stderr, \ - "Documentation error (missing docs): \n%s\n%s\n" % ( - cmd_line, '\n'.join(err_lines)) + print("Documentation error (missing docs): \n%s\n%s\n" % ( + cmd_line, '\n'.join(err_lines)), file=sys.stderr) return (len(all_errs), all_errs) @@ -157,7 +156,7 @@ def check_configs_tested(basedir, app_configs, ignore_configs): if config in app_configs[app]: found = True if not found: - print >> sys.stderr, "Warning: %s is not being tested" % config + print("Warning: %s is not being tested" % config, file=sys.stderr) def test_all_apps(apps, app_configs, tmpdir="writtenconfig", verbose=True, @@ -166,7 +165,7 @@ def test_all_apps(apps, app_configs, tmpdir="writtenconfig", verbose=True, errors = 0 for app in apps: if not app_exists(app): - print >> sys.stderr, "Skipping app %s (not found)" % app[1] + print("Skipping app %s (not found)" % app[1], file=sys.stderr) continue configs = app_configs[app[3]] @@ -178,7 +177,7 @@ def test_all_apps(apps, app_configs, tmpdir="writtenconfig", verbose=True, remove_tmpdir(tmpdir) if errors: - print >> sys.stderr, "ERRORS: %d" % errors + print("ERRORS: %d" % errors, file=sys.stderr) return errors diff --git a/scripts/osmotestvty.py b/scripts/osmotestvty.py index e513c05..55017a5 100644 --- a/scripts/osmotestvty.py +++ b/scripts/osmotestvty.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +from __future__ import print_function import os import time import unittest @@ -36,11 +36,11 @@ class TestVTY(unittest.TestCase): osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi]) try: - print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd()) + print("Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())) self.proc = osmoutil.popen_devnull(osmo_vty_cmd) except OSError: - print >> sys.stderr, "Current directory: %s" % os.getcwd() - print >> sys.stderr, "Consider setting -b" + print("Current directory: %s" % os.getcwd(), file=sys.stderr) + print("Consider setting -b", file=sys.stderr) appstring = osmoappdesc.vty_app[2] appport = osmoappdesc.vty_app[0] @@ -94,9 +94,9 @@ if __name__ == '__main__': osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc", args.p) - print "confpath %s, workdir %s" % (confpath, workdir) + print("confpath %s, workdir %s" % (confpath, workdir)) os.chdir(workdir) - print "Running tests for specific VTY commands" + print("Running tests for specific VTY commands") suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY) res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) sys.exit(len(res.errors) + len(res.failures)) diff --git a/setup.py b/setup.py index bab9c38..bbb4b93 100755 --- a/setup.py +++ b/setup.py @@ -23,7 +23,10 @@ if sys.version_info.major == 2: scripts = ["scripts/osmodumpdoc.py", "scripts/osmotestconfig.py", "scripts/osmotestvty.py"] elif sys.version_info.major == 3: - scripts = ["scripts/osmo_interact_vty.py", + scripts = ["scripts/osmodumpdoc.py", + "scripts/osmotestconfig.py", + "scripts/osmotestvty.py", + "scripts/osmo_interact_vty.py", "scripts/osmo_interact_ctrl.py", "scripts/osmo_ctrl.py", "scripts/soap.py",