driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 14s
driver / test (3.12) (pull_request) Failing after 38s
driver / types (pull_request) Failing after 46s
driver / lowest (pull_request) Failing after 19s
driver / test (3.13) (pull_request) Failing after 43s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Failing after 55s
CI / driver (pull_request) Failing after 1m49s
driver / publish (pull_request) Skipped
A multiarch host lists one name twice: libc6 for amd64 and for i386,
glibc for x86_64 and for i686. get_packages now reads dpkg's
${Architecture} and rpm's %{ARCH}, before the summary that stays last,
and reports it as PackageDict.architecture (napalm-device-types
4.59.0, the new floor). apk and pacman leave it out.
_rpm no longer postpones its annotations, so source_rpm is optional at
runtime too.
84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
"""The packages rpm has on a dnf or yum host: the query and its parse."""
|
|
|
|
# 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
|
|
|
|
#: 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 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%{ARCH}\\t%{SUMMARY}\\n'"
|
|
" 2>/dev/null"
|
|
)
|
|
|
|
|
|
class RpmPackageDict(PackageDict):
|
|
"""An rpm package with the name of the source rpm it was built from.
|
|
|
|
``perf``, ``python3-perf`` and ``bpftool`` come from ``kernel``, and Red Hat
|
|
lists them in every kernel CVE; their source is what tells them apart from
|
|
user space (NetOrk/netork#2394). Deliberately not dpkg's ``source_package``:
|
|
netOrk reads that as the name a distribution files its verdicts under, which
|
|
holds for Debian and not for Oracle or Red Hat, whose records name the binary.
|
|
Absent where rpm names none (``gpg-pubkey``).
|
|
"""
|
|
|
|
source_rpm: NotRequired[str]
|
|
|
|
|
|
def source_rpm_name(sourcerpm: str) -> str | None:
|
|
"""``kernel-5.14.0-687.66.1.el9_8.src.rpm`` → ``kernel``; ``None`` for ``(none)``."""
|
|
stem = sourcerpm.strip()
|
|
for suffix in (".src.rpm", ".nosrc.rpm"):
|
|
if stem.endswith(suffix):
|
|
stem = stem.removesuffix(suffix)
|
|
break
|
|
else:
|
|
return None
|
|
name, *version_release = stem.rsplit("-", 2)
|
|
return name if len(version_release) == 2 and name else None
|
|
|
|
|
|
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", 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
|