Python script updates
cover Python script updates
After writing the hugepages script, decided that the other python scripts need a cleanup and refresh. These changes apply after Louise Kilheeney's patch to change to full Python 3 only.
Stephen Hemminger (11):
cpu_layout: refactor to meet python standards
dpdk-pmdinfo: replace string.split with split
dpdk-pmdinfo: replace io.open with open
dpdk-pmdinfo: remove dead code
dpdk-pmdinfo: remove unnecessary paren and else
dpdk-pmdinfo: replace is False and is True
dpdk-pmdinfo: fix indentation
dpdk-pmdinfo: replace deprecated optionparse with argparse
dpdk-pmdinfo: do not use len(x) to test for empty
dpdk-telemetry-client: fix some pylint warnings
dpdk-devbind: use argparse instead of getopt
usertools/cpu_layout.py | 145 ++++++++++++--------- usertools/dpdk-devbind.py | 196 +++++++++++++---------------- usertools/dpdk-pmdinfo.py | 140 +++++++++------------ usertools/dpdk-telemetry-client.py | 28 +++-- 4 files changed, 249 insertions(+), 260 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/11 cpu_layout: refactor to meet python standards
Rearrange code to make it pass python lint. This includes add a main function, docstring, and some variable name changes.
--- usertools/cpu_layout.py | 145 +++++++++++++++++++++++----------------- 1 file changed, 85 insertions(+), 60 deletions(-)
usertools/cpu_layout.py +85 -60
@@ -2,67 +2,92 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2010-2014 Intel Corporation # Copyright(c) 2017 Cavium, Inc. All rights reserved. +""" +Show CPU layout +""" +SYS_DEVICES_CPU = "/sys/devices/system/cpu" -import sys - -sockets = [] -cores = [] -core_map = {} -base_path = "/sys/devices/system/cpu" -fd = open("{}/kernel_max".format(base_path)) -max_cpus = int(fd.read()) -fd.close() -for cpu in range(max_cpus + 1): - try: - fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu)) - except IOError: - continue - except: - break - core = int(fd.read()) - fd.close() - fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu)) - socket = int(fd.read()) - fd.close() - if core not in cores: - cores.append(core) - if socket not in sockets: - sockets.append(socket) - key = (socket, core) - if key not in core_map: - core_map[key] = [] - core_map[key].append(cpu) - -print(format("=" * (47 + len(base_path)))) -print("Core and Socket Information (as reported by '{}')".format(base_path)) -print("{}\n".format("=" * (47 + len(base_path)))) -print("cores = ", cores) -print("sockets = ", sockets) -print("") - -max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1)) -max_thread_count = len(list(core_map.values())[0]) -max_core_map_len = (max_processor_len * max_thread_count) \ + +def print_coremap(sockets, cores, core_map): + '''print core, thread, socket mapping''' + max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1)) + max_thread_count = len(list(core_map.values())[0]) + max_core_map_len = (max_processor_len * max_thread_count) \ + len(", ") * (max_thread_count - 1) \ + len('[]') + len('Socket ') -max_core_id_len = len(str(max(cores))) - -output = " ".ljust(max_core_id_len + len('Core ')) -for s in sockets: - output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket ')) -print(output) - -output = " ".ljust(max_core_id_len + len('Core ')) -for s in sockets: - output += " --------".ljust(max_core_map_len) - output += " " -print(output) - -for c in cores: - output = "Core %s" % str(c).ljust(max_core_id_len) - for s in sockets: - if (s,c) in core_map: - output += " " + str(core_map[(s, c)]).ljust(max_core_map_len) - else: - output += " " * (max_core_map_len + 1) + + max_core_id_len = len(str(max(cores))) + + output = " ".ljust(max_core_id_len + len('Core ')) + for socket in sockets: + output += " Socket %s" % str(socket).ljust(max_core_map_len - + len('Socket ')) + print(output) + + output = " ".ljust(max_core_id_len + len('Core ')) + for socket in sockets: + output += " --------".ljust(max_core_map_len) + output += " " print(output) + + for core in cores: + output = "Core %s" % str(core).ljust(max_core_id_len) + for socket in sockets: + if (socket, core) in core_map: + output += " " + str(core_map[(socket, + core)]).ljust(max_core_map_len) + else: + output += " " * (max_core_map_len + 1) + print(output) + + +def print_header(sockets, cores): + '''print the core socket information header''' + header_len = 47 + len(SYS_DEVICES_CPU) + print(format("=" * header_len)) + print("Core and Socket Information (as reported by '{}')".format( + SYS_DEVICES_CPU)) + print("{}\n".format("=" * header_len)) + print("cores = ", cores) + print("sockets = ", sockets) + print("") + + +def main(): + '''program main function''' + + with open("{}/kernel_max".format(SYS_DEVICES_CPU)) as kernel_max: + max_cpus = int(kernel_max.read()) + + core_map = {} + sockets = [] + cores = [] + + for cpu in range(max_cpus + 1): + topo_path = "{}/cpu{}/topology/".format(SYS_DEVICES_CPU, cpu) + try: + with open(topo_path + "core_id") as core_id: + core = int(core_id.read()) + except FileNotFoundError: + break + except IOError: + continue + + with open(topo_path + "physical_package_id") as package_id: + socket = int(package_id.read()) + + if core not in cores: + cores.append(core) + if socket not in sockets: + sockets.append(socket) + key = (socket, core) + if key not in core_map: + core_map[key] = [] + core_map[key].append(cpu) + + print_header(sockets, cores) + print_coremap(sockets, cores, core_map) + + +if __name__ == "__main__": + main()
Signature · 1 line
2.27.0
PATCH v2 1/1 cpu_layout: refactor to meet python standards
Rearrange code to make it pass python lint totally clean! This includes add a main function, docstring, and some variable name changes.
---
v2 - rebase to current main
usertools/cpu_layout.py | 143 ++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 58 deletions(-)
usertools/cpu_layout.py +85 -58
@@ -2,65 +2,92 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2010-2014 Intel Corporation # Copyright(c) 2017 Cavium, Inc. All rights reserved. +""" +Show CPU layout +""" +SYS_DEVICES_CPU = "/sys/devices/system/cpu" -sockets = [] -cores = [] -core_map = {} -base_path = "/sys/devices/system/cpu" -fd = open("{}/kernel_max".format(base_path)) -max_cpus = int(fd.read()) -fd.close() -for cpu in range(max_cpus + 1): - try: - fd = open("{}/cpu{}/topology/core_id".format(base_path, cpu)) - except IOError: - continue - except: - break - core = int(fd.read()) - fd.close() - fd = open("{}/cpu{}/topology/physical_package_id".format(base_path, cpu)) - socket = int(fd.read()) - fd.close() - if core not in cores: - cores.append(core) - if socket not in sockets: - sockets.append(socket) - key = (socket, core) - if key not in core_map: - core_map[key] = [] - core_map[key].append(cpu) - -print(format("=" * (47 + len(base_path)))) -print("Core and Socket Information (as reported by '{}')".format(base_path)) -print("{}\n".format("=" * (47 + len(base_path)))) -print("cores = ", cores) -print("sockets = ", sockets) -print("") - -max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1)) -max_thread_count = len(list(core_map.values())[0]) -max_core_map_len = (max_processor_len * max_thread_count) \ + +def print_coremap(sockets, cores, core_map): + '''print core, thread, socket mapping''' + max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1)) + max_thread_count = len(list(core_map.values())[0]) + max_core_map_len = (max_processor_len * max_thread_count) \ + len(", ") * (max_thread_count - 1) \ + len('[]') + len('Socket ') -max_core_id_len = len(str(max(cores))) - -output = " ".ljust(max_core_id_len + len('Core ')) -for s in sockets: - output += " Socket %s" % str(s).ljust(max_core_map_len - len('Socket ')) -print(output) - -output = " ".ljust(max_core_id_len + len('Core ')) -for s in sockets: - output += " --------".ljust(max_core_map_len) - output += " " -print(output) - -for c in cores: - output = "Core %s" % str(c).ljust(max_core_id_len) - for s in sockets: - if (s,c) in core_map: - output += " " + str(core_map[(s, c)]).ljust(max_core_map_len) - else: - output += " " * (max_core_map_len + 1) + + max_core_id_len = len(str(max(cores))) + + output = " ".ljust(max_core_id_len + len('Core ')) + for socket in sockets: + output += " Socket %s" % str(socket).ljust(max_core_map_len - + len('Socket ')) + print(output) + + output = " ".ljust(max_core_id_len + len('Core ')) + for socket in sockets: + output += " --------".ljust(max_core_map_len) + output += " " print(output) + + for core in cores: + output = "Core %s" % str(core).ljust(max_core_id_len) + for socket in sockets: + if (socket, core) in core_map: + output += " " + str(core_map[(socket, + core)]).ljust(max_core_map_len) + else: + output += " " * (max_core_map_len + 1) + print(output) + + +def print_header(sockets, cores): + '''print the core socket information header''' + header_len = 47 + len(SYS_DEVICES_CPU) + print(format("=" * header_len)) + print("Core and Socket Information (as reported by '{}')".format( + SYS_DEVICES_CPU)) + print("{}\n".format("=" * header_len)) + print("cores = ", cores) + print("sockets = ", sockets) + print("") + + +def main(): + '''program main function''' + + with open("{}/kernel_max".format(SYS_DEVICES_CPU)) as kernel_max: + max_cpus = int(kernel_max.read()) + + core_map = {} + sockets = [] + cores = [] + + for cpu in range(max_cpus + 1): + topo_path = "{}/cpu{}/topology/".format(SYS_DEVICES_CPU, cpu) + try: + with open(topo_path + "core_id") as core_id: + core = int(core_id.read()) + except FileNotFoundError: + break + except IOError: + continue + + with open(topo_path + "physical_package_id") as package_id: + socket = int(package_id.read()) + + if core not in cores: + cores.append(core) + if socket not in sockets: + sockets.append(socket) + key = (socket, core) + if key not in core_map: + core_map[key] = [] + core_map[key].append(cpu) + + print_header(sockets, cores) + print_coremap(sockets, cores, core_map) + + +if __name__ == "__main__": + main()
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH v2] cpu_layout: refactor to meet python standards
Quoted 9 lines · Stephen Hemminger
Rearrange code to make it pass python lint totally clean! This includes add a main function, docstring, and some variable name changes.
Signed-off-by Stephen Hemminger <[email protected]> --- v2 -rebase to current main
usertools/cpu_layout.py| 143 ++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 58 deletions(-)
Hi Stephen,
Thanks for looking at this, but I honestly query the value of this scale of rework, since we take a 58 line linear script and increase it to an 85 line script with multiple functions being called. Rather than trying for full lint cleanliness, I think we'd be better to keep it simple and just aim for pep8/pycodestyle cleanliness. This shows only two small things to fix and saves massive rework.
$ pycodestyle cpu_layout.py cpu_layout.py:18:5: E722 do not use bare 'except' cpu_layout.py:62:14: E231 missing whitespace after ','
Regards,
/Bruce
Re: [dpdk-dev] [PATCH v2] cpu_layout: refactor to meet python standards
Quoted 20 lines · On Wed, 4 Nov 2020 09:21:05 +0000 Bruce Richardson…
Quoted 9 lines · Stephen Hemminger
Rearrange code to make it pass python lint totally clean! This includes add a main function, docstring, and some variable name changes.
Signed-off-by Stephen Hemminger <[email protected]> --- v2 -rebase to current main
usertools/cpu_layout.py| 143 ++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 58 deletions(-)Hi Stephen,
Thanks for looking at this, but I honestly query the value of this scale of rework, since we take a 58 line linear script and increase it to an 85 line script with multiple functions being called. Rather than trying for full lint cleanliness, I think we'd be better to keep it simple and just aim for pep8/pycodestyle cleanliness. This shows only two small things to fix and saves massive rework.
Quoted 4 lines
$ pycodestyle cpu_layout.py cpu_layout.py:18:5: E722 do not use bare 'except' cpu_layout.py:62:14: E231 missing whitespace after ','
I was mostly hoping that the python code could be cleaned up so that when the next set of python code comes for some new feature it would be more readable.
Could you send an alternative patch that addresses those changes.
PATCH 2/11 dpdk-pmdinfo: replace string.split with split
In reply to Stephen HemmingerIn python3 the standard way to split strings is to use the split() on the string object itself. The old way is broken and would cause a traceback.
--- usertools/dpdk-pmdinfo.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
usertools/dpdk-pmdinfo.py +1 -2
@@ -11,7 +11,6 @@ import io import os import platform -import string import sys from elftools.common.exceptions import ELFError from elftools.common.py3compat import byte2int
@@ -229,7 +228,7 @@ def loadLocal(self): 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)
Signature · 1 line
2.27.0
v2 cover dpdk-pmdinfo: python lint cleanups
Ran dpdk-pmdinfo through Python3 lint and it had lots of complaints. These are the obvious simple ones to fix. Skipped adding docstrings everywhere and doing large scale variable recasing.
v2 - split out the pmdinfo specific part
incorporate feedback from Bruce about last use of len()
Stephen Hemminger (7):
dpdk-pmdinfo: replace string.split with split
dpdk-pmdinfo: replace io.open with open
dpdk-pmdinfo: remove unnecessary paren and else
dpdk-pmdinfo: replace is False and is True
dpdk-pmdinfo: fix indentation
dpdk-pmdinfo: replace deprecated optparse with argparse
dpdk-pmdinfo: do not use len(x) to test for empty
usertools/dpdk-pmdinfo.py | 132 ++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 71 deletions(-)
Signature · 1 line
2.27.0
PATCH v2 1/7 dpdk-pmdinfo: replace string.split with split
In python3 the standard way to split strings is to use the split() on the string object itself. The old way is broken and would cause a traceback.
--- usertools/dpdk-pmdinfo.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
usertools/dpdk-pmdinfo.py +1 -2
@@ -11,7 +11,6 @@ import io import os import platform -import string import sys from elftools.common.exceptions import ELFError from elftools.common.py3compat import byte2int
@@ -229,7 +228,7 @@ def loadLocal(self): 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)
Signature · 1 line
2.27.0
PATCH v2 2/7 dpdk-pmdinfo: replace io.open with open
In reply to Stephen HemmingerThe builtin open() is the recommended approach in python3. io.open was for compatiablity with older versions.
--- usertools/dpdk-pmdinfo.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
usertools/dpdk-pmdinfo.py +6 -7
@@ -8,7 +8,6 @@ # # ------------------------------------------------------------------------- import json -import io import os import platform import sys
@@ -211,7 +210,7 @@ def readLocal(self, filename): """ 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)
@@ -380,7 +379,7 @@ def search_for_autoload_path(self): return (None, None) if raw_output is False: 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
@@ -456,7 +455,7 @@ def process_dt_needed_entries(self): 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:
@@ -499,7 +498,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
@@ -526,7 +525,7 @@ def scan_for_autoload_pmds(dpdk_path): 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:
@@ -621,7 +620,7 @@ def main(stream=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()
Signature · 1 line
2.27.0
PATCH v2 3/7 dpdk-pmdinfo: remove unnecessary paren and else
In reply to Stephen HemmingerPython lint complains:usertools/dpdk-pmdinfo.py:303:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:328:0: C0325: Unnecessary parens after 'not' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:341:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:394:0: C0325: Unnecessary parens after 'not' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:407:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:515:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:530:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:533:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:534:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:539:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:594:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:602:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:605:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:611:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:110:12: R1705: Unnecessary "else" after "return" (no-else-return) usertools/dpdk-pmdinfo.py:254:12: R1705: Unnecessary "else" after "return" (no-else-return)
--- usertools/dpdk-pmdinfo.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
usertools/dpdk-pmdinfo.py +16 -18
@@ -109,8 +109,7 @@ def find_subid(self, subven, subdev): 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:
@@ -261,8 +260,7 @@ def _section_from_spec(self, spec): 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))
@@ -308,7 +306,7 @@ def parse_pmd_info_string(self, mystring): 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)
@@ -333,7 +331,7 @@ def display_pmd_info_strings(self, section_spec): 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):
@@ -346,7 +344,7 @@ def display_pmd_info_strings(self, section_spec): # 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
@@ -399,7 +397,7 @@ def search_for_autoload_path(self): 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):
@@ -412,7 +410,7 @@ def search_for_autoload_path(self): # 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)
@@ -520,7 +518,7 @@ def scan_for_autoload_pmds(dpdk_path): """ global raw_output - if (os.path.isfile(dpdk_path) is False): + if os.path.isfile(dpdk_path) is False: if raw_output is False: print("Must specify a file name") return
@@ -535,16 +533,16 @@ def scan_for_autoload_pmds(dpdk_path): (autoload_path, scannedfile) = readelf.search_for_autoload_path() if not autoload_path: - if (raw_output is False): + if raw_output is False: print("No autoload path configured in %s" % dpdk_path) return - if (raw_output is False): - if (scannedfile is None): + if raw_output is False: + 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 raw_output is False: print("Discovered Autoload HW Support:") scan_autoload_path(autoload_path) return
@@ -599,7 +597,7 @@ def main(stream=None): options.pcifile = None pcidb = None - if (len(args) == 0): + if len(args) == 0: optparser.print_usage() exit(1)
@@ -607,16 +605,16 @@ def main(stream=None): 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]) is True: 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)
Signature · 1 line
2.27.0
PATCH v2 4/7 dpdk-pmdinfo: replace is False and is True
In reply to Stephen HemmingerCode reads better if unnecessary comparison with False and True is not used.
--- usertools/dpdk-pmdinfo.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
usertools/dpdk-pmdinfo.py +12 -12
@@ -375,7 +375,7 @@ def search_for_autoload_path(self): ":/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 = open(library, 'rb') scanelf = ReadElf(scanfile, sys.stdout)
@@ -451,7 +451,7 @@ def process_dt_needed_entries(self): runpath + ":" + ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") if library is not None: - if raw_output is False: + if not raw_output: print("Scanning %s for pmd information" % library) with open(library, 'rb') as file: try:
@@ -481,7 +481,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:
@@ -505,7 +505,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()
@@ -518,8 +518,8 @@ 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
@@ -527,22 +527,22 @@ def scan_for_autoload_pmds(dpdk_path): 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 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
@@ -601,14 +601,14 @@ def main(stream=None): 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: ldlibpath = "" - if os.path.exists(args[0]) is True: + if os.path.exists(args[0]): myelffile = args[0] else: myelffile = search_file(
Signature · 1 line
2.27.0
PATCH v2 5/7 dpdk-pmdinfo: fix indentation
In reply to Stephen HemmingerThis fixes indentation warnings from pylint.
--- usertools/dpdk-pmdinfo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
usertools/dpdk-pmdinfo.py +2 -2
@@ -331,7 +331,7 @@ def display_pmd_info_strings(self, section_spec): 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):
@@ -397,7 +397,7 @@ def search_for_autoload_path(self): 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):
Signature · 1 line
2.27.0
PATCH v2 6/7 dpdk-pmdinfo: replace deprecated optparse with argparse
In reply to Stephen HemmingerThe optparse module is deprecated and replaced with new argparse. The code now enforces the rule that only one of the output formats can be specified: raw or json.
--- usertools/dpdk-pmdinfo.py | 70 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 38 deletions(-)
usertools/dpdk-pmdinfo.py +32 -38
@@ -11,10 +11,11 @@ import os import platform import sys +import argparse from elftools.common.exceptions import ELFError from elftools.common.py3compat import byte2int from elftools.elf.elffile import ELFFile -from optparse import OptionParser + # For running from development directory. It should take precedence over the # installed pyelftools.
@@ -563,56 +564,49 @@ def main(stream=None): if not os.path.exists(pcifile_default): pcifile_default = "/usr/share/misc/pci_vendors" - optparser = OptionParser( - usage='usage: %prog [-hrtp] [-d <pci id file] <elf-file>', - description="Dump pmd hardware support info", - add_help_option=True) - optparser.add_option('-r', '--raw', - action='store_true', dest='raw_output', - help='Dump raw json strings') - optparser.add_option("-d", "--pcidb", dest="pcifile", - help="specify a pci database " - "to get vendor names from", - default=pcifile_default, metavar="FILE") - optparser.add_option("-t", "--table", dest="tblout", - help="output information on hw support as a " - "hex table", - action='store_true') - optparser.add_option("-p", "--plugindir", dest="pdir", - help="scan dpdk for autoload plugins", - action='store_true') - - options, args = optparser.parse_args() - - if options.raw_output: + parser = argparse.ArgumentParser( + usage='usage: %(prog)s [-hrtp] [-d <pci id file>] elf_file', + description="Dump pmd hardware support info") + group = parser.add_mutually_exclusive_group() + group.add_argument('-r', '--raw', + action='store_true', dest='raw_output', + help='dump raw json strings') + group.add_argument("-t", "--table", dest="tblout", + help="output information on hw support as a hex table", + action='store_true') + parser.add_argument("-d", "--pcidb", dest="pcifile", + help="specify a pci database to get vendor names from", + default=pcifile_default, metavar="FILE") + parser.add_argument("-p", "--plugindir", dest="pdir", + help="scan dpdk for autoload plugins", + action='store_true') + parser.add_argument("elf_file", help="driver shared object file") + args = parser.parse_args() + + if args.raw_output: raw_output = True - if options.pcifile: - pcidb = PCIIds(options.pcifile) + if args.tblout: + args.pcifile = None + + if args.pcifile: + pcidb = PCIIds(args.pcifile) if pcidb is None: print("Pci DB file not found") exit(1) - if options.tblout: - options.pcifile = None - pcidb = None - - if len(args) == 0: - optparser.print_usage() - exit(1) - - if options.pdir: + if args.pdir: exit(scan_for_autoload_pmds(args[0])) ldlibpath = os.environ.get('LD_LIBRARY_PATH') if ldlibpath is None: ldlibpath = "" - if os.path.exists(args[0]): - myelffile = args[0] + if os.path.exists(args.elf_file): + myelffile = args.elf_file else: - myelffile = search_file( - args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") + myelffile = search_file(args.elf_file, + ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") if myelffile is None: print("File not found")
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH v2 6/7] dpdk-pmdinfo: replace deprecated optparse with argparse
Hi Hemminger,
Latest "dpdk-devbind.py --status-dev" just can support {net,baseband,crypto,event,mempool,compress} devices, But can not support show Misc devices, can you fix it or provide new method to show?
Notes: Old dpdk-devbind.py can support show cbdma device by cmd " ./usertools/dpdk-devbind.py --status-dev misc "
Trace Log:
root@dpdk-yaobing-purely147:~/dpdk# ./usertools/dpdk-devbind.py --status-dev misc
usage: dpdk-devbind.py [-h] [-s]
[--status-dev {net,baseband,crypto,event,mempool,compress}]
[-b DRIVER | -u] [--force]
[DEVICE [DEVICE ...]]
dpdk-devbind.py: error: argument --status-dev: invalid choice: 'misc' (choose from 'net', 'baseband', 'crypto', 'event', 'mempool', 'compress')root@dpdk-yaobing-purely147:~/dpdk# ./usertools/dpdk-devbind.py -s Misc (rawdev) devices using kernel driver ========================================= 0000:00:04.0 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci
Best Regards
Jiang yu
Quoted 123 lines
-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Stephen Hemminger
Sent: Wednesday, November 4, 2020 2:49 PM
To: [email protected]
Cc: [email protected]; Stephen Hemminger <[email protected]>
Subject: [dpdk-dev] [PATCH v2 6/7] dpdk-pmdinfo: replace deprecated optparse with argparseThe optparse module is deprecated and replaced with new argparse. The code now enforces the rule that only one of the output formats can be
specified: raw or json.Signed-off-by Stephen Hemminger <[email protected]>--- usertools/dpdk-pmdinfo.py | 70 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 38 deletions(-)usertools/dpdk-pmdinfo.py index renamed from usertools/dpdk-pmdinfo.py +0
-0
aec2abe9a683..1c2c3d05fea6100755usertools/dpdk-pmdinfo.py +1
-0@@ -11,10 +11,11 @@ import os import platform import sys +import argparse from elftools.common.exceptions import ELFError fromelftools.common.py3compat import byte2int from elftools.elf.elffile import ELFFile -from optparse import OptionParser
+# For running from development directory. It should take precedence over
the # installed pyelftools.usertools/dpdk-pmdinfo.py +31
-38@@ -563,56 +564,49 @@ def main(stream=None): if not os.path.exists(pcifile_default): pcifile_default = "/usr/share/misc/pci_vendors" - optparser = OptionParser( - usage='usage: %prog [-hrtp] [-d <pci id file] <elf-file>', - description="Dump pmd hardware support info", - add_help_option=True) - optparser.add_option('-r', '--raw', - action='store_true', dest='raw_output', - help='Dump raw json strings') - optparser.add_option("-d", "--pcidb", dest="pcifile", - help="specify a pci database " - "to get vendor names from", - default=pcifile_default, metavar="FILE") - optparser.add_option("-t", "--table", dest="tblout", - help="output information on hw support as a " - "hex table", - action='store_true') - optparser.add_option("-p", "--plugindir", dest="pdir", - help="scan dpdk for autoload plugins", - action='store_true') - - options, args = optparser.parse_args() - - if options.raw_output: + parser = argparse.ArgumentParser( + usage='usage: %(prog)s [-hrtp] [-d <pci id file>] elf_file', + description="Dump pmd hardware support info") + group = parser.add_mutually_exclusive_group() + group.add_argument('-r', '--raw', + action='store_true', dest='raw_output', + help='dump raw json strings') + group.add_argument("-t", "--table", dest="tblout", + help="output information on hw support as a hex table", + action='store_true') + parser.add_argument("-d", "--pcidb", dest="pcifile", + help="specify a pci database to get vendor names from", + default=pcifile_default, metavar="FILE") + parser.add_argument("-p", "--plugindir", dest="pdir", + help="scan dpdk for autoload plugins", + action='store_true') + parser.add_argument("elf_file", help="driver shared object file") + args = parser.parse_args() + + if args.raw_output: raw_output = True - if options.pcifile: - pcidb = PCIIds(options.pcifile) + if args.tblout: + args.pcifile = None + + if args.pcifile: + pcidb = PCIIds(args.pcifile) if pcidb is None: print("Pci DB file not found") exit(1) - if options.tblout: - options.pcifile = None - pcidb = None - - if len(args) == 0: - optparser.print_usage() - exit(1) - - if options.pdir: + if args.pdir: exit(scan_for_autoload_pmds(args[0])) ldlibpath = os.environ.get('LD_LIBRARY_PATH') if ldlibpath is None: ldlibpath = "" - if os.path.exists(args[0]): - myelffile = args[0] + if os.path.exists(args.elf_file): + myelffile = args.elf_file else: - myelffile = search_file( - args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") + myelffile = search_file(args.elf_file, + ldlibpath + + ":/usr/lib64:/lib64:/usr/lib:/lib") if myelffile is None: print("File not found") --
Quoted 1 line
2.27.0
root@dpdk-yaobing-purely147:~/dpdk# ./usertools/dpdk-devbind.py -h
usage: dpdk-devbind.py [-h] [-s]
[--status-dev {net,baseband,crypto,event,mempool,compress}]
[-b DRIVER | -u] [--force]
[DEVICE [DEVICE ...]]Utility to bind and unbind devices from Linux kernel
positional arguments:
DEVICE Device specified as PCI "domain:bus:slot.func" syntax
or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may be referred to by interface name.
optional arguments:
-h, --help show this help message and exit
-s, --status Print the current status of all known devices.
--status-dev {net,baseband,crypto,event,mempool,compress}
Print the status of given device group.
-b DRIVER, --bind DRIVER
Select the driver to use or "none" to unbind the device
-u, --unbind Unbind a device (equivalent to "-b none")
--force Override restriction on binding devices in use by
Linux" WARNING: This can lead to loss of network connection and should be used with caution.
Examples:
---------
To display current device status:
dpdk-devbind.py --status
To display current network device status:
dpdk-devbind.py --status-dev net
To bind eth1 from the current driver and move to use vfio-pci
dpdk-devbind.py --bind=vfio-pci eth1
To unbind 0000:01:00.0 from using any driver
dpdk-devbind.py -u 0000:01:00.0
To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver
dpdk-devbind.py -b ixgbe 02:00.0 02:00.1
root@dpdk-yaobing-purely147:~/dpdk#
root@dpdk-yaobing-purely147:~/dpdk# ./usertools/dpdk-devbind.py -s
Network devices using kernel driver
===================================
0000:20:00.0 'Ethernet Connection X722 for 10GBASE-T 37d2' if=eno1 drv=i40e unused=vfio-pci *Active* 0000:20:00.1 'Ethernet Connection X722 for 10GBASE-T 37d2' if=eno2 drv=i40e unused=vfio-pci 0000:20:00.2 'Ethernet Connection X722 for 10GbE SFP+ 37d3' if=eno3 drv=i40e unused=vfio-pci 0000:20:00.3 'Ethernet Connection X722 for 10GbE SFP+ 37d3' if=eno4 drv=i40e unused=vfio-pci 0000:3b:00.0 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp59s0f0 drv=i40e unused=vfio-pci 0000:3b:00.1 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp59s0f1 drv=i40e unused=vfio-pci 0000:60:00.0 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp96s0f0 drv=i40e unused=vfio-pci 0000:60:00.1 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp96s0f1 drv=i40e unused=vfio-pci 0000:86:00.0 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp134s0f0 drv=i40e unused=vfio-pci 0000:86:00.1 'Ethernet Controller XL710 for 40GbE QSFP+ 1583' if=enp134s0f1 drv=i40e unused=vfio-pci 0000:d8:00.0 'Ethernet Controller X710 for 10GbE SFP+ 1572' if=enp216s0f0 drv=i40e unused=vfio-pci 0000:d8:00.1 'Ethernet Controller X710 for 10GbE SFP+ 1572' if=enp216s0f1 drv=i40e unused=vfio-pci 0000:d8:00.2 'Ethernet Controller X710 for 10GbE SFP+ 1572' if=enp216s0f2 drv=i40e unused=vfio-pci 0000:d8:00.3 'Ethernet Controller X710 for 10GbE SFP+ 1572' if=enp216s0f3 drv=i40e unused=vfio-pci
No 'Baseband' devices detected
==============================
Crypto devices using DPDK-compatible driver
===========================================
0000:1a:01.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:01.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1a:02.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:01.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1c:02.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:01.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.0 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.1 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.2 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.3 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.4 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.5 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.6 'Device 37c9' drv=vfio-pci unused=qat_c62xvf 0000:1e:02.7 'Device 37c9' drv=vfio-pci unused=qat_c62xvf
Crypto devices using kernel driver
==================================
0000:1a:00.0 'C62x Chipset QuickAssist Technology 37c8' drv=c6xx unused=qat_c62x,vfio-pci 0000:1c:00.0 'C62x Chipset QuickAssist Technology 37c8' drv=c6xx unused=qat_c62x,vfio-pci 0000:1e:00.0 'C62x Chipset QuickAssist Technology 37c8' drv=c6xx unused=qat_c62x,vfio-pci
No 'Eventdev' devices detected
==============================
No 'Mempool' devices detected
=============================
No 'Compress' devices detected
==============================
Misc (rawdev) devices using kernel driver
=========================================
0000:00:04.0 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.1 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.2 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.3 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.4 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.5 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.6 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:00:04.7 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.0 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.1 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.2 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.3 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.4 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.5 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.6 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci 0000:80:04.7 'Sky Lake-E CBDMA Registers 2021' drv=ioatdma unused=vfio-pci
No 'Regex' devices detected
===========================
PATCH v2 7/7 dpdk-pmdinfo: do not use len(x) to test for empty
In reply to Stephen HemmingerThis fixes the following python lint warnings.usertools/dpdk-pmdinfo.py:188:11: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition) usertools/dpdk-pmdinfo.py:196:21: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition) usertools/dpdk-pmdinfo.py:302:11: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition)
--- usertools/dpdk-pmdinfo.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
usertools/dpdk-pmdinfo.py +3 -3
@@ -185,7 +185,7 @@ def findDate(self, content): return None def parse(self): - if len(self.contents) < 1: + if not self.contents: print("data/%s-pci.ids not found" % self.date) else: vendorID = ""
@@ -193,7 +193,7 @@ def parse(self): for l in self.contents: if l[0] == "#": continue - elif len(l.strip()) == 0: + elif not l.strip(): continue else: if l.find("\t\t") == 0:
@@ -307,7 +307,7 @@ def parse_pmd_info_string(self, mystring): except KeyError: continue - if len(pmdinfo["pci_ids"]) != 0: + if pmdinfo["pci_ids"]: print("PMD HW SUPPORT:") if pcidb is not None: self.pretty_print_pmdinfo(pmdinfo)
Signature · 1 line
2.27.0
Thomas Monjalon
AppliedRe: [dpdk-dev] [PATCH v2 0/7] dpdk-pmdinfo: python lint cleanups
In reply to Stephen Hemminger04/11/2020 07:48, Stephen Hemminger:
Quoted 7 lines
Ran dpdk-pmdinfo through Python3 lint and it had lots of complaints. These are the obvious simple ones to fix. Skipped adding docstrings everywhere and doing large scale variable recasing.
v2 - split out the pmdinfo specific part
incorporate feedback from Bruce about last use oflen()
Quoted 8 lines
Stephen Hemminger (7):
dpdk-pmdinfo: replace string.split with split
dpdk-pmdinfo: replace io.open with open
dpdk-pmdinfo: remove unnecessary paren and else
dpdk-pmdinfo: replace is False and is True
dpdk-pmdinfo: fix indentation
dpdk-pmdinfo: replace deprecated optparse with argparse
dpdk-pmdinfo: do not use len(x) to test for empty
Applied, thanks
PATCH 3/11 dpdk-pmdinfo: replace io.open with open
In reply to Stephen HemmingerThe builtin open() is the recommended approach in python3. io.open was for compatiablity with older versions.
--- usertools/dpdk-pmdinfo.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
usertools/dpdk-pmdinfo.py +6 -7
@@ -8,7 +8,6 @@ # # ------------------------------------------------------------------------- import json -import io import os import platform import sys
@@ -211,7 +210,7 @@ def readLocal(self, filename): """ 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)
@@ -380,7 +379,7 @@ def search_for_autoload_path(self): return (None, None) if raw_output is False: 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
@@ -456,7 +455,7 @@ def process_dt_needed_entries(self): 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:
@@ -499,7 +498,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
@@ -526,7 +525,7 @@ def scan_for_autoload_pmds(dpdk_path): 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:
@@ -621,7 +620,7 @@ def main(stream=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()
Signature · 1 line
2.27.0
PATCH 4/11 dpdk-pmdinfo: remove dead code
In reply to Stephen HemmingerThe method loadLocal() is never called, and if it was it would throw an exception because it calling readLocal() without a filename.
Pylint spotted this.
--- usertools/dpdk-pmdinfo.py | 8 -------- 1 file changed, 8 deletions(-)
usertools/dpdk-pmdinfo.py +0 -8
@@ -214,14 +214,6 @@ def readLocal(self, filename): self.contents = f.readlines() self.date = self.findDate(self.contents) - def loadLocal(self): - """ - Loads database from local. If there is no file, - it creates a new one from web - """ - self.date = idsfile[0].split("/")[1].split("-")[0] - self.readLocal() - # =======================================
Signature · 1 line
2.27.0
PATCH 5/11 dpdk-pmdinfo: remove unnecessary paren and else
In reply to Stephen HemmingerPython lint complains:usertools/dpdk-pmdinfo.py:303:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:328:0: C0325: Unnecessary parens after 'not' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:341:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:394:0: C0325: Unnecessary parens after 'not' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:407:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:515:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:530:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:533:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:534:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:539:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:594:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:602:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:605:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:611:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) usertools/dpdk-pmdinfo.py:110:12: R1705: Unnecessary "else" after "return" (no-else-return) usertools/dpdk-pmdinfo.py:254:12: R1705: Unnecessary "else" after "return" (no-else-return)
--- usertools/dpdk-pmdinfo.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
usertools/dpdk-pmdinfo.py +16 -18
@@ -109,8 +109,7 @@ def find_subid(self, subven, subdev): 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:
@@ -253,8 +252,7 @@ def _section_from_spec(self, spec): 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))
@@ -300,7 +298,7 @@ def parse_pmd_info_string(self, mystring): 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)
@@ -325,7 +323,7 @@ def display_pmd_info_strings(self, section_spec): 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):
@@ -338,7 +336,7 @@ def display_pmd_info_strings(self, section_spec): # 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
@@ -391,7 +389,7 @@ def search_for_autoload_path(self): 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):
@@ -404,7 +402,7 @@ def search_for_autoload_path(self): # 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)
@@ -512,7 +510,7 @@ def scan_for_autoload_pmds(dpdk_path): """ global raw_output - if (os.path.isfile(dpdk_path) is False): + if os.path.isfile(dpdk_path) is False: if raw_output is False: print("Must specify a file name") return
@@ -527,16 +525,16 @@ def scan_for_autoload_pmds(dpdk_path): (autoload_path, scannedfile) = readelf.search_for_autoload_path() if not autoload_path: - if (raw_output is False): + if raw_output is False: print("No autoload path configured in %s" % dpdk_path) return - if (raw_output is False): - if (scannedfile is None): + if raw_output is False: + 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 raw_output is False: print("Discovered Autoload HW Support:") scan_autoload_path(autoload_path) return
@@ -591,7 +589,7 @@ def main(stream=None): options.pcifile = None pcidb = None - if (len(args) == 0): + if len(args) == 0: optparser.print_usage() exit(1)
@@ -599,16 +597,16 @@ def main(stream=None): 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]) is True: 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)
Signature · 1 line
2.27.0
PATCH 6/11 dpdk-pmdinfo: replace is False and is True
In reply to Stephen HemmingerCode reads better if unnecessary comparison with False and True is not used.
--- usertools/dpdk-pmdinfo.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
usertools/dpdk-pmdinfo.py +12 -12
@@ -367,7 +367,7 @@ def search_for_autoload_path(self): ":/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 = open(library, 'rb') scanelf = ReadElf(scanfile, sys.stdout)
@@ -443,7 +443,7 @@ def process_dt_needed_entries(self): runpath + ":" + ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") if library is not None: - if raw_output is False: + if not raw_output: print("Scanning %s for pmd information" % library) with open(library, 'rb') as file: try:
@@ -473,7 +473,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:
@@ -497,7 +497,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()
@@ -510,8 +510,8 @@ 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
@@ -519,22 +519,22 @@ def scan_for_autoload_pmds(dpdk_path): 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 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
@@ -593,14 +593,14 @@ def main(stream=None): 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: ldlibpath = "" - if os.path.exists(args[0]) is True: + if os.path.exists(args[0]): myelffile = args[0] else: myelffile = search_file(
Signature · 1 line
2.27.0
PATCH 7/11 dpdk-pmdinfo: fix indentation
In reply to Stephen HemmingerThis fixes indentation warnings from pylint.
--- usertools/dpdk-pmdinfo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
usertools/dpdk-pmdinfo.py +2 -2
@@ -323,7 +323,7 @@ def display_pmd_info_strings(self, section_spec): 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):
@@ -389,7 +389,7 @@ def search_for_autoload_path(self): 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):
Signature · 1 line
2.27.0
PATCH 8/11 dpdk-pmdinfo: replace deprecated optparse with argparse
In reply to Stephen HemmingerThe optparse module is deprecated and replaced with new argparse. The code now enforces the rule that only one of the output formats can be specified: raw or json.
--- usertools/dpdk-pmdinfo.py | 70 ++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 38 deletions(-)
usertools/dpdk-pmdinfo.py +32 -38
@@ -11,10 +11,11 @@ import os import platform import sys +import argparse from elftools.common.exceptions import ELFError from elftools.common.py3compat import byte2int from elftools.elf.elffile import ELFFile -from optparse import OptionParser + # For running from development directory. It should take precedence over the # installed pyelftools.
@@ -555,56 +556,49 @@ def main(stream=None): if not os.path.exists(pcifile_default): pcifile_default = "/usr/share/misc/pci_vendors" - optparser = OptionParser( - usage='usage: %prog [-hrtp] [-d <pci id file] <elf-file>', - description="Dump pmd hardware support info", - add_help_option=True) - optparser.add_option('-r', '--raw', - action='store_true', dest='raw_output', - help='Dump raw json strings') - optparser.add_option("-d", "--pcidb", dest="pcifile", - help="specify a pci database " - "to get vendor names from", - default=pcifile_default, metavar="FILE") - optparser.add_option("-t", "--table", dest="tblout", - help="output information on hw support as a " - "hex table", - action='store_true') - optparser.add_option("-p", "--plugindir", dest="pdir", - help="scan dpdk for autoload plugins", - action='store_true') - - options, args = optparser.parse_args() - - if options.raw_output: + parser = argparse.ArgumentParser( + usage='usage: %(prog)s [-hrtp] [-d <pci id file>] elf_file', + description="Dump pmd hardware support info") + group = parser.add_mutually_exclusive_group() + group.add_argument('-r', '--raw', + action='store_true', dest='raw_output', + help='dump raw json strings') + group.add_argument("-t", "--table", dest="tblout", + help="output information on hw support as a hex table", + action='store_true') + parser.add_argument("-d", "--pcidb", dest="pcifile", + help="specify a pci database to get vendor names from", + default=pcifile_default, metavar="FILE") + parser.add_argument("-p", "--plugindir", dest="pdir", + help="scan dpdk for autoload plugins", + action='store_true') + parser.add_argument("elf_file", help="driver shared object file") + args = parser.parse_args() + + if args.raw_output: raw_output = True - if options.pcifile: - pcidb = PCIIds(options.pcifile) + if args.tblout: + args.pcifile = None + + if args.pcifile: + pcidb = PCIIds(args.pcifile) if pcidb is None: print("Pci DB file not found") exit(1) - if options.tblout: - options.pcifile = None - pcidb = None - - if len(args) == 0: - optparser.print_usage() - exit(1) - - if options.pdir: + if args.pdir: exit(scan_for_autoload_pmds(args[0])) ldlibpath = os.environ.get('LD_LIBRARY_PATH') if ldlibpath is None: ldlibpath = "" - if os.path.exists(args[0]): - myelffile = args[0] + if os.path.exists(args.elf_file): + myelffile = args.elf_file else: - myelffile = search_file( - args[0], ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") + myelffile = search_file(args.elf_file, + ldlibpath + ":/usr/lib64:/lib64:/usr/lib:/lib") if myelffile is None: print("File not found")
Signature · 1 line
2.27.0
PATCH 9/11 dpdk-pmdinfo: do not use len(x) to test for empty
In reply to Stephen HemmingerThis fixes the following python lint warnings.usertools/dpdk-pmdinfo.py:188:11: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition) usertools/dpdk-pmdinfo.py:196:21: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition) usertools/dpdk-pmdinfo.py:302:11: C1801: Do not use len(SEQUENCE) to determine if a sequence is empty (len-as-condition)
--- usertools/dpdk-pmdinfo.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
usertools/dpdk-pmdinfo.py +3 -3
@@ -185,7 +185,7 @@ def findDate(self, content): return None def parse(self): - if len(self.contents) < 1: + if not len(self.contents): print("data/%s-pci.ids not found" % self.date) else: vendorID = ""
@@ -193,7 +193,7 @@ def parse(self): for l in self.contents: if l[0] == "#": continue - elif len(l.strip()) == 0: + elif not l.strip(): continue else: if l.find("\t\t") == 0:
@@ -299,7 +299,7 @@ def parse_pmd_info_string(self, mystring): except KeyError: continue - if len(pmdinfo["pci_ids"]) != 0: + if pmdinfo["pci_ids"]: print("PMD HW SUPPORT:") if pcidb is not None: self.pretty_print_pmdinfo(pmdinfo)
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH 09/11] dpdk-pmdinfo: do not use len(x) to test for empty
Quoted 15 lines · Stephen Hemminger
This fixes the following python lint warnings.
usertools/dpdk-pmdinfo.py:188:11: C1801: Do not uselen(SEQUENCE)to determine if a sequence is empty (len-as-condition)usertools/dpdk-pmdinfo.py:196:21: C1801: Do not uselen(SEQUENCE)to determine if a sequence is empty (len-as-condition)usertools/dpdk-pmdinfo.py:302:11: C1801: Do not uselen(SEQUENCE)to determine if a sequence is empty (len-as-condition)Signed-off-by Stephen Hemminger <[email protected]>--- usertools/dpdk-pmdinfo.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)usertools/dpdk-pmdinfo.py +0
-0
usertools/dpdk-pmdinfo.py def findDate(self, content): · line 188
Quoted 5 lines
usertools/dpdk-pmdinfo.py +1
-1@@ -185,7 +185,7 @@ def findDate(self, content): return None def parse(self): - if len(self.contents) < 1: + if not len(self.contents):
Does an empty sequence not directly resolve to false in python? If so, the len should be removable and just use "if not self.contents:"
Quoted 24 lines
usertools/dpdk-pmdinfo.py +2
-2print("data/%s-pci.ids not found" % self.date) else: vendorID = ""@@ -193,7 +193,7 @@ def parse(self): for l in self.contents: if l[0] == "#": continue - elif len(l.strip()) == 0: + elif not l.strip(): continue else: if l.find("\t\t") == 0:@@ -299,7 +299,7 @@ def parse_pmd_info_string(self, mystring): except KeyError: continue - if len(pmdinfo["pci_ids"]) != 0: + if pmdinfo["pci_ids"]: print("PMD HW SUPPORT:") if pcidb is not None: self.pretty_print_pmdinfo(pmdinfo)Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH 09/11] dpdk-pmdinfo: do not use len(x) to test for empty
Quoted 2 lines · On Mon, 7 Sep 2020 10:03:43 +0100 Bruce Richardson…
Does an empty sequence not directly resolve to false in python? If so, the len should be removable and just use "if not self.contents:"
Yes, len should be removed there
PATCH 10/11 dpdk-telemetry-client: fix some pylint warnings
In reply to Stephen HemmingerConvert comments to docstrings as appropriate.
Remove unnecessary paren in if statement.
Remove extra whitespace after print.
--- usertools/dpdk-telemetry-client.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
usertools/dpdk-telemetry-client.py +18 -10
@@ -32,7 +32,8 @@ def __del__(self): class Client: - def __init__(self): # Creates a client instance + def __init__(self): + ''' Creates a client instance ''' self.socket = Socket() self.file_path = None self.choice = None
@@ -45,16 +46,18 @@ def __del__(self): except: print("Error - Client could not be destroyed") - def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client + def getFilepath(self, file_path): + '''Gets arguments from Command-Line and assigns to instance of client''' self.file_path = file_path - def register(self): # Connects a client to DPDK-instance + def register(self): + '''Connects a client to DPDK-instance''' if os.path.exists(self.file_path): os.unlink(self.file_path) try: self.socket.recv_fd.bind(self.file_path) except socket.error as msg: - print ("Error - Socket binding error: " + str(msg) + "\n") + print("Error - Socket binding error: " + str(msg) + "\n") self.socket.recv_fd.settimeout(2) self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry") JSON = (API_REG + self.file_path + "\"}}")
@@ -63,16 +66,19 @@ def register(self): # Connects a client to DPDK-instance self.socket.recv_fd.listen(1) self.socket.client_fd = self.socket.recv_fd.accept()[0] - def unregister(self): # Unregister a given client + def unregister(self): + ''' Unregister a given client ''' self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode()) self.socket.client_fd.close() - def requestMetrics(self): # Requests metrics for given client + def requestMetrics(self): + ''' Requests metrics for given client ''' self.socket.client_fd.send(METRICS_REQ.encode()) data = self.socket.client_fd.recv(BUFFER_SIZE).decode() print("\nResponse: \n", data) - def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client + def repeatedlyRequestMetrics(self, sleep_time): + ''' Recursively requests metrics for given client ''' print("\nPlease enter the number of times you'd like to continuously request Metrics:") n_requests = int(input("\n:")) print("\033[F") #Removes the user input from screen, cleans it up
@@ -81,12 +87,14 @@ def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics f self.requestMetrics() time.sleep(sleep_time) - def requestGlobalMetrics(self): #Requests global metrics for given client + def requestGlobalMetrics(self): + ''' Requests global metrics for given client ''' self.socket.client_fd.send(GLOBAL_METRICS_REQ.encode()) data = self.socket.client_fd.recv(BUFFER_SIZE).decode() print("\nResponse: \n", data) - def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script + def interactiveMenu(self, sleep_time): + ''' Creates Interactive menu within the script ''' while self.choice != 4: print("\nOptions Menu") print("[1] Send for Metrics for all ports")
@@ -116,7 +124,7 @@ def interactiveMenu(self, sleep_time): # Creates Interactive menu within the scr sleep_time = 1 file_path = "" - if (len(sys.argv) == 2): + if len(sys.argv) == 2: file_path = sys.argv[1] else: print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH 10/11] dpdk-telemetry-client: fix some pylint warnings
Quoted 9 lines · Stephen Hemminger
Convert comments to docstrings as appropriate.
Remove unnecessary paren in if statement.
Remove extra whitespace after print.Signed-off-by Stephen Hemminger <[email protected]>--- usertools/dpdk-telemetry-client.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
PATCH v2 1/1 dpdk-telemetry-client: fix some pylint warnings
In reply to Stephen HemmingerConvert comments to docstrings as appropriate.
Remove unnecessary paren in if statement.
Remove extra whitespace after print.
Remove extra semicolon; this is not C.
---
v2 - fix a few more warnings.
split this patch from the bundle
usertools/dpdk-telemetry-client.py | 34 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-)
usertools/dpdk-telemetry-client.py +21 -13
@@ -16,7 +16,7 @@ DEFAULT_FP = "/var/run/dpdk/default_client" class Socket: - + ''' Opens AF_UNIX sockets to telemetry server ''' def __init__(self): self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
@@ -31,8 +31,9 @@ def __del__(self): print("Error - Sockets could not be closed") class Client: - - def __init__(self): # Creates a client instance + ''' Calls for communication with telemetry server ''' + def __init__(self): + ''' Creates a client instance ''' self.socket = Socket() self.file_path = None self.choice = None
@@ -41,20 +42,22 @@ def __init__(self): # Creates a client instance def __del__(self): try: if self.unregistered == 0: - self.unregister(); + self.unregister() except: print("Error - Client could not be destroyed") - def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client + def getFilepath(self, file_path): + '''Gets arguments from Command-Line and assigns to instance of client''' self.file_path = file_path - def register(self): # Connects a client to DPDK-instance + def register(self): + '''Connects a client to DPDK-instance''' if os.path.exists(self.file_path): os.unlink(self.file_path) try: self.socket.recv_fd.bind(self.file_path) except socket.error as msg: - print ("Error - Socket binding error: " + str(msg) + "\n") + print("Error - Socket binding error: " + str(msg) + "\n") self.socket.recv_fd.settimeout(2) self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry") JSON = (API_REG + self.file_path + "\"}}")
@@ -63,16 +66,19 @@ def register(self): # Connects a client to DPDK-instance self.socket.recv_fd.listen(1) self.socket.client_fd = self.socket.recv_fd.accept()[0] - def unregister(self): # Unregister a given client + def unregister(self): + ''' Unregister a given client ''' self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode()) self.socket.client_fd.close() - def requestMetrics(self): # Requests metrics for given client + def requestMetrics(self): + ''' Requests metrics for given client ''' self.socket.client_fd.send(METRICS_REQ.encode()) data = self.socket.client_fd.recv(BUFFER_SIZE).decode() print("\nResponse: \n", data) - def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client + def repeatedlyRequestMetrics(self, sleep_time): + ''' Recursively requests metrics for given client ''' print("\nPlease enter the number of times you'd like to continuously request Metrics:") n_requests = int(input("\n:")) print("\033[F") #Removes the user input from screen, cleans it up
@@ -81,12 +87,14 @@ def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics f self.requestMetrics() time.sleep(sleep_time) - def requestGlobalMetrics(self): #Requests global metrics for given client + def requestGlobalMetrics(self): + ''' Requests global metrics for given client ''' self.socket.client_fd.send(GLOBAL_METRICS_REQ.encode()) data = self.socket.client_fd.recv(BUFFER_SIZE).decode() print("\nResponse: \n", data) - def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script + def interactiveMenu(self, sleep_time): + ''' Creates Interactive menu within the script ''' while self.choice != 4: print("\nOptions Menu") print("[1] Send for Metrics for all ports")
@@ -116,7 +124,7 @@ def interactiveMenu(self, sleep_time): # Creates Interactive menu within the scr sleep_time = 1 file_path = "" - if (len(sys.argv) == 2): + if len(sys.argv) == 2: file_path = sys.argv[1] else: print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH v2] dpdk-telemetry-client: fix some pylint warnings
04/11/2020 08:00, Stephen Hemminger:
Quoted 1 line
Convert comments to docstrings as appropriate.
Why inserting some spaces inside the docstrings?
Quoted 6 lines
Remove unnecessary paren in if statement.
Remove extra whitespace after print.
Remove extra semicolon; this is not C.Signed-off-by Stephen Hemminger <[email protected]>Acked-by Bruce Richardson <[email protected]>
Some issues are remaining:
pycodestyle usertools/dpdk-telemetry-client.pyusertools/dpdk-telemetry-client.py:12:80: E501 line too long (82 > 79 characters) usertools/dpdk-telemetry-client.py:14:80: E501 line too long (80 > 79 characters) usertools/dpdk-telemetry-client.py:15:80: E501 line too long (86 > 79 characters) usertools/dpdk-telemetry-client.py:18:1: E302 expected 2 blank lines, found 1 usertools/dpdk-telemetry-client.py:30:9: E722 do not use bare 'except' usertools/dpdk-telemetry-client.py:33:1: E302 expected 2 blank lines, found 1 usertools/dpdk-telemetry-client.py:46:9: E722 do not use bare 'except' usertools/dpdk-telemetry-client.py:50:80: E501 line too long (80 > 79 characters) usertools/dpdk-telemetry-client.py:71:80: E501 line too long (82 > 79 characters) usertools/dpdk-telemetry-client.py:82:80: E501 line too long (95 > 79 characters) usertools/dpdk-telemetry-client.py:84:24: E261 at least two spaces before inline comment usertools/dpdk-telemetry-client.py:84:25: E262 inline comment should start with '# ' usertools/dpdk-telemetry-client.py:107:32: E261 at least two spaces before inline comment usertools/dpdk-telemetry-client.py:107:33: E262 inline comment should start with '# ' usertools/dpdk-telemetry-client.py:107:80: E501 line too long (80 > 79 characters) usertools/dpdk-telemetry-client.py:120:13: E722 do not use bare 'except' usertools/dpdk-telemetry-client.py:123:1: E305 expected 2 blank lines after class or function definition, found 1
PATCH 11/11 dpdk-devbind: use argparse instead of getopt
In reply to Stephen HemmingerUsing the python standard argument parser instead of C library style getopt gives a number of advantages such as checking for conflicting arguments, restricting choices, and automatically generating help messages.
The result is similar to the original the only visible change is that some of the help messages are now less wordy.
--- usertools/dpdk-devbind.py | 196 +++++++++++++++++--------------------- 1 file changed, 85 insertions(+), 111 deletions(-)
usertools/dpdk-devbind.py +85 -111
@@ -5,8 +5,8 @@ import sys import os -import getopt import subprocess +import argparse from os.path import exists, abspath, dirname, basename # The PCI base class for all devices
@@ -72,76 +72,6 @@ args = [] -def usage(): - '''Print usage information for the program''' - argv0 = basename(sys.argv[0]) - print(""" -Usage: ------- - - %(argv0)s [options] DEVICE1 DEVICE2 .... - -where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax -or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may -also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc. - -Options: - --help, --usage: - Display usage information and quit - - -s, --status: - Print the current status of all known network, crypto, event - and mempool devices. - For each device, it displays the PCI domain, bus, slot and function, - along with a text description of the device. Depending upon whether the - device is being used by a kernel driver, the igb_uio driver, or no - driver, other relevant information will be displayed: - * the Linux interface name e.g. if=eth0 - * the driver being used e.g. drv=igb_uio - * any suitable drivers not currently using that device - e.g. unused=igb_uio - NOTE: if this flag is passed along with a bind/unbind option, the - status display will always occur after the other operations have taken - place. - - --status-dev: - Print the status of given device group. Supported device groups are: - "net", "baseband", "crypto", "event", "mempool" and "compress" - - -b driver, --bind=driver: - Select the driver to use or \"none\" to unbind the device - - -u, --unbind: - Unbind a device (Equivalent to \"-b none\") - - --force: - By default, network devices which are used by Linux - as indicated by - having routes in the routing table - cannot be modified. Using the - --force flag overrides this behavior, allowing active links to be - forcibly unbound. - WARNING: This can lead to loss of network connection and should be used - with caution. - -Examples: ---------- - -To display current device status: - %(argv0)s --status - -To display current network device status: - %(argv0)s --status-dev net - -To bind eth1 from the current driver and move to use igb_uio - %(argv0)s --bind=igb_uio eth1 - -To unbind 0000:01:00.0 from using any driver - %(argv0)s -u 0000:01:00.0 - -To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver - %(argv0)s -b ixgbe 02:00.0 02:00.1 - - """ % locals()) # replace items from local variables - # check if a specific kernel module is loaded def module_is_loaded(module): global loaded_modules
@@ -642,38 +572,93 @@ def parse_args(): global status_dev global force_flag global args - if len(sys.argv) <= 1: - usage() - sys.exit(0) - try: - opts, args = getopt.getopt(sys.argv[1:], "b:us", - ["help", "usage", "status", "status-dev=", - "force", "bind=", "unbind", ]) - except getopt.GetoptError as error: - print(str(error)) - print("Run '%s --usage' for further information" % sys.argv[0]) + parser = argparse.ArgumentParser( + description='Utility to bind and unbind devices from Linux kernel', + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=""" +Examples: +--------- + +To display current device status: + %(prog)s --status + +To display current network device status: + %(prog)s --status-dev net + +To bind eth1 from the current driver and move to use vfio-pci + %(prog)s --bind=vfio-pci eth1 + +To unbind 0000:01:00.0 from using any driver + %(prog)s -u 0000:01:00.0 + +To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver + %(prog)s -b ixgbe 02:00.0 02:00.1 +""") + + parser.add_argument( + '-s', + '--status', + action='store_true', + help="Print the current status of all known devices.") + parser.add_argument( + '--status-dev', + help="Print the status of given device group.", + choices=['net', 'baseband', 'crypto', 'event', 'mempool', 'compress']) + bind_group = parser.add_mutually_exclusive_group() + bind_group.add_argument( + '-b', + '--bind', + metavar='DRIVER', + help="Select the driver to use or \"none\" to unbind the device") + bind_group.add_argument( + '-u', + '--unbind', + action='store_true', + help="Unbind a device (equivalent to \"-b none\")") + parser.add_argument( + '--force', + action='store_true', + help=""" +Override restriction on binding devices in use by Linux" +WARNING: This can lead to loss of network connection and should be used with caution. +""") + parser.add_argument( + 'devices', + metavar='DEVICE', + nargs='*', + help=""" +Device specified as PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax. +For devices bound to Linux kernel drivers, they may be referred to by interface name. +""") + + opt = parser.parse_args() + + if opt.status_dev: + status_flag = True + status_dev = opt.status_dev + if opt.status: + status_flag = True + status_dev = "all" + if opt.force: + force_flag = True + if opt.bind: + b_flag = opt.bind + elif opt.unbind: + b_flag = "none" + args = opt.devices + + if not b_flag and not status_flag: + print("Error: No action specified for devices. " + "Please give a --bind, --ubind or --status option", + file=sys.stderr) + parser.print_usage() sys.exit(1) - for opt, arg in opts: - if opt == "--help" or opt == "--usage": - usage() - sys.exit(0) - if opt == "--status-dev": - status_flag = True - status_dev = arg - if opt == "--status" or opt == "-s": - status_flag = True - status_dev = "all" - if opt == "--force": - force_flag = True - if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind": - if b_flag is not None: - sys.exit("Error: binding and unbinding are mutually exclusive") - if opt == "-u" or opt == "--unbind": - b_flag = "none" - else: - b_flag = arg + if b_flag and not args: + print("Error: No devices specified.", file=sys.stderr) + parser.print_usage() + sys.exit(1) def do_arg_actions():
@@ -683,17 +668,6 @@ def do_arg_actions(): global force_flag global args - if b_flag is None and not status_flag: - print("Error: No action specified for devices. " - "Please give a -b or -u option", file=sys.stderr) - usage() - sys.exit(1) - - if b_flag is not None and len(args) == 0: - print("Error: No devices specified.", file=sys.stderr) - usage() - sys.exit(1) - if b_flag == "none" or b_flag == "None": unbind_all(args, force_flag) elif b_flag is not None:
Signature · 1 line
2.27.0
v2 cover dpdk-devbind: python lint cleanups
This addresses several warnings from python3 lint. Split out from earlier patchset
Stephen Hemminger (6):
dpdk-devbind: use argparse instead of getopt
dpdk-devbind: fix indentation
dpdk-devbind: fix python lint warnings for imports
dpdk-devbind: do not use len(x) to test for empty
dpdk-devbind: fix unnecessary else after return
dpdk-devbind: use in to test for multiple strings
usertools/dpdk-devbind.py | 315 +++++++++++++++++--------------------- 1 file changed, 142 insertions(+), 173 deletions(-)
Signature · 1 line
2.27.0
PATCH v2 1/6 dpdk-devbind: use argparse instead of getopt
Using the python standard argument parser instead of C library style getopt gives a number of advantages such as checking for conflicting arguments, restricting choices, and automatically generating help messages.
The result is similar to the original the only visible change is that some of the help messages are now less wordy.
--- usertools/dpdk-devbind.py | 203 ++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 117 deletions(-)
usertools/dpdk-devbind.py +86 -117
@@ -5,8 +5,9 @@ import sys import os -import getopt import subprocess +import argparse + from glob import glob from os.path import exists, abspath, dirname, basename from os.path import join as path_join
@@ -82,82 +83,6 @@ force_flag = False args = [] - -def usage(): - '''Print usage information for the program''' - argv0 = basename(sys.argv[0]) - print(""" -Usage: ------- - - %(argv0)s [options] DEVICE1 DEVICE2 .... - -where DEVICE1, DEVICE2 etc, are specified via PCI "domain:bus:slot.func" syntax -or "bus:slot.func" syntax. For devices bound to Linux kernel drivers, they may -also be referred to by Linux interface name e.g. eth0, eth1, em0, em1, etc. -If devices are specified using PCI <domain:>bus:device:func format, then -shell wildcards and ranges may be used, e.g. 80:04.*, 80:04.[0-3] - -Options: - --help, --usage: - Display usage information and quit - - -s, --status: - Print the current status of all known network, crypto, event - and mempool devices. - For each device, it displays the PCI domain, bus, slot and function, - along with a text description of the device. Depending upon whether the - device is being used by a kernel driver, the igb_uio driver, or no - driver, other relevant information will be displayed: - * the Linux interface name e.g. if=eth0 - * the driver being used e.g. drv=igb_uio - * any suitable drivers not currently using that device - e.g. unused=igb_uio - NOTE: if this flag is passed along with a bind/unbind option, the - status display will always occur after the other operations have taken - place. - - --status-dev: - Print the status of given device group. Supported device groups are: - "net", "baseband", "crypto", "event", "mempool" and "compress" - - -b driver, --bind=driver: - Select the driver to use or \"none\" to unbind the device - - -u, --unbind: - Unbind a device (Equivalent to \"-b none\") - - --force: - By default, network devices which are used by Linux - as indicated by - having routes in the routing table - cannot be modified. Using the - --force flag overrides this behavior, allowing active links to be - forcibly unbound. - WARNING: This can lead to loss of network connection and should be used - with caution. - -Examples: ---------- - -To display current device status: - %(argv0)s --status - -To display current network device status: - %(argv0)s --status-dev net - -To bind eth1 from the current driver and move to use igb_uio - %(argv0)s --bind=igb_uio eth1 - -To unbind 0000:01:00.0 from using any driver - %(argv0)s -u 0000:01:00.0 - -To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver - %(argv0)s -b ixgbe 02:00.0 02:00.1 - -To bind all functions on device 0000:02:00 to ixgbe kernel driver - %(argv0)s -b ixgbe 02:00.* - - """ % locals()) # replace items from local variables - # check if a specific kernel module is loaded def module_is_loaded(module): global loaded_modules
@@ -677,38 +602,93 @@ def parse_args(): global status_dev global force_flag global args - if len(sys.argv) <= 1: - usage() - sys.exit(0) - try: - opts, args = getopt.getopt(sys.argv[1:], "b:us", - ["help", "usage", "status", "status-dev=", - "force", "bind=", "unbind", ]) - except getopt.GetoptError as error: - print(str(error)) - print("Run '%s --usage' for further information" % sys.argv[0]) + parser = argparse.ArgumentParser( + description='Utility to bind and unbind devices from Linux kernel', + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=""" +Examples: +--------- + +To display current device status: + %(prog)s --status + +To display current network device status: + %(prog)s --status-dev net + +To bind eth1 from the current driver and move to use vfio-pci + %(prog)s --bind=vfio-pci eth1 + +To unbind 0000:01:00.0 from using any driver + %(prog)s -u 0000:01:00.0 + +To bind 0000:02:00.0 and 0000:02:00.1 to the ixgbe kernel driver + %(prog)s -b ixgbe 02:00.0 02:00.1 +""") + + parser.add_argument( + '-s', + '--status', + action='store_true', + help="Print the current status of all known devices.") + parser.add_argument( + '--status-dev', + help="Print the status of given device group.", + choices=['net', 'baseband', 'crypto', 'event', 'mempool', 'compress']) + bind_group = parser.add_mutually_exclusive_group() + bind_group.add_argument( + '-b', + '--bind', + metavar='DRIVER', + help="Select the driver to use or \"none\" to unbind the device") + bind_group.add_argument( + '-u', + '--unbind', + action='store_true', + help="Unbind a device (equivalent to \"-b none\")") + parser.add_argument( + '--force', + action='store_true', + help=""" +Override restriction on binding devices in use by Linux" +WARNING: This can lead to loss of network connection and should be used with caution. +""") + parser.add_argument( + 'devices', + metavar='DEVICE', + nargs='*', + help=""" +Device specified as PCI "domain:bus:slot.func" syntax or "bus:slot.func" syntax. +For devices bound to Linux kernel drivers, they may be referred to by interface name. +""") + + opt = parser.parse_args() + + if opt.status_dev: + status_flag = True + status_dev = opt.status_dev + if opt.status: + status_flag = True + status_dev = "all" + if opt.force: + force_flag = True + if opt.bind: + b_flag = opt.bind + elif opt.unbind: + b_flag = "none" + args = opt.devices + + if not b_flag and not status_flag: + print("Error: No action specified for devices. " + "Please give a --bind, --ubind or --status option", + file=sys.stderr) + parser.print_usage() sys.exit(1) - for opt, arg in opts: - if opt == "--help" or opt == "--usage": - usage() - sys.exit(0) - if opt == "--status-dev": - status_flag = True - status_dev = arg - if opt == "--status" or opt == "-s": - status_flag = True - status_dev = "all" - if opt == "--force": - force_flag = True - if opt == "-b" or opt == "-u" or opt == "--bind" or opt == "--unbind": - if b_flag is not None: - sys.exit("Error: binding and unbinding are mutually exclusive") - if opt == "-u" or opt == "--unbind": - b_flag = "none" - else: - b_flag = arg + if b_flag and not args: + print("Error: No devices specified.", file=sys.stderr) + parser.print_usage() + sys.exit(1) # resolve any PCI globs in the args new_args = []
@@ -723,17 +703,6 @@ def do_arg_actions(): global force_flag global args - if b_flag is None and not status_flag: - print("Error: No action specified for devices. " - "Please give a -b or -u option", file=sys.stderr) - usage() - sys.exit(1) - - if b_flag is not None and len(args) == 0: - print("Error: No devices specified.", file=sys.stderr) - usage() - sys.exit(1) - if b_flag == "none" or b_flag == "None": unbind_all(args, force_flag) elif b_flag is not None:
Signature · 1 line
2.27.0
PATCH v2 2/6 dpdk-devbind: fix indentation
In reply to Stephen HemmingerPython lint complains about identation and missing spaces around commas in this script.
--- usertools/dpdk-devbind.py | 53 ++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 26 deletions(-)
usertools/dpdk-devbind.py +27 -26
@@ -14,15 +14,15 @@ # The PCI base class for all devices network_class = {'Class': '02', 'Vendor': None, 'Device': None, - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} acceleration_class = {'Class': '12', 'Vendor': None, 'Device': None, 'SVendor': None, 'SDevice': None} ifpga_class = {'Class': '12', 'Vendor': '8086', 'Device': '0b30', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} encryption_class = {'Class': '10', 'Vendor': None, 'Device': None, - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} intel_processor_class = {'Class': '0b', 'Vendor': '8086', 'Device': None, - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} cavium_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a04b,a04d', 'SVendor': None, 'SDevice': None} cavium_fpa = {'Class': '08', 'Vendor': '177d', 'Device': 'a053',
@@ -34,29 +34,30 @@ cavium_zip = {'Class': '12', 'Vendor': '177d', 'Device': 'a037', 'SVendor': None, 'SDevice': None} avp_vnic = {'Class': '05', 'Vendor': '1af4', 'Device': '1110', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} octeontx2_sso = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f9,a0fa', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} octeontx2_npa = {'Class': '08', 'Vendor': '177d', 'Device': 'a0fb,a0fc', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} octeontx2_dma = {'Class': '08', 'Vendor': '177d', 'Device': 'a081', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} octeontx2_ree = {'Class': '08', 'Vendor': '177d', 'Device': 'a0f4', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} -intel_ioat_bdw = {'Class': '08', 'Vendor': '8086', 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f', - 'SVendor': None, 'SDevice': None} +intel_ioat_bdw = {'Class': '08', 'Vendor': '8086', + 'Device': '6f20,6f21,6f22,6f23,6f24,6f25,6f26,6f27,6f2e,6f2f', + 'SVendor': None, 'SDevice': None} intel_ioat_skx = {'Class': '08', 'Vendor': '8086', 'Device': '2021', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} intel_ioat_icx = {'Class': '08', 'Vendor': '8086', 'Device': '0b00', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} intel_idxd_spr = {'Class': '08', 'Vendor': '8086', 'Device': '0b25', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} intel_ntb_skx = {'Class': '06', 'Vendor': '8086', 'Device': '201c', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} intel_ntb_icx = {'Class': '06', 'Vendor': '8086', 'Device': '347e', - 'SVendor': None, 'SDevice': None} + 'SVendor': None, 'SDevice': None} network_devices = [network_class, cavium_pkx, avp_vnic, ifpga_class] baseband_devices = [acceleration_class]
@@ -206,7 +207,7 @@ def get_device_details(devices_type): route = subprocess.check_output(["ip", "-o", "route"]) # filter out all lines for 169.254 routes route = "\n".join(filter(lambda ln: not ln.startswith("169.254"), - route.decode().splitlines())) + route.decode().splitlines())) rt_info = route.split() for i in range(len(rt_info) - 1): if rt_info[i] == "dev":
@@ -366,8 +367,8 @@ def bind_one(dev_id, driver, force): return try: # Convert Device and Vendor Id to int to write to new_id - f.write("%04x %04x" % (int(dev["Vendor"],16), - int(dev["Device"], 16))) + f.write("%04x %04x" % (int(dev["Vendor"], 16), + int(dev["Device"], 16))) f.close() except: print("Error: bind failed for %s - Cannot write new PCI ID to "
@@ -409,13 +410,13 @@ def bind_one(dev_id, driver, force): f = open(filename, "w") except: sys.exit("Error: unbind failed for %s - Cannot open %s" - % (dev_id, filename)) + % (dev_id, filename)) try: f.write("\00") f.close() except: sys.exit("Error: unbind failed for %s - Cannot open %s" - % (dev_id, filename)) + % (dev_id, filename)) def unbind_all(dev_list, force=False):
@@ -456,7 +457,7 @@ def bind_all(dev_list, driver, force=False): pass # check if we're attempting to bind to a driver that isn't loaded - if not module_is_loaded(driver.replace('-','_')): + if not module_is_loaded(driver.replace('-', '_')): sys.exit("Error: Driver '%s' is not loaded." % driver) try:
@@ -501,9 +502,9 @@ def display_devices(title, dev_list, extra_params=None): for dev in dev_list: if extra_params is not None: strings.append("%s '%s %s' %s" % (dev["Slot"], - dev["Device_str"], - dev["Device"], - extra_params % dev)) + dev["Device_str"], + dev["Device"], + extra_params % dev)) else: strings.append("%s '%s'" % (dev["Slot"], dev["Device_str"])) # sort before printing, so that the entries appear in PCI order
@@ -573,7 +574,7 @@ def show_status(): show_device_status(mempool_devices, "Mempool") if status_dev == "compress" or status_dev == "all": - show_device_status(compress_devices , "Compress") + show_device_status(compress_devices, "Compress") if status_dev == "misc" or status_dev == "all": show_device_status(misc_devices, "Misc (rawdev)")
Signature · 1 line
2.27.0
PATCH v2 3/6 dpdk-devbind: fix python lint warnings for imports
In reply to Stephen HemmingerAddress python lint complaints about unused imports.
--- usertools/dpdk-devbind.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
usertools/dpdk-devbind.py +4 -4
@@ -9,7 +9,7 @@ import argparse from glob import glob -from os.path import exists, abspath, dirname, basename +from os.path import exists, basename from os.path import join as path_join # The PCI base class for all devices
@@ -342,7 +342,7 @@ def bind_one(dev_id, driver, force): # of unbinding those devices if driver in dpdk_drivers: filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id - if os.path.exists(filename): + if exists(filename): try: f = open(filename, "w") except:
@@ -405,7 +405,7 @@ def bind_one(dev_id, driver, force): # Before unbinding it, overwrite driver_override with empty string so that # the device can be bound to any other driver filename = "/sys/bus/pci/devices/%s/driver_override" % dev_id - if os.path.exists(filename): + if exists(filename): try: f = open(filename, "w") except:
@@ -473,7 +473,7 @@ def bind_all(dev_list, driver, force=False): # that are not bound to any other driver could be bound even if no one has # asked them to. hence, we check the list of drivers again, and see if # some of the previously-unbound devices were erroneously bound. - if not os.path.exists("/sys/bus/pci/devices/%s/driver_override" % d): + if not exists("/sys/bus/pci/devices/%s/driver_override" % d): for d in devices.keys(): # skip devices that were already bound or that we know should be bound if "Driver_str" in devices[d] or d in dev_list:
Signature · 1 line
2.27.0
PATCH v2 4/6 dpdk-devbind: do not use len(x) to test for empty
In reply to Stephen HemmingerPython lint warns about using len(SEQUENCE) to determine if sequence is empty.
--- usertools/dpdk-devbind.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
usertools/dpdk-devbind.py +7 -7
@@ -143,7 +143,7 @@ def get_pci_device_details(dev_id, probe_lspci): extra_info = subprocess.check_output(["lspci", "-vmmks", dev_id]).splitlines() # parse lspci details for line in extra_info: - if len(line) == 0: + if not line: continue name, value = line.decode("utf8").split("\t", 1) name = name.strip(":") + "_str"
@@ -178,7 +178,7 @@ def get_device_details(devices_type): dev = {} dev_lines = subprocess.check_output(["lspci", "-Dvmmnnk"]).splitlines() for dev_line in dev_lines: - if len(dev_line) == 0: + if not dev_line: if device_type_match(dev, devices_type): # Replace "Driver" with "Driver_str" to have consistency of # of dictionary key names
@@ -193,7 +193,7 @@ def get_device_details(devices_type): else: name, value = dev_line.decode("utf8").split("\t", 1) value_list = value.rsplit(' ', 1) - if len(value_list) > 1: + if value_list: # String stored in <name>_str dev[name.rstrip(":") + '_str'] = value_list[0] # Numeric IDs
@@ -496,7 +496,7 @@ def display_devices(title, dev_list, extra_params=None): strings = [] # this holds the strings to print. We sort before printing print("\n%s" % title) print("="*len(title)) - if len(dev_list) == 0: + if not dev_list: strings.append("<none>") else: for dev in dev_list:
@@ -539,17 +539,17 @@ def show_device_status(devices_type, device_name, if_field=False): return # print each category separately, so we can clearly see what's used by DPDK - if len(dpdk_drv) != 0: + if dpdk_drv: display_devices("%s devices using DPDK-compatible driver" % device_name, dpdk_drv, "drv=%(Driver_str)s unused=%(Module_str)s") - if len(kernel_drv) != 0: + if kernel_drv: if_text = "" if if_field: if_text = "if=%(Interface)s " display_devices("%s devices using kernel driver" % device_name, kernel_drv, if_text + "drv=%(Driver_str)s " "unused=%(Module_str)s %(Active)s") - if len(no_drv) != 0: + if no_drv: display_devices("Other %s devices" % device_name, no_drv, "unused=%(Module_str)s")
Signature · 1 line
2.27.0
PATCH v2 5/6 dpdk-devbind: fix unnecessary else after return
In reply to Stephen HemmingerPython lint complains about
Unnecessary "else" after "return" (no-else-return)
--- usertools/dpdk-devbind.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
usertools/dpdk-devbind.py +9 -10
@@ -274,13 +274,13 @@ def dev_id_from_dev_name(dev_name): if dev_name in devices: return dev_name # check if it's an index just missing the domain part - elif "0000:" + dev_name in devices: + if "0000:" + dev_name in devices: return "0000:" + dev_name - else: - # check if it's an interface name, e.g. eth1 - for d in devices.keys(): - if dev_name in devices[d]["Interface"].split(","): - return devices[d]["Slot"] + + # check if it's an interface name, e.g. eth1 + for d in devices.keys(): + if dev_name in devices[d]["Interface"].split(","): + return devices[d]["Slot"] # if nothing else matches - error raise ValueError("Unknown device: %s. " "Please specify device in \"bus:slot.func\" format" % dev_name)
@@ -329,10 +329,9 @@ def bind_one(dev_id, driver, force): print("Notice: %s already bound to driver %s, skipping" % (dev_id, driver), file=sys.stderr) return - else: - saved_driver = dev["Driver_str"] - unbind_one(dev_id, force) - dev["Driver_str"] = "" # clear driver string + saved_driver = dev["Driver_str"] + unbind_one(dev_id, force) + dev["Driver_str"] = "" # clear driver string # For kernels >= 3.15 driver_override can be used to specify the driver # for a device rather than relying on the driver to provide a positive
Signature · 1 line
2.27.0
PATCH v2 6/6 dpdk-devbind: use in to test for multiple strings
In reply to Stephen HemmingerPython lint suggests using in instead of multiple comparisons.
--- usertools/dpdk-devbind.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
usertools/dpdk-devbind.py +9 -9
@@ -557,28 +557,28 @@ def show_status(): Displays to the user what devices are bound to the igb_uio driver, the kernel driver or to no driver''' - if status_dev == "net" or status_dev == "all": + if status_dev in ["net", "all"]: show_device_status(network_devices, "Network", if_field=True) - if status_dev == "baseband" or status_dev == "all": + if status_dev in ["baseband", "all"]: show_device_status(baseband_devices, "Baseband") - if status_dev == "crypto" or status_dev == "all": + if status_dev in ["crypto", "all"]: show_device_status(crypto_devices, "Crypto") - if status_dev == "event" or status_dev == "all": + if status_dev in ["event", "all"]: show_device_status(eventdev_devices, "Eventdev") - if status_dev == "mempool" or status_dev == "all": + if status_dev in ["mempool", "all"]: show_device_status(mempool_devices, "Mempool") - if status_dev == "compress" or status_dev == "all": + if status_dev in ["compress", "all"]: show_device_status(compress_devices, "Compress") - if status_dev == "misc" or status_dev == "all": + if status_dev in ["misc", "all"]: show_device_status(misc_devices, "Misc (rawdev)") - if status_dev == "regex" or status_dev == "all": + if status_dev in ["regex", "all"]: show_device_status(regex_devices, "Regex")
@@ -703,7 +703,7 @@ def do_arg_actions(): global force_flag global args - if b_flag == "none" or b_flag == "None": + if b_flag in ["none", "None"]: unbind_all(args, force_flag) elif b_flag is not None: bind_all(args, b_flag, force_flag)
Signature · 1 line
2.27.0
Re: [dpdk-dev] [PATCH v2 0/6] dpdk-devbind: python lint cleanups
In reply to Stephen HemmingerQuoted 11 lines · Stephen Hemminger
This addresses several warnings from python3 lint. Split out from earlier patchset
Stephen Hemminger (6):
dpdk-devbind: use argparse instead of getopt
dpdk-devbind: fix indentation
dpdk-devbind: fix python lint warnings for imports
dpdk-devbind: do not use len(x) to test for empty
dpdk-devbind: fix unnecessary else after return
dpdk-devbind: use in to test for multiple strings
Quoted 3 lines
usertools/dpdk-devbind.py | 315 +++++++++++++++++--------------------- 1 file changed, 142 insertions(+), 173 deletions(-)
Thomas Monjalon
AppliedRe: [dpdk-dev] [PATCH v2 0/6] dpdk-devbind: python lint cleanups
04/11/2020 10:28, Bruce Richardson:
Quoted 12 lines · Stephen Hemminger
Quoted 11 lines
This addresses several warnings from python3 lint. Split out from earlier patchset
Stephen Hemminger (6):
dpdk-devbind: use argparse instead of getopt
dpdk-devbind: fix indentation
dpdk-devbind: fix python lint warnings for imports
dpdk-devbind: do not use len(x) to test for empty
dpdk-devbind: fix unnecessary else after return
dpdk-devbind: use in to test for multiple strings
Quoted 4 lines
Quoted 3 lines
usertools/dpdk-devbind.py | 315 +++++++++++++++++--------------------- 1 file changed, 142 insertions(+), 173 deletions(-)Series-acked-by Bruce Richardson <[email protected]>
Applied, thanks
Re: [dpdk-dev] [PATCH v2 0/6] dpdk-devbind: python lint cleanups
In reply to Stephen HemmingerQuoted 11 lines · On Tue, 3 Nov 2020 23:03:44 -0800 Stephen Hemminger…
This addresses several warnings from python3 lint. Split out from earlier patchset
Stephen Hemminger (6):
dpdk-devbind: use argparse instead of getopt
dpdk-devbind: fix indentation
dpdk-devbind: fix python lint warnings for imports
dpdk-devbind: do not use len(x) to test for empty
dpdk-devbind: fix unnecessary else after return
dpdk-devbind: use in to test for multiple strings
Quoted 3 lines
usertools/dpdk-devbind.py | 315 +++++++++++++++++--------------------- 1 file changed, 142 insertions(+), 173 deletions(-)
There are some typos in the commit messages.
Can send new version, or you can fix/leave them