Files
napalm-linux/napalm_linux/_dpkg.py
T
Christian Manivong 024f68c543 fix(packages): leave removed packages with leftover config out of apt
dpkg-query -W also lists packages that were removed but whose
configuration dpkg keeps (state rc), and the driver marked every line
installed: old kernels and a removed agent showed up as installed
software. The query now asks for db:Status-Abbrev and drops packages
that are not installed or only left their configuration; held, unpacked
and half-configured packages keep counting, their files are on disk.

The dpkg query and its parse move to napalm_linux/_dpkg.py, a pure
function under strict mypy, which keeps linux.py inside its line count.

Closes #17
2026-11-01 21:48:05 +01:00

74 lines
3.1 KiB
Python

"""The packages dpkg has on a Debian-family host: the query and its parse."""
from __future__ import annotations
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).
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"
)
#: 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
#: state -- half-installed, unpacked, half-configured, awaiting triggers -- has
#: its files on disk.
_NOT_ON_DISK = frozenset("nc")
class DpkgPackageDict(PackageDict):
"""A dpkg package with the source package it was built from."""
source_package: str
source_version: str
def parse_dpkg_query(out: str) -> list[PackageDict]:
"""The packages in ``DPKG_QUERY_COMMAND``'s output whose files are on disk."""
packages: list[PackageDict] = []
for line in out.splitlines():
# Summary stays last and keeps whatever it contains: maxsplit must
# equal the number of tabs the format writes, not the field count.
status, _, line = line.partition("\t")
if status[1:2] in _NOT_ON_DISK:
continue
package = _package(line.split("\t", 5))
if package is not None:
packages.append(package)
return packages
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
# 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
# And its version, which is a different number from this package's.
#
# OSV states Debian ranges in *source* versions. A source package
# that ships several binaries gives each its own upstream version:
# libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba,
# is 2:4.22.11+dfsg-…. A consumer matching on source_package and
# comparing `version` compares two unrelated numbers — dpkg reads
# ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed
# CVE-2022-44640, and a host five releases past the fix was reported
# vulnerable on four packages at once.
#
# 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 {
"name": name,
"version": version,
"installed": True,
"description": parts[5].strip() if len(parts) > 5 else "",
"size": size,
"source": "apt",
"source_package": source_package,
"source_version": source_version,
}