[dpdk-dev] [PATCH v2 2/3] app: make python apps python2/3 compliant

John McNamara john.mcnamara at intel.com
Sun Dec 18 15:25:49 CET 2016


Make all the DPDK Python apps work with Python 2 or 3 to
allow them to work with whatever is the system default.

Signed-off-by: John McNamara <john.mcnamara at intel.com>
---
 app/cmdline_test/cmdline_test.py      | 26 ++++++++++++------------
 app/cmdline_test/cmdline_test_data.py |  2 --
 app/test/autotest.py                  | 10 ++++-----
 app/test/autotest_data.py             |  2 --
 app/test/autotest_runner.py           | 37 ++++++++++++++++------------------
 app/test/autotest_test_funcs.py       |  2 --
 tools/cpu_layout.py                   | 38 ++++++++++++++++++-----------------
 tools/dpdk-devbind.py                 |  2 +-
 tools/dpdk-pmdinfo.py                 | 14 +++++++------
 9 files changed, 64 insertions(+), 69 deletions(-)

diff --git a/app/cmdline_test/cmdline_test.py b/app/cmdline_test/cmdline_test.py
index 4729987..229f71f 100755
--- a/app/cmdline_test/cmdline_test.py
+++ b/app/cmdline_test/cmdline_test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 #   BSD LICENSE
 #
@@ -32,7 +32,7 @@
 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 # Script that runs cmdline_test app and feeds keystrokes into it.
-
+from __future__ import print_function
 import cmdline_test_data
 import os
 import pexpect
@@ -81,38 +81,38 @@ def runHistoryTest(child):
 
 # the path to cmdline_test executable is supplied via command-line.
 if len(sys.argv) < 2:
-    print "Error: please supply cmdline_test app path"
+    print("Error: please supply cmdline_test app path")
     sys.exit(1)
 
 test_app_path = sys.argv[1]
 
 if not os.path.exists(test_app_path):
-    print "Error: please supply cmdline_test app path"
+    print("Error: please supply cmdline_test app path")
     sys.exit(1)
 
 child = pexpect.spawn(test_app_path)
 
-print "Running command-line tests..."
+print("Running command-line tests...")
 for test in cmdline_test_data.tests:
-    print (test["Name"] + ":").ljust(30),
+    testname = (test["Name"] + ":").ljust(30)
     try:
         runTest(child, test)
-        print "PASS"
+        print(testname, "PASS")
     except:
-        print "FAIL"
-        print child
+        print(testname, "FAIL")
+        print(child)
         sys.exit(1)
 
 # since last test quits the app, run new instance
 child = pexpect.spawn(test_app_path)
 
-print ("History fill test:").ljust(30),
+testname = ("History fill test:").ljust(30)
 try:
     runHistoryTest(child)
-    print "PASS"
+    print(testname, "PASS")
 except:
-    print "FAIL"
-    print child
+    print(testname, "FAIL")
+    print(child)
     sys.exit(1)
 child.close()
 sys.exit(0)
diff --git a/app/cmdline_test/cmdline_test_data.py b/app/cmdline_test/cmdline_test_data.py
index 3ce6cbc..28dfefe 100644
--- a/app/cmdline_test/cmdline_test_data.py
+++ b/app/cmdline_test/cmdline_test_data.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 #   BSD LICENSE
 #
 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
diff --git a/app/test/autotest.py b/app/test/autotest.py
index 3a00538..5c19a02 100644
--- a/app/test/autotest.py
+++ b/app/test/autotest.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 
 #   BSD LICENSE
 #
@@ -32,15 +32,15 @@
 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 # Script that uses either test app or qemu controlled by python-pexpect
-
+from __future__ import print_function
 import autotest_data
 import autotest_runner
 import sys
 
 
 def usage():
-    print"Usage: autotest.py [test app|test iso image]",
-    print "[target] [whitelist|-blacklist]"
+    print("Usage: autotest.py [test app|test iso image] ",
+          "[target] [whitelist|-blacklist]")
 
 if len(sys.argv) < 3:
     usage()
@@ -63,7 +63,7 @@ def usage():
 
 cmdline = "%s -c f -n 4" % (sys.argv[1])
 
-print cmdline
+print(cmdline)
 
 runner = autotest_runner.AutotestRunner(cmdline, target, test_blacklist,
                                         test_whitelist)
diff --git a/app/test/autotest_data.py b/app/test/autotest_data.py
index 0cf4cfd..0cd598b 100644
--- a/app/test/autotest_data.py
+++ b/app/test/autotest_data.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-
 #   BSD LICENSE
 #
 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
diff --git a/app/test/autotest_runner.py b/app/test/autotest_runner.py
index 55b63a8..fc882ec 100644
--- a/app/test/autotest_runner.py
+++ b/app/test/autotest_runner.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 #   BSD LICENSE
 #
 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
@@ -271,15 +269,16 @@ def __process_results(self, results):
             total_time = int(cur_time - self.start)
 
             # print results, test run time and total time since start
-            print ("%s:" % test_name).ljust(30),
-            print result_str.ljust(29),
-            print "[%02dm %02ds]" % (test_time / 60, test_time % 60),
+            result = ("%s:" % test_name).ljust(30)
+            result += result_str.ljust(29)
+            result += "[%02dm %02ds]" % (test_time / 60, test_time % 60)
 
             # don't print out total time every line, it's the same anyway
             if i == len(results) - 1:
-                print "[%02dm %02ds]" % (total_time / 60, total_time % 60)
+                print(result,
+                      "[%02dm %02ds]" % (total_time / 60, total_time % 60))
             else:
-                print ""
+                print(result)
 
             # if test failed and it wasn't a "start" test
             if test_result < 0 and not i == 0:
@@ -294,7 +293,7 @@ def __process_results(self, results):
                     f = open("%s_%s_report.rst" %
                              (self.target, test_name), "w")
                 except IOError:
-                    print "Report for %s could not be created!" % test_name
+                    print("Report for %s could not be created!" % test_name)
                 else:
                     with f:
                         f.write(report)
@@ -360,12 +359,10 @@ def run_all_tests(self):
         try:
 
             # create table header
-            print ""
-            print "Test name".ljust(30),
-            print "Test result".ljust(29),
-            print "Test".center(9),
-            print "Total".center(9)
-            print "=" * 80
+            print("")
+            print("Test name".ljust(30), "Test result".ljust(29),
+                  "Test".center(9), "Total".center(9))
+            print("=" * 80)
 
             # make a note of tests start time
             self.start = time.time()
@@ -407,11 +404,11 @@ def run_all_tests(self):
             total_time = int(cur_time - self.start)
 
             # print out summary
-            print "=" * 80
-            print "Total run time: %02dm %02ds" % (total_time / 60,
-                                                   total_time % 60)
+            print("=" * 80)
+            print("Total run time: %02dm %02ds" % (total_time / 60,
+                                                   total_time % 60))
             if self.fails != 0:
-                print "Number of failed tests: %s" % str(self.fails)
+                print("Number of failed tests: %s" % str(self.fails))
 
             # write summary to logfile
             self.logfile.write("Summary\n")
@@ -420,8 +417,8 @@ def run_all_tests(self):
             self.logfile.write("Failed tests: ".ljust(
                 15) + "%i\n" % self.fails)
         except:
-            print "Exception occurred"
-            print sys.exc_info()
+            print("Exception occurred")
+            print(sys.exc_info())
             self.fails = 1
 
         # drop logs from all executions to a logfile
diff --git a/app/test/autotest_test_funcs.py b/app/test/autotest_test_funcs.py
index c482ea8..1c5f390 100644
--- a/app/test/autotest_test_funcs.py
+++ b/app/test/autotest_test_funcs.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 #   BSD LICENSE
 #
 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
diff --git a/tools/cpu_layout.py b/tools/cpu_layout.py
index ccc22ec..0e049a6 100755
--- a/tools/cpu_layout.py
+++ b/tools/cpu_layout.py
@@ -1,4 +1,5 @@
-#! /usr/bin/python
+#!/usr/bin/env python
+
 #
 #   BSD LICENSE
 #
@@ -31,7 +32,7 @@
 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
-
+from __future__ import print_function
 import sys
 
 sockets = []
@@ -55,7 +56,7 @@
 for core in core_details:
     for field in ["processor", "core id", "physical id"]:
         if field not in core:
-            print "Error getting '%s' value from /proc/cpuinfo" % field
+            print("Error getting '%s' value from /proc/cpuinfo" % field)
             sys.exit(1)
         core[field] = int(core[field])
 
@@ -68,29 +69,30 @@
         core_map[key] = []
     core_map[key].append(core["processor"])
 
-print "============================================================"
-print "Core and Socket Information (as reported by '/proc/cpuinfo')"
-print "============================================================\n"
-print "cores = ", cores
-print "sockets = ", sockets
-print ""
+print("============================================================")
+print("Core and Socket Information (as reported by '/proc/cpuinfo')")
+print("============================================================\n")
+print("cores = ", cores)
+print("sockets = ", sockets)
+print("")
 
 max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
 max_core_map_len = max_processor_len * 2 + len('[, ]') + len('Socket ')
 max_core_id_len = len(str(max(cores)))
 
-print " ".ljust(max_core_id_len + len('Core ')),
+output = " ".ljust(max_core_id_len + len('Core '))
 for s in sockets:
-    print "Socket %s" % str(s).ljust(max_core_map_len - len('Socket ')),
-print ""
+    output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket '))
+print(output)
 
-print " ".ljust(max_core_id_len + len('Core ')),
+output = " ".ljust(max_core_id_len + len('Core '))
 for s in sockets:
-    print "--------".ljust(max_core_map_len),
-print ""
+    output += " --------".ljust(max_core_map_len)
+    output += " "
+print(output)
 
 for c in cores:
-    print "Core %s" % str(c).ljust(max_core_id_len),
+    output = "Core %s" % str(c).ljust(max_core_id_len)
     for s in sockets:
-        print str(core_map[(s, c)]).ljust(max_core_map_len),
-    print ""
+        output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
+    print(output)
diff --git a/tools/dpdk-devbind.py b/tools/dpdk-devbind.py
index 4f51a4b..e057b87 100755
--- a/tools/dpdk-devbind.py
+++ b/tools/dpdk-devbind.py
@@ -1,4 +1,4 @@
-#! /usr/bin/python
+#! /usr/bin/env python
 #
 #   BSD LICENSE
 #
diff --git a/tools/dpdk-pmdinfo.py b/tools/dpdk-pmdinfo.py
index 3d3ad7d..d4e51aa 100755
--- a/tools/dpdk-pmdinfo.py
+++ b/tools/dpdk-pmdinfo.py
@@ -1,9 +1,11 @@
 #!/usr/bin/env python
+
 # -------------------------------------------------------------------------
 #
 # Utility to dump PMD_INFO_STRING support from an object file
 #
 # -------------------------------------------------------------------------
+from __future__ import print_function
 import json
 import os
 import platform
@@ -54,7 +56,7 @@ def addDevice(self, deviceStr):
             self.devices[devID] = Device(deviceStr)
 
     def report(self):
-        print self.ID, self.name
+        print(self.ID, self.name)
         for id, dev in self.devices.items():
             dev.report()
 
@@ -80,7 +82,7 @@ def __init__(self, deviceStr):
         self.subdevices = {}
 
     def report(self):
-        print "\t%s\t%s" % (self.ID, self.name)
+        print("\t%s\t%s" % (self.ID, self.name))
         for subID, subdev in self.subdevices.items():
             subdev.report()
 
@@ -126,7 +128,7 @@ def __init__(self, vendor, device, name):
         self.name = name
 
     def report(self):
-        print "\t\t%s\t%s\t%s" % (self.vendorID, self.deviceID, self.name)
+        print("\t\t%s\t%s\t%s" % (self.vendorID, self.deviceID, self.name))
 
 
 class PCIIds:
@@ -154,7 +156,7 @@ def reportVendors(self):
         """Reports the vendors
         """
         for vid, v in self.vendors.items():
-            print v.ID, v.name
+            print(v.ID, v.name)
 
     def report(self, vendor=None):
         """
@@ -185,7 +187,7 @@ def findDate(self, content):
 
     def parse(self):
         if len(self.contents) < 1:
-            print "data/%s-pci.ids not found" % self.date
+            print("data/%s-pci.ids not found" % self.date)
         else:
             vendorID = ""
             deviceID = ""
@@ -432,7 +434,7 @@ def process_dt_needed_entries(self):
 
         for tag in dynsec.iter_tags():
             if tag.entry.d_tag == 'DT_NEEDED':
-                rc = tag.needed.find("librte_pmd")
+                rc = tag.needed.find(b"librte_pmd")
                 if (rc != -1):
                     library = search_file(tag.needed,
                                           runpath + ":" + ldlibpath +
-- 
2.7.4



More information about the dev mailing list