feat: each package's architecture, 0.28.0 (NetOrk/netork#2907)
driver / lint (pull_request) Successful in 16s
driver / standard (pull_request) Successful in 18s
driver / lowest (pull_request) Successful in 33s
driver / types (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Successful in 2m5s
driver / publish (pull_request) Skipped
driver / lint (pull_request) Successful in 16s
driver / standard (pull_request) Successful in 18s
driver / lowest (pull_request) Successful in 33s
driver / types (pull_request) Successful in 1m12s
driver / test (3.12) (pull_request) Successful in 1m21s
driver / test (3.13) (pull_request) Successful in 1m10s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Successful in 2m5s
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.
This commit is contained in:
1 parent
43999267ac
commit
40cb888da7
7 files changed
+174
-48
No files matched your search
+21
-10
@@ -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
|
||||
+35
-21
@@ -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
|
||||
Reference in new issue
Block a user