[PATCH] python/osmo-python-tests[master]: Improve python3 compatibility

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue Dec 19 16:35:08 UTC 2017


Hello Neels Hofmeyr, Harald Welte, Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/5038

to look at the new patch set (#4).

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
---
M contrib/jenkins.sh
M scripts/osmodumpdoc.py
M scripts/osmotestconfig.py
M scripts/osmotestvty.py
M setup.py
5 files changed, 33 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/38/5038/4

diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh
index 126f66b..749f57a 100755
--- a/contrib/jenkins.sh
+++ b/contrib/jenkins.sh
@@ -19,6 +19,9 @@
 python3 -m compileall scripts/osmo_interact_common.py
 python3 -m compileall scripts/osmo_interact_ctrl.py
 python3 -m compileall scripts/osmo_interact_vty.py
+python3 -m compileall scripts/osmodumpdoc.py
+python3 -m compileall scripts/osmotestvty.py
+python3 -m compileall scripts/osmotestconfig.py
 python3 -m compileall scripts/osmo_verify_transcript_ctrl.py
 python3 -m compileall scripts/osmo_verify_transcript_vty.py
 python3 -m compileall scripts/soap.py
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 @@
     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 @@
 
     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 @@
     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 <http://www.gnu.org/licenses/>.
-
+from __future__ import print_function
 import os
 import os.path
 import time
@@ -52,7 +52,7 @@
     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 @@
         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 @@
 
             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 @@
             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 @@
     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 @@
         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 <http://www.gnu.org/licenses/>.
-
+from __future__ import print_function
 import os
 import time
 import unittest
@@ -36,11 +36,11 @@
             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 @@
     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 494f63f..e1bdafe 100755
--- a/setup.py
+++ b/setup.py
@@ -23,7 +23,10 @@
 	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/osmo_rate_ctr2csv.py",

-- 
To view, visit https://gerrit.osmocom.org/5038
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I80e5850a8978d78cda793e2192ef4bd3fd54a121
Gerrit-PatchSet: 4
Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list