usertools/devbind: conform to PEP8 recommended style

This fixes most of the warnings from the Flake8 style checker.
The ones remaining are long lines (we allow > 79 characters)
and a line break warning.  The line break style changed in later
versions of PEP 8 and the tool is not updated.

https://www.flake8rules.com/rules/W503.html

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
Tested-by: Bruce Richardson <bruce.richardson@intel.com>
This commit is contained in:
Stephen Hemminger 2021-09-01 14:27:07 -07:00 committed by Thomas Monjalon
parent b54403fd08
commit 62d3ce6043

View File

@ -93,6 +93,7 @@
force_flag = False force_flag = False
args = [] args = []
# check if a specific kernel module is loaded # check if a specific kernel module is loaded
def module_is_loaded(module): def module_is_loaded(module):
global loaded_modules global loaded_modules
@ -180,11 +181,13 @@ def get_pci_device_details(dev_id, probe_lspci):
return device return device
def clear_data(): def clear_data():
'''This function clears any old data''' '''This function clears any old data'''
global devices global devices
devices = {} devices = {}
def get_device_details(devices_type): def get_device_details(devices_type):
'''This function populates the "devices" dictionary. The keys used are '''This function populates the "devices" dictionary. The keys used are
the pci addresses (domain:bus:slot.func). The values are themselves the pci addresses (domain:bus:slot.func). The values are themselves
@ -284,6 +287,7 @@ def device_type_match(dev, devices_type):
return True return True
return False return False
def dev_id_from_dev_name(dev_name): def dev_id_from_dev_name(dev_name):
'''Take a device "name" - a string passed in by user to identify a NIC '''Take a device "name" - a string passed in by user to identify a NIC
device, and determine the device id - i.e. the domain:bus:slot.func - for device, and determine the device id - i.e. the domain:bus:slot.func - for
@ -323,9 +327,9 @@ def unbind_one(dev_id, force):
filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"] filename = "/sys/bus/pci/drivers/%s/unbind" % dev["Driver_str"]
try: try:
f = open(filename, "a") f = open(filename, "a")
except: except OSError as err:
sys.exit("Error: unbind failed for %s - Cannot open %s" % sys.exit("Error: unbind failed for %s - Cannot open %s: %s" %
(dev_id, filename)) (dev_id, filename, err))
f.write(dev_id) f.write(dev_id)
f.close() f.close()
@ -363,58 +367,58 @@ def bind_one(dev_id, driver, force):
if exists(filename): if exists(filename):
try: try:
f = open(filename, "w") f = open(filename, "w")
except: except OSError as err:
print("Error: bind failed for %s - Cannot open %s" print("Error: bind failed for %s - Cannot open %s: %s"
% (dev_id, filename), file=sys.stderr) % (dev_id, filename, err), file=sys.stderr)
return return
try: try:
f.write("%s" % driver) f.write("%s" % driver)
f.close() f.close()
except: except OSError as err:
print("Error: bind failed for %s - Cannot write driver %s to " print("Error: bind failed for %s - Cannot write driver %s to "
"PCI ID " % (dev_id, driver), file=sys.stderr) "PCI ID: %s" % (dev_id, driver, err), file=sys.stderr)
return return
# For kernels < 3.15 use new_id to add PCI id's to the driver # For kernels < 3.15 use new_id to add PCI id's to the driver
else: else:
filename = "/sys/bus/pci/drivers/%s/new_id" % driver filename = "/sys/bus/pci/drivers/%s/new_id" % driver
try: try:
f = open(filename, "w") f = open(filename, "w")
except: except OSError as err:
print("Error: bind failed for %s - Cannot open %s" print("Error: bind failed for %s - Cannot open %s: %s"
% (dev_id, filename), file=sys.stderr) % (dev_id, filename, err), file=sys.stderr)
return return
try: try:
# Convert Device and Vendor Id to int to write to new_id # Convert Device and Vendor Id to int to write to new_id
f.write("%04x %04x" % (int(dev["Vendor"], 16), f.write("%04x %04x" % (int(dev["Vendor"], 16),
int(dev["Device"], 16))) int(dev["Device"], 16)))
f.close() f.close()
except: except OSError as err:
print("Error: bind failed for %s - Cannot write new PCI ID to " print("Error: bind failed for %s - Cannot write new PCI ID to "
"driver %s" % (dev_id, driver), file=sys.stderr) "driver %s: %s" % (dev_id, driver, err), file=sys.stderr)
return return
# do the bind by writing to /sys # do the bind by writing to /sys
filename = "/sys/bus/pci/drivers/%s/bind" % driver filename = "/sys/bus/pci/drivers/%s/bind" % driver
try: try:
f = open(filename, "a") f = open(filename, "a")
except: except OSError as err:
print("Error: bind failed for %s - Cannot open %s" print("Error: bind failed for %s - Cannot open %s: %s"
% (dev_id, filename), file=sys.stderr) % (dev_id, filename, err), file=sys.stderr)
if saved_driver is not None: # restore any previous driver if saved_driver is not None: # restore any previous driver
bind_one(dev_id, saved_driver, force) bind_one(dev_id, saved_driver, force)
return return
try: try:
f.write(dev_id) f.write(dev_id)
f.close() f.close()
except: except OSError as err:
# for some reason, closing dev_id after adding a new PCI ID to new_id # for some reason, closing dev_id after adding a new PCI ID to new_id
# results in IOError. however, if the device was successfully bound, # results in IOError. however, if the device was successfully bound,
# we don't care for any errors and can safely ignore IOError # we don't care for any errors and can safely ignore IOError
tmp = get_pci_device_details(dev_id, True) tmp = get_pci_device_details(dev_id, True)
if "Driver_str" in tmp and tmp["Driver_str"] == driver: if "Driver_str" in tmp and tmp["Driver_str"] == driver:
return return
print("Error: bind failed for %s - Cannot bind to driver %s" print("Error: bind failed for %s - Cannot bind to driver %s: %s"
% (dev_id, driver), file=sys.stderr) % (dev_id, driver, err), file=sys.stderr)
if saved_driver is not None: # restore any previous driver if saved_driver is not None: # restore any previous driver
bind_one(dev_id, saved_driver, force) bind_one(dev_id, saved_driver, force)
return return
@ -426,15 +430,15 @@ def bind_one(dev_id, driver, force):
if exists(filename): if exists(filename):
try: try:
f = open(filename, "w") f = open(filename, "w")
except: except OSError as err:
sys.exit("Error: unbind failed for %s - Cannot open %s" sys.exit("Error: unbind failed for %s - Cannot open %s: %s"
% (dev_id, filename)) % (dev_id, filename, err))
try: try:
f.write("\00") f.write("\00")
f.close() f.close()
except: except OSError as err:
sys.exit("Error: unbind failed for %s - Cannot open %s" sys.exit("Error: unbind failed for %s - Cannot write %s: %s"
% (dev_id, filename)) % (dev_id, filename, err))
def unbind_all(dev_list, force=False): def unbind_all(dev_list, force=False):
@ -468,7 +472,7 @@ def bind_all(dev_list, driver, force=False):
dev_id_from_dev_name(driver) dev_id_from_dev_name(driver)
# if we've made it this far, this means that the "driver" was a valid # if we've made it this far, this means that the "driver" was a valid
# device string, so it's probably not a valid driver name. # device string, so it's probably not a valid driver name.
sys.exit("Error: Driver '%s' does not look like a valid driver. " \ sys.exit("Error: Driver '%s' does not look like a valid driver. "
"Did you forget to specify the driver to bind devices to?" % driver) "Did you forget to specify the driver to bind devices to?" % driver)
except ValueError: except ValueError:
# driver generated error - it's not a valid device ID, so all is well # driver generated error - it's not a valid device ID, so all is well
@ -498,8 +502,8 @@ def bind_all(dev_list, driver, force=False):
continue continue
# update information about this device # update information about this device
devices[d] = dict(devices[d].items() + devices[d] = dict(devices[d].items()
get_pci_device_details(d, True).items()) + get_pci_device_details(d, True).items())
# check if updated information indicates that the device was bound # check if updated information indicates that the device was bound
if "Driver_str" in devices[d]: if "Driver_str" in devices[d]:
@ -529,6 +533,7 @@ def display_devices(title, dev_list, extra_params=None):
strings.sort() strings.sort()
print("\n".join(strings)) # print one per line print("\n".join(strings)) # print one per line
def show_device_status(devices_type, device_name, if_field=False): def show_device_status(devices_type, device_name, if_field=False):
global dpdk_drivers global dpdk_drivers
kernel_drv = [] kernel_drv = []
@ -571,6 +576,7 @@ def show_device_status(devices_type, device_name, if_field=False):
display_devices("Other %s devices" % device_name, no_drv, display_devices("Other %s devices" % device_name, no_drv,
"unused=%(Module_str)s") "unused=%(Module_str)s")
def show_status(): def show_status():
'''Function called when the script is passed the "--status" option. '''Function called when the script is passed the "--status" option.
Displays to the user what devices are bound to the igb_uio driver, the Displays to the user what devices are bound to the igb_uio driver, the
@ -719,6 +725,7 @@ def parse_args():
new_args.extend(pci_glob(arg)) new_args.extend(pci_glob(arg))
args = new_args args = new_args
def do_arg_actions(): def do_arg_actions():
'''do the actual action requested by the user''' '''do the actual action requested by the user'''
global b_flag global b_flag
@ -768,5 +775,6 @@ def main():
get_device_details(misc_devices) get_device_details(misc_devices)
do_arg_actions() do_arg_actions()
if __name__ == "__main__": if __name__ == "__main__":
main() main()