115 lines
4.0 KiB
Python
115 lines
4.0 KiB
Python
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
|
|
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) -> 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)
|
|
|
|
updates: List[PackageUpdate] = []
|
|
for line in res.stdout.splitlines():
|
|
line = line.strip()
|
|
if not line or "->" not in line:
|
|
continue
|
|
|
|
# Format: package old_version -> new_version [repo]
|
|
try:
|
|
left, right = line.split("->", 1)
|
|
left_parts = left.strip().split()
|
|
right_parts = right.strip().split()
|
|
|
|
pkg = left_parts[0]
|
|
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,
|
|
dry_run: bool = False,
|
|
non_interactive: bool = False,
|
|
) -> CommandResult:
|
|
if packages:
|
|
cmd = ["pacman", "-S"]
|
|
if dry_run:
|
|
cmd.append("-p")
|
|
else:
|
|
cmd.append("--noconfirm")
|
|
cmd.extend(packages)
|
|
else:
|
|
cmd = ["pacman", "-Su"]
|
|
if dry_run:
|
|
cmd.append("-p")
|
|
else:
|
|
cmd.append("--noconfirm")
|
|
|
|
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")]
|