Files
soda64 8bec4ed932
Build and Publish Release / Build Packages and Publish (push) Successful in 27s
feat: release v2.0.0 with tab completion, package filtering, db health checks, flatpak and logrotate
2026-10-03 08:31:00 +02:00

148 lines
5.2 KiB
Python

import os
import shutil
from pathlib import Path
from typing import List, Optional, Tuple
from linux_updater.core.runner import CommandRunner
from linux_updater.managers.base import PackageManager
from linux_updater.models import CommandResult, PackageUpdate, RebootStatus
class PacmanManager(PackageManager):
def __init__(self, runner: Optional[CommandRunner] = None) -> None:
self.runner = runner or CommandRunner()
@property
def name(self) -> str:
return "pacman"
def refresh_repositories(self) -> CommandResult:
return self.runner.run(["pacman", "-Sy"])
def check_updates(
self,
exclude: Optional[List[str]] = None,
security_only: bool = False,
) -> List[PackageUpdate]:
# Prefer checkupdates if installed (safe non-root update check)
cmd = ["checkupdates"] if shutil.which("checkupdates") else ["pacman", "-Qu"]
res = self.runner.run(cmd)
import fnmatch
updates: List[PackageUpdate] = []
for line in res.stdout.splitlines():
line = line.strip()
if not line or "->" not in line:
continue
try:
left, right = line.split("->", 1)
left_parts = left.strip().split()
right_parts = right.strip().split()
pkg = left_parts[0]
if exclude and any(fnmatch.fnmatch(pkg, pat) for pat in exclude):
continue
old_ver = left_parts[1] if len(left_parts) > 1 else "unknown"
new_ver = right_parts[0] if right_parts else "unknown"
repo = right_parts[1].strip("[]") if len(right_parts) > 1 else None
updates.append(PackageUpdate(package=pkg, current_version=old_ver, new_version=new_ver, repo=repo))
except Exception:
continue
return updates
def apply_updates(
self,
packages: Optional[List[str]] = None,
exclude: Optional[List[str]] = None,
security_only: bool = False,
dry_run: bool = False,
non_interactive: bool = False,
) -> CommandResult:
if packages:
import fnmatch
target_pkgs = [p for p in packages if not (exclude and any(fnmatch.fnmatch(p, pat) for pat in exclude))]
if not target_pkgs:
return CommandResult(0, "All specified packages were excluded.", "", True)
cmd = ["pacman", "-S"]
if dry_run:
cmd.append("-p")
else:
cmd.append("--noconfirm")
if exclude:
cmd.extend(["--ignore", ",".join(exclude)])
cmd.extend(target_pkgs)
else:
cmd = ["pacman", "-Su"]
if dry_run:
cmd.append("-p")
else:
cmd.append("--noconfirm")
if exclude:
cmd.extend(["--ignore", ",".join(exclude)])
return self.runner.run(cmd, stream=not dry_run)
def release_upgrade(
self,
target_release: Optional[str] = None,
dry_run: bool = False,
non_interactive: bool = False,
) -> CommandResult:
# Arch is a rolling release distribution - system upgrade is a full sync
cmd = ["pacman", "-Syu"]
if dry_run:
cmd.append("-p")
else:
cmd.append("--noconfirm")
return self.runner.run(cmd, stream=not dry_run)
def clean_cache(self) -> CommandResult:
return self.runner.run(["pacman", "-Sc", "--noconfirm"])
def autoremove(self) -> CommandResult:
# Find orphan packages
orphans_res = self.runner.run(["pacman", "-Qtdq"])
orphans = [p.strip() for p in orphans_res.stdout.splitlines() if p.strip()]
if not orphans:
return CommandResult(return_code=0, stdout="No orphan packages to remove.", stderr="", success=True)
cmd = ["pacman", "-Rns", "--noconfirm"] + orphans
return self.runner.run(cmd)
def is_reboot_required(self) -> RebootStatus:
# On Arch, if running kernel version does not match /usr/lib/modules/<uname>, reboot is required
try:
running_kernel = os.uname().release
modules_dir = Path("/usr/lib/modules") / running_kernel
if Path("/usr/lib/modules").exists() and not modules_dir.exists():
return RebootStatus(
required=True,
reason=f"Running kernel {running_kernel} modules directory missing (kernel upgraded)",
)
except Exception:
pass
return RebootStatus(required=False)
def get_lock_files(self) -> List[Path]:
return [Path("/var/lib/pacman/db.lck")]
def verify_db_consistency(self) -> "Tuple[bool, str]":
lock_p = Path("/var/lib/pacman/db.lck")
if lock_p.exists():
return False, f"Stale pacman database lock: {lock_p}"
return True, "Pacman database is consistent."
def repair_db(self) -> CommandResult:
lock_p = Path("/var/lib/pacman/db.lck")
if lock_p.exists():
try:
lock_p.unlink()
except Exception:
pass
return CommandResult(0, "Pacman database checked.", "", True)