usertools: fix indentation
[dpdk.git] / usertools / dpdk-pmdinfo.py
index 9d5c636..cef5e2b 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # SPDX-License-Identifier: BSD-3-Clause
 # Copyright(c) 2016  Neil Horman <nhorman@tuxdriver.com>
 
@@ -7,13 +7,9 @@
 # Utility to dump PMD_INFO_STRING support from an object file
 #
 # -------------------------------------------------------------------------
-from __future__ import print_function
-from __future__ import unicode_literals
 import json
-import io
 import os
 import platform
-import string
 import sys
 from elftools.common.exceptions import ELFError
 from elftools.common.py3compat import byte2int
@@ -29,7 +25,6 @@ pcidb = None
 
 # ===========================================
 
-
 class Vendor:
     """
     Class for vendors. This is the top level class
@@ -114,8 +109,7 @@ class Device:
         except:
             if (subven == "ffff" and subdev == "ffff"):
                 return SubDevice("ffff", "ffff", "(All Subdevices)")
-            else:
-                return SubDevice(subven, subdev, "(Unknown Subdevice)")
+            return SubDevice(subven, subdev, "(Unknown Subdevice)")
 
 
 class SubDevice:
@@ -215,7 +209,7 @@ class PCIIds:
         """
         Reads the local file
         """
-        with io.open(filename, 'r', encoding='utf-8') as f:
+        with open(filename, 'r', encoding='utf-8') as f:
             self.contents = f.readlines()
         self.date = self.findDate(self.contents)
 
@@ -232,7 +226,7 @@ class PCIIds:
 
 def search_file(filename, search_path):
     """ Given a search path, find file with requested name """
-    for path in string.split(search_path, ":"):
+    for path in search_path.split(':'):
         candidate = os.path.join(path, filename)
         if os.path.exists(candidate):
             return os.path.abspath(candidate)
@@ -266,8 +260,7 @@ class ReadElf(object):
             num = int(spec)
             if num < self.elffile.num_sections():
                 return self.elffile.get_section(num)
-            else:
-                return None
+            return None
         except ValueError:
             # Not a number. Must be a name then
             section = self.elffile.get_section_by_name(force_unicode(spec))
@@ -313,7 +306,7 @@ class ReadElf(object):
             except KeyError:
                 continue
 
-        if (len(pmdinfo["pci_ids"]) != 0):
+        if len(pmdinfo["pci_ids"]) != 0:
             print("PMD HW SUPPORT:")
             if pcidb is not None:
                 self.pretty_print_pmdinfo(pmdinfo)
@@ -338,7 +331,7 @@ class ReadElf(object):
 
         while dataptr < len(data):
             while (dataptr < len(data) and
-                    not (32 <= byte2int(data[dataptr]) <= 127)):
+                   not 32 <= byte2int(data[dataptr]) <= 127):
                 dataptr += 1
 
             if dataptr >= len(data):
@@ -351,7 +344,7 @@ class ReadElf(object):
             # pyelftools may return byte-strings, force decode them
             mystring = force_unicode(data[dataptr:endptr])
             rc = mystring.find("PMD_INFO_STRING")
-            if (rc != -1):
+            if rc != -1:
                 self.parse_pmd_info_string(mystring)
 
             dataptr = endptr
@@ -382,9 +375,9 @@ class ReadElf(object):
                                       ":/usr/lib64:/lib64:/usr/lib:/lib")
                 if library is None:
                     return (None, None)
-                if raw_output is False:
+                if not raw_output:
                     print("Scanning for autoload path in %s" % library)
-                scanfile = io.open(library, 'rb')
+                scanfile = open(library, 'rb')
                 scanelf = ReadElf(scanfile, sys.stdout)
         except AttributeError:
             # Not a dynamic binary
@@ -404,7 +397,7 @@ class ReadElf(object):
 
         while dataptr < len(data):
             while (dataptr < len(data) and
-                    not (32 <= byte2int(data[dataptr]) <= 127)):
+                   not 32 <= byte2int(data[dataptr]) <= 127):
                 dataptr += 1
 
             if dataptr >= len(data):
@@ -417,7 +410,7 @@ class ReadElf(object):
             # pyelftools may return byte-strings, force decode them
             mystring = force_unicode(data[dataptr:endptr])
             rc = mystring.find("DPDK_PLUGIN_PATH")
-            if (rc != -1):
+            if rc != -1:
                 rc = mystring.find("=")
                 return (mystring[rc + 1:], library)
 
@@ -437,7 +430,6 @@ class ReadElf(object):
         """ Look to see if there are any DT_NEEDED entries in the binary
             And process those if there are
         """
-        global raw_output
         runpath = ""
         ldlibpath = os.environ.get('LD_LIBRARY_PATH')
         if ldlibpath is None:
@@ -453,14 +445,12 @@ class ReadElf(object):
         for tag in dynsec.iter_tags():
             # pyelftools may return byte-strings, force decode them
             if force_unicode(tag.entry.d_tag) == 'DT_NEEDED':
-                if 'librte_pmd' in force_unicode(tag.needed):
+                if 'librte_' in force_unicode(tag.needed):
                     library = search_file(force_unicode(tag.needed),
                                           runpath + ":" + ldlibpath +
                                           ":/usr/lib64:/lib64:/usr/lib:/lib")
                     if library is not None:
-                        if raw_output is False:
-                            print("Scanning %s for pmd information" % library)
-                        with io.open(library, 'rb') as file:
+                        with open(library, 'rb') as file:
                             try:
                                 libelf = ReadElf(file, sys.stdout)
                             except ELFError:
@@ -488,7 +478,7 @@ def force_bytes(s):
 def scan_autoload_path(autoload_path):
     global raw_output
 
-    if os.path.exists(autoload_path) is False:
+    if not os.path.exists(autoload_path):
         return
 
     try:
@@ -503,7 +493,7 @@ def scan_autoload_path(autoload_path):
             scan_autoload_path(dpath)
         if os.path.isfile(dpath):
             try:
-                file = io.open(dpath, 'rb')
+                file = open(dpath, 'rb')
                 readelf = ReadElf(file, sys.stdout)
             except ELFError:
                 # this is likely not an elf file, skip it
@@ -512,7 +502,7 @@ def scan_autoload_path(autoload_path):
                 # No permission to read the file, skip it
                 continue
 
-            if raw_output is False:
+            if not raw_output:
                 print("Hw Support for library %s" % d)
             readelf.display_pmd_info_strings(".rodata")
             file.close()
@@ -525,31 +515,31 @@ def scan_for_autoload_pmds(dpdk_path):
     """
     global raw_output
 
-    if (os.path.isfile(dpdk_path) is False):
-        if raw_output is False:
+    if not os.path.isfile(dpdk_path):
+        if not raw_output:
             print("Must specify a file name")
         return
 
-    file = io.open(dpdk_path, 'rb')
+    file = open(dpdk_path, 'rb')
     try:
         readelf = ReadElf(file, sys.stdout)
     except ElfError:
-        if raw_output is False:
+        if not raw_output:
             print("Unable to parse %s" % file)
         return
 
     (autoload_path, scannedfile) = readelf.search_for_autoload_path()
     if not autoload_path:
-        if (raw_output is False):
+        if not raw_output:
             print("No autoload path configured in %s" % dpdk_path)
         return
-    if (raw_output is False):
-        if (scannedfile is None):
+    if not raw_output:
+        if scannedfile is None:
             scannedfile = dpdk_path
         print("Found autoload path %s in %s" % (autoload_path, scannedfile))
 
     file.close()
-    if (raw_output is False):
+    if not raw_output:
         print("Discovered Autoload HW Support:")
     scan_autoload_path(autoload_path)
     return
@@ -561,7 +551,10 @@ def main(stream=None):
 
     pcifile_default = "./pci.ids"  # For unknown OS's assume local file
     if platform.system() == 'Linux':
-        pcifile_default = "/usr/share/hwdata/pci.ids"
+        # hwdata is the legacy location, misc is supported going forward
+        pcifile_default = "/usr/share/misc/pci.ids"
+        if not os.path.exists(pcifile_default):
+            pcifile_default = "/usr/share/hwdata/pci.ids"
     elif platform.system() == 'FreeBSD':
         pcifile_default = "/usr/local/share/pciids/pci.ids"
         if not os.path.exists(pcifile_default):
@@ -601,28 +594,28 @@ def main(stream=None):
         options.pcifile = None
         pcidb = None
 
-    if (len(args) == 0):
+    if len(args) == 0:
         optparser.print_usage()
         exit(1)
 
-    if options.pdir is True:
+    if options.pdir:
         exit(scan_for_autoload_pmds(args[0]))
 
     ldlibpath = os.environ.get('LD_LIBRARY_PATH')
-    if (ldlibpath is None):
+    if ldlibpath is None:
         ldlibpath = ""
 
-    if (os.path.exists(args[0]) is True):
+    if os.path.exists(args[0]):
         myelffile = args[0]
     else:
         myelffile = search_file(
             args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib")
 
-    if (myelffile is None):
+    if myelffile is None:
         print("File not found")
         sys.exit(1)
 
-    with io.open(myelffile, 'rb') as file:
+    with open(myelffile, 'rb') as file:
         try:
             readelf = ReadElf(file, sys.stdout)
             readelf.process_dt_needed_entries()