refactor(packages): the rpm query and its parse in _rpm.py, as dpkg's in _dpkg.py
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 16s
driver / test (3.13) (pull_request) Successful in 55s
driver / types (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m16s
driver / publish (pull_request) Skipped

linux.py stays under its recorded size (2039 -> 2034 lines).
This commit is contained in:
Christian Manivong committed 2026-11-12 12:24:43 +01:00
1 parent 1d377acc5f
commit bc7162affd
3 files changed
+43 -43

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
"""The packages rpm has on a dnf or yum host: the query and its parse."""
from __future__ import annotations
from napalm_device_types.models import PackageDict
#: One line per package, the version as ``[epoch:]version-release`` the way dpkg
#: writes it: the epoch only where the package has one. Oracle's and Red Hat's
#: fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as newer
#: than every installed build (NetOrk/netork#2394).
RPM_QUERY_COMMAND = (
"rpm -qa --queryformat "
"'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'"
" 2>/dev/null"
)
def parse_rpm_query(out: str, source: str) -> list[PackageDict]:
"""The packages in ``RPM_QUERY_COMMAND``'s output, each marked as *source*'s."""
packages: list[PackageDict] = []
for line in out.splitlines():
parts = line.split("\t", 3)
if len(parts) < 2:
continue
size = parts[2].strip() if len(parts) > 2 else ""
packages.append(
{
"name": parts[0].strip(),
"version": parts[1].strip(),
"installed": True,
"description": parts[3].strip() if len(parts) > 3 else "",
"size": int(size) if size.isdigit() else 0,
"source": source,
}
)
return packages
+6 -42
View File
@@ -82,6 +82,7 @@ from napalm_linux._ping import parse_ping, ping_command
from napalm_linux._posix import LinuxPosixMixin
from napalm_linux._readings import LinuxProcessReadingsMixin
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, with_length
from napalm_linux._rpm import RPM_QUERY_COMMAND, parse_rpm_query
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -1140,35 +1141,7 @@ class LinuxDriver(
return parse_dpkg_query(self._send(DPKG_QUERY_COMMAND))
def _get_packages_rpm(self) -> list[PackageDict]:
"""``rpm -qa``, the version as ``[epoch:]version-release``, as dpkg writes it.
The epoch is there only where the package has one: Oracle's and Red Hat's
fixes name it (``1:6.2.0-13.el9``), and without it an epoch-1 fix reads as
newer than every installed build (NetOrk/netork#2394).
"""
out = self._send(
"rpm -qa --queryformat "
"'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SUMMARY}\\n'"
" 2>/dev/null"
)
packages: list[PackageDict] = []
for line in out.splitlines():
parts = line.split("\t", 3)
if len(parts) < 2:
continue
packages.append(
{
"name": parts[0].strip(),
"version": parts[1].strip(),
"installed": True,
"description": parts[3].strip() if len(parts) > 3 else "",
"size": int(parts[2].strip())
if len(parts) > 2 and parts[2].strip().isdigit()
else 0,
"source": self._pkg_manager or "rpm",
}
)
return packages
return parse_rpm_query(self._send(RPM_QUERY_COMMAND), self._pkg_manager or "rpm")
def _get_packages_apk(self) -> list[PackageDict]:
out = self._send("apk info -v 2>/dev/null")
@@ -1487,20 +1460,11 @@ class LinuxDriver(
return None
def get_installed_advisory_cves(self) -> dict[str, list[str]]:
"""The CVEs the installed security advisories close, per package (dnf 4).
As root, from the cache, as the security read does: root's cache is the one
the host's own updates keep. The output is long (90 000 lines on an Oracle
Linux 9 host), so it gets a longer budget; netOrk reads it only where its
knowledge base holds no records of the release (NetOrk/netork#2394).
:raises NotImplementedError: on any package manager but dnf.
:raises ValueError: when dnf failed or its output was cut short.
"""
"""The CVEs the installed advisories close, per package: dnf only, as root and
from the cache like the security read, with a longer budget for its 90 000
lines on Oracle Linux 9 (NetOrk/netork#2394)."""
if self._pkg_manager != "dnf":
raise NotImplementedError(
f"Installed advisories are not read for {self._pkg_manager!r}"
)
raise NotImplementedError(f"No installed advisories for {self._pkg_manager!r}")
cmd = DNF_INSTALLED_CVES_COMMAND
output, status = _split_status(self._run_privileged(f"{cmd}; echo {_RC_MARKER}$?", 300))
if status != 0:
+1 -1
View File
@@ -20,7 +20,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2039
"napalm_linux/linux.py": 2034
},
"untyped_public": []
}