From 92891473beeaf52b9c833faed20edaa96cfc7148 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sat, 7 Nov 2026 20:14:11 +0100 Subject: [PATCH] fix(host-status): keep models.py at its recorded size, one literal for the dnf query The quality ratchet records models.py at 1454 lines and the new comments had made it 1460. bandit read the dnf history query, built with an f-string for its path, as SQL put together from strings (B608); it is a fixed query, now one literal with the path spelt out, and a test holds it to DNF_HISTORY. Refs NetOrk/netork#851 --- napalm_device_types/models.py | 14 ++++---------- napalm_device_types/package_managers.py | 4 +++- tests/test_host_status.py | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index f637499..94f9b30 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -85,11 +85,8 @@ class InterruptedPackageDict(TypedDict): """A package an interrupted package manager run left halfway.""" name: str - #: dpkg's word for it: "half-installed", "unpacked", "half-configured", - #: "triggers-awaited", "triggers-pending", or "reinstall-required"; opkg's - #: "unpacked", "half-configured", "half-installed" or "reinstall-required"; - #: rpm's "duplicate" (two versions installed); apk's "broken-files" or - #: "broken-script". + #: The manager's word for it: dpkg's and opkg's ("unpacked", "half-configured", ...), + #: rpm's "duplicate", apk's "broken-files" and "broken-script" (``package_managers``). state: str @@ -114,12 +111,9 @@ class HostStatusDict(TypedDict): reboot_reason: str | None #: True when the host installs updates on its own (unattended-upgrades, dnf-automatic). auto_updates: bool | None - #: The package manager the next two fields are about: "dpkg", "dnf", "yum", - #: "apk" or "opkg"; None when none could be read. + #: The package manager read (dpkg, dnf, yum, apk, opkg; None: none) and whether a run + #: of it was cut off, left halfway (None: cannot tell, or one is running right now). package_manager: NotRequired[str | None] - #: True when a package manager run was cut off and left the system halfway - #: (dpkg: until ``dpkg --configure -a``, opkg: ``opkg configure``); None when - #: it cannot tell, or a package manager is running right now. package_manager_interrupted: NotRequired[bool | None] #: The packages that run left halfway; empty when only its journal says so. interrupted_packages: NotRequired[list[InterruptedPackageDict]] diff --git a/napalm_device_types/package_managers.py b/napalm_device_types/package_managers.py index f32d627..2ad621a 100644 --- a/napalm_device_types/package_managers.py +++ b/napalm_device_types/package_managers.py @@ -74,8 +74,10 @@ _OPKG_HALFWAY = frozenset({"unpacked", "half-configured", "half-installed"}) _OPKG_REINSTALL = "reinstreq" #: The newest transaction in dnf's history as `` ``; nothing when unreadable. +#: One literal, with :data:`DNF_HISTORY` spelt out: a fixed query, nothing put into it. _DNF_HISTORY_PY = ( - f"import sqlite3; c = sqlite3.connect('file:{DNF_HISTORY}?mode=ro', uri=True); " + "import sqlite3; " + "c = sqlite3.connect('file:/var/lib/dnf/history.sqlite?mode=ro', uri=True); " "r = c.execute('SELECT id, state FROM trans ORDER BY id DESC LIMIT 1').fetchone(); " "r and print(*r)" ) diff --git a/tests/test_host_status.py b/tests/test_host_status.py index 83bfb46..4813756 100644 --- a/tests/test_host_status.py +++ b/tests/test_host_status.py @@ -684,7 +684,7 @@ class TestInterruptedRpm: def test_the_command_asks_rpm_only_where_dnf_or_yum_exists(self): assert "command -v rpm" in HOST_STATUS_COMMAND assert "rpm -qa --qf" in HOST_STATUS_COMMAND - assert "/var/lib/dnf/history.sqlite?mode=ro" in HOST_STATUS_COMMAND + assert f"file:{DNF_HISTORY}?mode=ro" in HOST_STATUS_COMMAND def test_it_never_repairs(self): for word in ("distro-sync", "--cleandupes", "remove", "yum-complete-transaction", "rpm -e"):