diff --git a/nvmet/__init__.py b/nvmet/__init__.py index 5e2f525..facd019 100644 --- a/nvmet/__init__.py +++ b/nvmet/__init__.py @@ -1,2 +1,2 @@ -from .nvme import Root, Subsystem, Namespace, Port, Host, Referral, ANAGroup, Passthru, \ - DEFAULT_SAVE_FILE +from .nvme import Root, Subsystem, Namespace, Port, Host, Referral, ANAGroup, \ + Passthru, DEFAULT_SAVE_FILE # noqa: F401 diff --git a/nvmet/nvme.py b/nvmet/nvme.py index faf1eb2..2bc0cb8 100644 --- a/nvmet/nvme.py +++ b/nvmet/nvme.py @@ -84,7 +84,7 @@ def _create_in_cfs(self, mode): if not self.exists: try: os.mkdir(self.path) - except: + except Exception: raise CFSError("Could not create %s in configFS" % self.__class__.__name__) self.get_enable() @@ -110,7 +110,7 @@ def list_attrs(self, group, writable=None): names = [os.path.basename(name).split('_', 1)[1] for name in glob("%s/%s_*" % (self._path, group)) - if os.path.isfile(name)] + if os.path.isfile(name)] if writable is True: names = [name for name in names @@ -203,10 +203,10 @@ def delete(self): path = property(_get_path, doc="Get the configFS object path.") exists = property(_exists, - doc="Is True as long as the underlying configFS object exists. " - + "If the underlying configFS objects gets deleted " - + "either by calling the delete() method, or by any " - + "other means, it will be False.") + doc="Is True as long as the underlying configFS object" + + " exists. If the underlying configFS objects gets" + + " deleted either by calling the delete() method, or by" + + " any other means, it will be False.") def dump(self): d = {} @@ -261,7 +261,7 @@ def _modprobe(self, modname): try: kmod.Kmod().modprobe(modname) - except Exception as e: + except Exception: pass except ImportError: # Try the binary specified in /proc @@ -272,7 +272,7 @@ def _modprobe(self, modname): if modprobe_cmd: subprocess.run(shlex.split(modprobe_cmd) + [modname], check=False) - except Exception as e: + except Exception: pass def _list_subsystems(self): @@ -291,7 +291,7 @@ def _list_ports(self): yield Port(d, 'lookup') ports = property(_list_ports, - doc="Get the list of Ports.") + doc="Get the list of Ports.") def _list_hosts(self): self._check_self() @@ -489,7 +489,8 @@ def _list_allowed_hosts(self): for name in os.listdir("%s/allowed_hosts/" % self._path)] allowed_hosts = property(_list_allowed_hosts, - doc="Get the list of Allowed Hosts for the Subsystem.") + doc="Get the list of Allowed Hosts for the " + + "Subsystem.") def add_allowed_host(self, nqn): ''' @@ -658,6 +659,7 @@ def dump(self): d['ana_grpid'] = self.grpid return d + class Passthru(CFSNode): ''' This is an interface to a NVMe passthru in ConfigFS. @@ -682,7 +684,7 @@ def _get_clear_ids(self): return _ids ids = property(_get_clear_ids, - doc = "Get the passthru namespace clear_ids attribute.") + doc="Get the passthru namespace clear_ids attribute.") def set_clear_ids(self, clear): self._check_self() @@ -701,7 +703,7 @@ def _get_admin_timeout(self): return _timeout admin_timeout = property(_get_admin_timeout, - doc = "Get the passthru admin command timeout.") + doc="Get the passthru admin command timeout.") def set_admin_timeout(self, timeout): self._check_self() @@ -720,7 +722,7 @@ def _get_io_timeout(self): return _timeout io_timeout = property(_get_io_timeout, - doc = "Get the passthru IO command timeout.") + doc="Get the passthru IO command timeout.") def set_io_timeout(self, timeout): self._check_self() @@ -751,6 +753,7 @@ def dump(self): d['io_timeout'] = self.io_timeout return d + class Port(CFSNode): ''' This is an interface to a NVMe Port in configFS. @@ -946,7 +949,8 @@ def __init__(self, port, grpid, mode='any'): else: grpid = int(grpid) if grpid < 1 or grpid > self.MAX_GRPID: - raise CFSError("GRPID %d must be 1 to %d" % (grpid, self.MAX_GRPID)) + raise CFSError("GRPID %d must be 1 to %d" + % (grpid, self.MAX_GRPID)) self.attr_groups = ['ana'] self._port = port @@ -1030,7 +1034,7 @@ def setup(cls, t, err_func): return try: - h = Host(t['nqn']) + Host(t['nqn']) except CFSError as e: err_func("Could not create Host object: %s" % e) return @@ -1045,5 +1049,6 @@ def _test(): from doctest import testmod testmod() + if __name__ == "__main__": _test() diff --git a/nvmet/test_nvmet.py b/nvmet/test_nvmet.py index 13e2f6c..ecbbf90 100644 --- a/nvmet/test_nvmet.py +++ b/nvmet/test_nvmet.py @@ -115,7 +115,8 @@ def test_namespace(self): self.assertEqual(len(list(s.namespaces)), 0) @unittest.skipUnless(test_devices_present(), - "Devices %s not available or suitable" % ','.join(NVMET_TEST_DEVICES)) + "Devices %s not available or suitable" + % ','.join(NVMET_TEST_DEVICES)) def test_namespace_attrs(self): root = nvme.Root() root.clear_existing() @@ -157,8 +158,8 @@ def test_recursive_delete(self): root.clear_existing() s = nvme.Subsystem(nqn='testnqn', mode='create') - n1 = nvme.Namespace(s, mode='create') - n2 = nvme.Namespace(s, mode='create') + nvme.Namespace(s, mode='create') + nvme.Namespace(s, mode='create') s.delete() self.assertEqual(len(list(root.subsystems)), 0) @@ -203,7 +204,7 @@ def test_loop_port(self): root = nvme.Root() root.clear_existing() - s = nvme.Subsystem(nqn='testnqn', mode='create') + nvme.Subsystem(nqn='testnqn', mode='create') p = nvme.Port(portid=0, mode='create') # subsystem doesn't exists, should fail @@ -372,9 +373,9 @@ def test_referral(self): self.assertEqual(len(list(p.referrals)), 0) def test_allowed_hosts(self): - root = nvme.Root() + nvme.Root() - h = nvme.Host(nqn='hostnqn', mode='create') + nvme.Host(nqn='hostnqn', mode='create') s = nvme.Subsystem(nqn='testnqn', mode='create') @@ -405,9 +406,9 @@ def test_invalid_input(self): self.assertRaises(nvme.CFSError, nvme.Subsystem, nqn='/', mode='create') - for l in [ 257, 512, 1024, 2048 ]: + for x in [257, 512, 1024, 2048]: toolong = ''.join(random.choice(string.ascii_lowercase) - for i in range(l)) + for i in range(x)) self.assertRaises(nvme.CFSError, nvme.Subsystem, nqn=toolong, mode='create') @@ -425,7 +426,7 @@ def test_save_restore(self): root = nvme.Root() root.clear_existing() - h = nvme.Host(nqn='hostnqn', mode='create') + nvme.Host(nqn='hostnqn', mode='create') s = nvme.Subsystem(nqn='testnqn', mode='create') s.add_allowed_host(nqn='hostnqn') @@ -460,7 +461,7 @@ def test_save_restore(self): root.restore_from_file('test.json', True) # rebuild our view of the world - h = nvme.Host(nqn='hostnqn', mode='lookup') + nvme.Host(nqn='hostnqn', mode='lookup') s = nvme.Subsystem(nqn='testnqn', mode='lookup') s2 = nvme.Subsystem(nqn='testnqn2', mode='lookup') n = nvme.Namespace(s, nsid=42, mode='lookup') diff --git a/nvmetcli b/nvmetcli index 859d22b..c565510 100755 --- a/nvmetcli +++ b/nvmetcli @@ -97,6 +97,7 @@ class UIRootNode(UINode): ui_desc_discovery = { 'nqn': ('string', 'Discovery NQN'), } + def __init__(self, shell): UINode.__init__(self, '/', parent=None, cfnode=nvme.Root(), shell=shell) @@ -188,9 +189,10 @@ class UISubsystemNode(UINode): info.append("serial=" + self.cfnode.get_attr("attr", "serial")) return (", ".join(info), True) + class UIPassthruNode(UINode): ui_desc_device = { - 'path' : ('string', 'Passthru device path') + 'path': ('string', 'Passthru device path') } def __init__(self, parent): @@ -207,7 +209,7 @@ class UIPassthruNode(UINode): try: self.cfnode.set_enable(1) self.shell.log.info("The passthru has been enabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The passthru could not be enabled.") @@ -218,7 +220,7 @@ class UIPassthruNode(UINode): try: self.cfnode.set_enable(0) self.shell.log.info("The passthru has been disabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The passthru could not be disabled.") @@ -229,7 +231,7 @@ class UIPassthruNode(UINode): ''' try: self.cfnode.set_clear_ids(clear) - except Exception as e: + except Exception: raise configshell.ExecutionError( "Failed to set clear_ids for this passthru target.") @@ -239,7 +241,7 @@ class UIPassthruNode(UINode): ''' try: self.cfnode.set_admin_timeout(timeout) - except Exception as e: + except Exception: raise configshell.ExecutionError( "Failed to set the admin passthru command timeout.") @@ -249,7 +251,7 @@ class UIPassthruNode(UINode): ''' try: self.cfnode.set_io_timeout(timeout) - except Exception as e: + except Exception: raise configshell.ExecutionError( "Failed to set the IO passthru command timeout.") @@ -265,6 +267,7 @@ class UIPassthruNode(UINode): info.append("enabled" if self.cfnode.get_enable() else "disabled") return (", ".join(info), True) + class UINamespacesNode(UINode): def __init__(self, parent): UINode.__init__(self, 'namespaces', parent) @@ -329,7 +332,7 @@ class UINamespaceNode(UINode): try: self.cfnode.set_enable(1) self.shell.log.info("The Namespace has been enabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The Namespace could not be enabled.") @@ -347,7 +350,7 @@ class UINamespaceNode(UINode): try: self.cfnode.set_enable(0) self.shell.log.info("The Namespace has been disabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The Namespace could not be disabled.") @@ -357,7 +360,7 @@ class UINamespaceNode(UINode): ''' try: self.cfnode.set_grpid(grpid) - except Exception as e: + except Exception: raise configshell.ExecutionError( "Failed to set ANA Group ID for this Namespace.") @@ -513,7 +516,7 @@ class UIPortNode(UINode): ''' try: inline_data_size = self.cfnode.get_attr("param", "inline_data_size") - except Exception as e: + except Exception: inline_data_size = "n/a" if inline_data_size != "n/a": info.append("inline_data_size=" + inline_data_size) @@ -648,7 +651,7 @@ class UIReferralNode(UINode): try: self.cfnode.set_enable(1) self.shell.log.info("The Referral has been enabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The Referral could not be enabled.") @@ -666,7 +669,7 @@ class UIReferralNode(UINode): try: self.cfnode.set_enable(0) self.shell.log.info("The Referral has been disabled.") - except Exception as e: + except Exception: raise configshell.ExecutionError( "The Referral could not be disabled.") @@ -706,7 +709,7 @@ class UIANAGroupsNode(UINode): class UIANAGroupNode(UINode): ui_desc_ana = { - 'state' : ('string', 'ANA state'), + 'state': ('string', 'ANA state'), } def __init__(self, parent, cfnode): @@ -717,6 +720,7 @@ class UIANAGroupNode(UINode): info.append("state=" + self.cfnode.get_attr("ana", "state")) return (", ".join(info), True) + class UIHostsNode(UINode): def __init__(self, parent): UINode.__init__(self, 'hosts', parent)