Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions avocado/utils/nvme.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,41 @@ def get_subsystem_using_ctrl_name(ctrl):
if ctrl in ctrls:
return get_subsys_name_with_nqn(device_nqn)
return ""


def get_nvme_subsystem_io_policy(subsystem):
"""
Read io policy parameter from sysfs folder and returns a string

:param subsystem: Name of the subsystem
:rtype: String
"""
subsys_iopolicy_path = f"/sys/class/nvme-subsystem/{subsystem}/iopolicy"
if os.path.isfile(subsys_iopolicy_path):
return open(subsys_iopolicy_path, "r", encoding="utf-8").readline().rstrip("\n")
raise NvmeException(f"iopolicy file not found: {subsys_iopolicy_path}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Satisfy Ruff TRY003.

Shorten messages and/or raise from the underlying exception as shown above.

Run:

Also applies to: 566-566, 569-569


🏁 Script executed:

#!/bin/bash
ruff check avocado/utils/nvme.py

Length of output: 17897


Comply with Ruff TRY003 on exception messages
In avocado/utils/nvme.py at lines 544, 566 and 569 the literal exception texts exceed Ruff’s length limit—shorten them or chain from the original error (e.g. raise NvmeException("iopolicy file missing") from err).

🧰 Tools
🪛 Ruff (0.12.2)

544-544: Avoid specifying long messages outside the exception class

(TRY003)

🤖 Prompt for AI Agents
In avocado/utils/nvme.py around lines 544, 566 and 569 the raised NvmeException
messages are too long per Ruff TRY003; replace the long literal texts with
shorter messages and chain the original error when appropriate (e.g., use a
concise message like "iopolicy file missing" or "failed to read iopolicy" and
re-raise with from err) so the exception text is brief while preserving the
original exception context.



def change_nvme_subsystem_io_policy(subsystem, io_policy):
"""
Changes io policy of nvme device to specified io policy
Change in io policy is not persistent

:param subsystem: Name of the subsystem
:param io_policy: Name of the io_policy to which we want to set
:raises: NvmeException on command failures
:rtype: Boolean
"""
subsys_iopolicy_path = f"/sys/class/nvme-subsystem/{subsystem}/iopolicy"
if get_nvme_subsystem_io_policy(subsystem) == io_policy:
LOGGER.info("Returning True as iopolicy is same as current")
return True
if os.path.isfile(subsys_iopolicy_path):
with open(subsys_iopolicy_path, "w", encoding="utf-8") as subsys_iopolicy_file:
subsys_iopolicy_file.write(io_policy)
subsys_iopolicy_file.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are using the open context manager, you don't need to call subsys_iopolicy_file.close(). It will be handled by the context manager.

else:
raise NvmeException(f"iopolicy file not found: {subsys_iopolicy_path}")
if get_nvme_subsystem_io_policy(subsystem) == io_policy:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain to me why the lines 567,568,569 are needed? IIUIC any issues with changing the policy will raise an error during the write operation at line 563 where you should handle it.

return True
raise NvmeException("Failed to change iopolicy")
Loading