diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d3ae89..50c24fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.28.0] - 2026-12-05 + +### Added +- `get_packages()` names each package's architecture (`architecture`, napalm-device-types + 4.59.0): dpkg's `${Architecture}` (`amd64`, `i386`, `all`) and rpm's `%{ARCH}` + (`x86_64`, `i686`, `noarch`). A multiarch host's `libc6` for amd64 and for i386 are two + entries an application can now tell apart (NetOrk/netork#2907). apk and pacman leave it + out: Alpine and Arch install one architecture per host, and their listings do not + name it. + +### Fixed +- `source_rpm` on an rpm package is optional at runtime too + (`RpmPackageDict.__optional_keys__`): the module no longer postpones its annotations, + under which TypedDict read the key as required. + ## [0.27.0] - 2026-12-03 ### Added diff --git a/README.md b/README.md index db2b37c..811224a 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ with Driver( | Method | Supported | Package managers | |---|---|---| -| `get_packages()` | ✅ | apt, dnf, yum, apk, pacman | +| `get_packages()` | ✅ | apt, dnf, yum, apk, pacman; each package's `architecture` from dpkg and rpm | | `get_pending_updates()` | ✅ | apt, dnf, yum, apk, pacman | | `apply_updates(packages)` | ✅ | apt, dnf, yum, apk, pacman | | `get_services()` | ✅ | systemd, one round trip (fallback: SysV `service`) | diff --git a/napalm_linux/_dpkg.py b/napalm_linux/_dpkg.py index ae0bc70..df931d4 100644 --- a/napalm_linux/_dpkg.py +++ b/napalm_linux/_dpkg.py @@ -6,9 +6,12 @@ from napalm_device_types.models import PackageDict #: One line per package dpkg knows, its state first (``ii``, ``rc``, ``hi``, …): #: dpkg also lists removed packages whose configuration it still keeps (#17). +#: ``${Architecture}`` tells a multiarch host's ``libc6`` for amd64 from the one for +#: i386, two packages of one ``${Package}`` (NetOrk/netork#2907). DPKG_QUERY_COMMAND = ( "dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}\\t${Version}\\t${Installed-Size}" - "\\t${source:Package}\\t${source:Version}\\t${binary:Summary}\\n' 2>/dev/null" + "\\t${source:Package}\\t${source:Version}\\t${Architecture}\\t${binary:Summary}\\n'" + " 2>/dev/null" ) #: The second letter of ``db:Status-Abbrev`` for a package whose files are gone: #: ``n`` not installed, ``c`` only its configuration left (``rc``). Every other @@ -33,20 +36,25 @@ def parse_dpkg_query(out: str) -> list[PackageDict]: status, _, line = line.partition("\t") if status[1:2] in _NOT_ON_DISK: continue - package = _package(line.split("\t", 5)) + package = _package(line.split("\t", 6)) if package is not None: packages.append(package) return packages +def _field(parts: list[str], index: int) -> str: + """Field *index* of a split line, stripped; empty where the line has fewer.""" + return parts[index].strip() if len(parts) > index else "" + + def _package(parts: list[str]) -> DpkgPackageDict | None: if len(parts) < 2: return None - name = parts[0].strip() - version = parts[1].strip() - size = int(parts[2].strip()) * 1024 if len(parts) > 2 and parts[2].strip().isdigit() else 0 + name = _field(parts, 0) + version = _field(parts, 1) + size = _field(parts, 2) # Debian source package (e.g. openssh-server → openssh) for OSV matching. - source_package = parts[3].strip() if len(parts) > 3 and parts[3].strip() else name + source_package = _field(parts, 3) or name # And its version, which is a different number from this package's. # # OSV states Debian ranges in *source* versions. A source package @@ -60,14 +68,17 @@ def _package(parts: list[str]) -> DpkgPackageDict | None: # # dpkg leaves this empty when it equals `Version`; so does an older # dpkg that does not know the field at all. - source_version = parts[4].strip() if len(parts) > 4 and parts[4].strip() else version - return { + source_version = _field(parts, 4) or version + package: DpkgPackageDict = { "name": name, "version": version, "installed": True, - "description": parts[5].strip() if len(parts) > 5 else "", - "size": size, + "description": _field(parts, 6), + "size": int(size) * 1024 if size.isdigit() else 0, "source": "apt", "source_package": source_package, "source_version": source_version, } + if architecture := _field(parts, 5): + package["architecture"] = architecture + return package diff --git a/napalm_linux/_rpm.py b/napalm_linux/_rpm.py index 3c2bcd7..a844407 100644 --- a/napalm_linux/_rpm.py +++ b/napalm_linux/_rpm.py @@ -1,7 +1,7 @@ """The packages rpm has on a dnf or yum host: the query and its parse.""" -from __future__ import annotations - +# No ``from __future__ import annotations``: TypedDict tells a ``NotRequired`` key only +# from an evaluated annotation (``RpmPackageDict.__optional_keys__``). from typing import NotRequired from napalm_device_types.models import PackageDict @@ -9,12 +9,14 @@ 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). The source rpm before the -#: summary, which stays last and keeps whatever it contains. +#: than every installed build (NetOrk/netork#2394). The source rpm and the +#: architecture before the summary, which stays last and keeps whatever it contains: +#: a multiarch host has ``glibc`` for x86_64 and for i686, two packages of one name +#: (NetOrk/netork#2907). RPM_QUERY_COMMAND = ( "rpm -qa --queryformat " "'%{NAME}\\t%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}\\t%{SIZE}\\t%{SOURCERPM}" - "\\t%{SUMMARY}\\n'" + "\\t%{ARCH}\\t%{SUMMARY}\\n'" " 2>/dev/null" ) @@ -50,20 +52,32 @@ 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", 4) - if len(parts) < 2: - continue - size = parts[2].strip() if len(parts) > 2 else "" - package: RpmPackageDict = { - "name": parts[0].strip(), - "version": parts[1].strip(), - "installed": True, - "description": parts[4].strip() if len(parts) > 4 else "", - "size": int(size) if size.isdigit() else 0, - "source": source, - } - built_from = source_rpm_name(parts[3]) if len(parts) > 3 else None - if built_from: - package["source_rpm"] = built_from - packages.append(package) + parts = line.split("\t", 5) + if len(parts) >= 2: + packages.append(_package(parts, source)) return packages + + +def _field(parts: list[str], index: int) -> str: + """Field *index* of a split line, stripped; empty where the line has fewer.""" + return parts[index].strip() if len(parts) > index else "" + + +def _package(parts: list[str], source: str) -> RpmPackageDict: + size = _field(parts, 2) + package: RpmPackageDict = { + "name": _field(parts, 0), + "version": _field(parts, 1), + "installed": True, + "description": _field(parts, 5), + "size": int(size) if size.isdigit() else 0, + "source": source, + } + built_from = source_rpm_name(_field(parts, 3)) + if built_from: + package["source_rpm"] = built_from + # rpm writes ``(none)`` where a package has no architecture (``gpg-pubkey``). + architecture = _field(parts, 4) + if architecture and architecture != "(none)": + package["architecture"] = architecture + return package diff --git a/pyproject.toml b/pyproject.toml index 3f76ea7..4637e73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-linux" -version = "0.27.0" +version = "0.28.0" description = "NAPALM driver for generic Linux systems via SSH" readme = "README.md" requires-python = ">=3.12" @@ -34,7 +34,7 @@ classifiers = [ ] dependencies = [ "napalm>=4.0", - "napalm-device-types>=4.58.0", # PosixSharesMixin, nfs-server running + "napalm-device-types>=4.59.0", # PackageDict.architecture "netmiko>=4.0.0", "paramiko>=5.0.0", # CVE-2026-44405 ] diff --git a/tests/test_linux.py b/tests/test_linux.py index 23d220e..b336499 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -230,24 +230,24 @@ def test_parse_uptime_invalid(driver): #: data does. APT_PKG_OUTPUT = ( "ii \topenssh-server\t1:9.2p1-2+deb12u2\t512\topenssh\t1:9.2p1-2+deb12u2" - "\tsecure shell server\n" + "\tamd64\tsecure shell server\n" "ii \tcurl\t7.88.1-10+deb12u5\t1024\tcurl\t7.88.1-10+deb12u5" - "\tcommand line tool for transferring data\n" + "\tamd64\tcommand line tool for transferring data\n" # The shape that matters: a binary package whose own upstream version has # nothing to do with its source package's. ldb 2.11.0 is built from samba # 4.22.11, and OSV states Debian ranges in source versions. "ii \tlibldb2\t2:2.11.0+samba4.22.11+dfsg-0+deb13u1\t2048\tsamba" - "\t2:4.22.11+dfsg-0+deb13u1\tLDB shared library\n" + "\t2:4.22.11+dfsg-0+deb13u1\tamd64\tLDB shared library\n" ) #: Removed packages whose configuration dpkg still keeps (state ``rc``), next to #: a held one and one whose postinst failed -- both still on disk (#17). APT_PKG_STATES = ( - "rc \twazuh-agent\t4.14.7-1\t\twazuh-agent\t4.14.7-1\tWazuh agent\n" - "rc \tlinux-image-6.8.0-117-generic\t6.8.0-117.117\t\tlinux-signed\t6.8.0-117.117\tSigned kernel image generic\n" - "hi \tdocker-ce\t5:28.5.1-1~debian.13~trixie\t104858\tdocker-ce\t5:28.5.1-1~debian.13~trixie\tDocker: the open-source application container engine\n" - "iU \tzabbix-agent\t1:7.0.10-1\t1550\tzabbix\t1:7.0.10-1\tZabbix network monitoring solution - agent\n" - "ii \tcurl\t8.14.1-2\t512\tcurl\t8.14.1-2\tcommand line tool for transferring data with URL syntax\n" + "rc \twazuh-agent\t4.14.7-1\t\twazuh-agent\t4.14.7-1\tamd64\tWazuh agent\n" + "rc \tlinux-image-6.8.0-117-generic\t6.8.0-117.117\t\tlinux-signed\t6.8.0-117.117\tamd64\tSigned kernel image generic\n" + "hi \tdocker-ce\t5:28.5.1-1~debian.13~trixie\t104858\tdocker-ce\t5:28.5.1-1~debian.13~trixie\tamd64\tDocker: the open-source application container engine\n" + "iU \tzabbix-agent\t1:7.0.10-1\t1550\tzabbix\t1:7.0.10-1\tamd64\tZabbix network monitoring solution - agent\n" + "ii \tcurl\t8.14.1-2\t512\tcurl\t8.14.1-2\tamd64\tcommand line tool for transferring data with URL syntax\n" ) @@ -303,7 +303,9 @@ def test_get_packages_apt_falls_back_when_dpkg_gives_no_source(driver): """`source:Package` is empty for a package whose source name equals its own. Older dpkg builds leave `source:Version` empty in that case too.""" driver._pkg_manager = "apt" - with patch.object(driver, "_send", return_value="ii \tcurl\t7.88.1-10\t1024\t\t\ttool\n"): + with patch.object( + driver, "_send", return_value="ii \tcurl\t7.88.1-10\t1024\t\t\tamd64\ttool\n" + ): (pkg,) = driver.get_packages() assert pkg["source_package"] == "curl" @@ -2875,13 +2877,15 @@ class TestIpv6NextHops: # built from the kernel's source rpm; gpg-pubkey has none. RPM_QA = ( "glibc-common\t2.34-284.0.1.el9_8.4\t1210460\tglibc-2.34-284.0.1.el9_8.4.src.rpm" - "\tCommon binaries and locale data for glibc\n" - "gmp\t1:6.2.0-13.el9\t805146\tgmp-6.2.0-13.el9.src.rpm\tA GNU arbitrary precision library\n" + "\taarch64\tCommon binaries and locale data for glibc\n" + "gmp\t1:6.2.0-13.el9\t805146\tgmp-6.2.0-13.el9.src.rpm\taarch64" + "\tA GNU arbitrary precision library\n" "openssl\t1:3.5.8-2.0.1.el9_8\t2106925\topenssl-3.5.8-2.0.1.el9_8.src.rpm" - "\tUtilities from the general purpose cryptography library\n" + "\taarch64\tUtilities from the general purpose cryptography library\n" "perf\t5.14.0-687.66.1.el9_8\t5401292\tkernel-5.14.0-687.66.1.el9_8.src.rpm" - "\tPerformance monitoring for the Linux kernel\n" - "gpg-pubkey\t8d8b756f-629e59ec\t0\t(none)\tOracle OSS group (Open Source Software group)\n" + "\taarch64\tPerformance monitoring for the Linux kernel\n" + "gpg-pubkey\t8d8b756f-629e59ec\t0\t(none)\t(none)" + "\tOracle OSS group (Open Source Software group)\n" ) diff --git a/tests/test_package_architecture.py b/tests/test_package_architecture.py new file mode 100644 index 0000000..6c38637 --- /dev/null +++ b/tests/test_package_architecture.py @@ -0,0 +1,82 @@ +"""Each package names its architecture (NetOrk/netork#2907). + +A multiarch host lists one name twice: Debian's ``libc6`` for amd64 and for i386, an +rpm host's ``glibc`` for x86_64 and for i686. The name alone does not tell the two +apart; ``architecture``, in the package manager's own words, does. +""" + +from napalm_linux._dpkg import DPKG_QUERY_COMMAND, DpkgPackageDict, parse_dpkg_query +from napalm_linux._rpm import RPM_QUERY_COMMAND, RpmPackageDict, parse_rpm_query + +#: ``dpkg-query`` on a Debian 12 host with i386 added as a foreign architecture. +DPKG_MULTIARCH = ( + "ii \tlibc6\t2.36-9+deb12u9\t12684\tglibc\t2.36-9+deb12u9\tamd64" + "\tGNU C Library: Shared libraries\n" + "ii \tlibc6\t2.36-9+deb12u9\t11892\tglibc\t2.36-9+deb12u9\ti386" + "\tGNU C Library: Shared libraries\n" + "ii \ttzdata\t2024b-0+deb12u1\t3420\ttzdata\t2024b-0+deb12u1\tall" + "\ttime zone and daylight-saving time data\n" +) + +#: ``rpm -qa`` on a Rocky 9 host with the 32-bit glibc next to the 64-bit one; +#: gpg-pubkey has no architecture (``(none)``). +RPM_MULTIARCH = ( + "glibc\t2.34-125.el9_5.8\t6211339\tglibc-2.34-125.el9_5.8.src.rpm\tx86_64" + "\tThe GNU libc libraries\n" + "glibc\t2.34-125.el9_5.8\t6305421\tglibc-2.34-125.el9_5.8.src.rpm\ti686" + "\tThe GNU libc libraries\n" + "tzdata\t2024b-2.el9\t1854201\ttzdata-2024b-2.el9.src.rpm\tnoarch\tTimezone data\n" + "gpg-pubkey\t8d8b756f-629e59ec\t0\t(none)\t(none)\tgpg(Rocky Enterprise Software)\n" +) + + +class TestDpkg: + def test_the_query_asks_for_the_architecture_before_the_summary(self) -> None: + assert "\\t${Architecture}\\t${binary:Summary}\\n" in DPKG_QUERY_COMMAND + + def test_one_name_in_two_architectures_is_two_packages(self) -> None: + packages = parse_dpkg_query(DPKG_MULTIARCH) + + assert [(p["name"], p.get("architecture")) for p in packages] == [ + ("libc6", "amd64"), + ("libc6", "i386"), + ("tzdata", "all"), + ] + + def test_the_summary_stays_the_description(self) -> None: + (amd64, i386, _) = parse_dpkg_query(DPKG_MULTIARCH) + + assert amd64["description"] == "GNU C Library: Shared libraries" + assert i386["size"] == 11892 * 1024 + assert i386["source_package"] == "glibc" # type: ignore[typeddict-item] + + def test_an_empty_architecture_is_left_out(self) -> None: + (package,) = parse_dpkg_query("ii \tcurl\t7.88.1-10\t1024\t\t\t\ttool\n") + + assert "architecture" not in package + assert package["description"] == "tool" + + def test_architecture_is_optional_at_runtime(self) -> None: + assert "architecture" in DpkgPackageDict.__optional_keys__ + + +class TestRpm: + def test_the_query_asks_for_the_architecture_before_the_summary(self) -> None: + assert "\\t%{ARCH}\\t%{SUMMARY}\\n" in RPM_QUERY_COMMAND + + def test_one_name_in_two_architectures_is_two_packages(self) -> None: + packages = parse_rpm_query(RPM_MULTIARCH, "dnf") + + assert [(p["name"], p.get("architecture")) for p in packages] == [ + ("glibc", "x86_64"), + ("glibc", "i686"), + ("tzdata", "noarch"), + ("gpg-pubkey", None), + ] + assert packages[1]["description"] == "The GNU libc libraries" + assert packages[1]["source_rpm"] == "glibc" # type: ignore[typeddict-item] + + def test_source_rpm_and_architecture_are_optional_at_runtime(self) -> None: + """No postponed annotations in ``_rpm``: TypedDict sees ``NotRequired`` only in + an evaluated annotation.""" + assert {"source_rpm", "architecture"} <= RpmPackageDict.__optional_keys__