fix(packages): read install_package's success from the exit status

install_package searched the output of `... || true` for words such as
"failed", the pattern uninstall_package left behind for netork#267: a
postinst that only mentions a failure read as a failed install, and a
failure phrased without those words read as success. It now runs through
_sudo_status like the removal and falls back to the keywords only when
the channel gave no exit status.

The ruff findings named in the issue are gone since the driver adopted
the standard; strict mypy for linux.py is held by the quality ratchet,
and the None-vs-dict package contract is NAPALM/napalm-device-types#36.

Closes #4
This commit is contained in:
Christian Manivong committed 2026-11-01 21:44:51 +01:00
1 parent a729b35d8b
commit eaa08ae7e3
4 files changed
+70 -26

No files matched your search

+20 -25
View File
@@ -1455,39 +1455,33 @@ class LinuxDriver(
return packages
def install_package(self, name: str) -> dict[str, Any]:
"""Install a package by name. Returns ``{"success": bool, "output": str}``."""
"""Install a package by name. Returns ``{"success": bool, "output": str}``.
Success is the package manager's exit status, as for ``uninstall_package``."""
from shlex import quote as _q
safe = _q(name)
pm = self._pkg_manager
if pm == "apt":
raw = self._sudo(
f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1 || true"
)
cmd = f"DEBIAN_FRONTEND=noninteractive apt-get install -y {safe} 2>&1"
elif pm in ("dnf", "yum"):
raw = self._sudo(f"{pm} install -y {safe} 2>&1 || true")
cmd = f"{pm} install -y {safe} 2>&1"
elif pm == "apk":
raw = self._sudo(f"apk add {safe} 2>&1 || true")
cmd = f"apk add {safe} 2>&1"
elif pm == "pacman":
raw = self._sudo(f"pacman -S --noconfirm {safe} 2>&1 || true")
cmd = f"pacman -S --noconfirm {safe} 2>&1"
else:
return {"success": False, "output": f"Unsupported package manager: {pm}"}
low = raw.lower()
success = not any(
kw in low
for kw in (
"error:",
"failed",
"no packages",
"not found",
"unable to locate",
"no match",
)
)
return {"success": success, "output": raw.strip()}
raw, rc = self._sudo_status(cmd)
failed = self._package_command_failed(raw, rc, self._INSTALL_FAILED)
return {"success": not failed, "output": raw.strip()}
#: Words in a package manager's output that mean an install did not happen.
#: Only consulted when the exit status is unknown; see ``_package_command_failed``.
_INSTALL_FAILED = ("error:", "failed", "no packages", "not found", "unable to locate")
#: Words in a package manager's output that mean it did not do the job.
#: Only consulted when the exit status is unknown; see ``_uninstall_failed``.
#: Only consulted when the exit status is unknown; see ``_package_command_failed``.
_UNINSTALL_FAILED = ("error:", "failed", "not found", "is not installed", "no packages")
def uninstall_package(self, name: str, purge: bool = False) -> dict[str, Any]:
@@ -1533,16 +1527,17 @@ class LinuxDriver(
return {"success": False, "output": f"Unsupported package manager: {pm}"}
raw, rc = self._sudo_status(cmd)
failed = self._uninstall_failed(raw, rc)
failed = self._package_command_failed(raw, rc, self._UNINSTALL_FAILED)
if failed and pm == "apt":
forced, forced_rc = self._sudo_status(f"dpkg --purge --force-all {safe} 2>&1")
raw = f"{raw.strip()}\n--- dpkg --purge --force-all ---\n{forced.strip()}"
failed = self._uninstall_failed(forced, forced_rc)
failed = self._package_command_failed(forced, forced_rc, self._UNINSTALL_FAILED)
return {"success": not failed, "output": raw.strip()}
def _uninstall_failed(self, output: str, rc: int | None = None) -> bool:
@staticmethod
def _package_command_failed(output: str, rc: int | None, keywords: tuple[str, ...]) -> bool:
"""Whether the package manager did not do the job.
The exit status decides whenever there is one (netork#267): it is the
@@ -1566,7 +1561,7 @@ class LinuxDriver(
low = output.lower()
if any(line.lstrip().startswith("e: ") for line in low.splitlines()):
return True
return any(kw in low for kw in self._UNINSTALL_FAILED)
return any(kw in low for kw in keywords)
def get_available_updates(self) -> list[UpdateDict]:
if self._pkg_manager == "apt":