"""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