Skip to content

Commit 89c132d

Browse files
hreineckeigaw
authored andcommitted
examples/discover-loop.py: rework to use 'with' statement
Now that we have resource tracking we can use the 'with' statement for discovery and clean up connections and discovery controllers on exit. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent eb72e3b commit 89c132d

1 file changed

Lines changed: 29 additions & 32 deletions

File tree

examples/discover-loop.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,43 @@ def disc_supp_str(dlp_supp_opts):
1616
}
1717
return [txt for msk, txt in d.items() if dlp_supp_opts & msk]
1818

19-
root = nvme.root()
20-
host = nvme.host(root)
21-
22-
subsysnqn = nvme.NVME_DISC_SUBSYS_NAME
23-
transport = 'tcp'
24-
traddr = '127.0.0.1'
25-
trsvcid = '4420'
19+
def discover(host, ctrl):
20+
try:
21+
ctrl.connect(host)
22+
except Exception as e:
23+
print(f'Failed to connect: {e}')
24+
return
2625

27-
ctrl = nvme.ctrl(root, subsysnqn=subsysnqn, transport=transport, traddr=traddr, trsvcid=trsvcid)
26+
print(f'{ctrl.name} connected to subsys {ctrl.subsystem}')
2827

29-
try:
30-
ctrl.connect(host)
31-
except Exception as e:
32-
sys.exit(f'Failed to connect: {e}')
28+
slp = ctrl.supported_log_pages()
29+
try:
30+
dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16
31+
except (TypeError, IndexError):
32+
dlp_supp_opts = 0
3333

34-
print(f'{ctrl.name} connected to subsys {ctrl.subsystem}')
34+
print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}")
3535

36-
slp = ctrl.supported_log_pages()
36+
try:
37+
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
38+
disc_log = ctrl.discover(lsp=lsp)
39+
except Exception as e:
40+
print(f'Failed to discover: {e}')
41+
return
3742

38-
try:
39-
dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16
40-
except (TypeError, IndexError):
41-
dlp_supp_opts = 0
43+
for dlpe in disc_log:
44+
print(f'log entry {dlpe["portid"]}: {dlpe["subtype"]} {dlpe["subnqn"]}')
4245

43-
print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}")
44-
45-
try:
46-
lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0
47-
disc_log = ctrl.discover(lsp=lsp)
48-
except Exception as e:
49-
print(f'Failed to discover: {e}')
50-
disc_log = []
46+
root = nvme.root()
47+
host = nvme.host(root)
5148

52-
for dlpe in disc_log:
53-
print(f'log entry {dlpe["portid"]}: {dlpe["subtype"]} {dlpe["subnqn"]}')
49+
subsysnqn = nvme.NVME_DISC_SUBSYS_NAME
50+
transport = 'tcp'
51+
traddr = '127.0.0.1'
52+
trsvcid = '4420'
5453

55-
try:
56-
ctrl.disconnect()
57-
except Exception as e:
58-
sys.exit(f'Failed to disconnect: {e}')
54+
with nvme.ctrl(root, subsysnqn=subsysnqn, transport=transport, traddr=traddr, trsvcid=trsvcid) as ctrl:
55+
discover(host, ctrl)
5956

6057
for s in host.subsystems():
6158
for c in s.controllers():

0 commit comments

Comments
 (0)