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 DnfManager(PackageManager): def __init__(self, runner: Optional[CommandRunner] = None, binary: Optional[str] = None) -> None: self.runner = runner or CommandRunner() if binary is not None: self.binary = binary else: self.binary = "yum" if (not shutil.which("dnf") and shutil.which("yum")) else "dnf" @property def name(self) -> str: return self.binary def refresh_repositories(self) -> CommandResult: cmd = [self.binary, "makecache"] return self.runner.run(cmd) def check_updates(self) -> List[PackageUpdate]: cmd = [self.binary, "check-update"] res = self.runner.run(cmd) # DNF returns exit code 100 when updates are found, 0 when clean if res.return_code not in (0, 100): return [] updates: List[PackageUpdate] = [] lines = res.stdout.splitlines() for line in lines: line = line.strip() if not line or line.startswith("Last metadata") or line.startswith("Security:") or "Obsoleting Packages" in line: continue parts = line.split() if len(parts) >= 3: pkg = parts[0] new_ver = parts[1] repo = parts[2] updates.append(PackageUpdate(package=pkg, current_version="installed", new_version=new_ver, repo=repo)) return updates def apply_updates( self, packages: Optional[List[str]] = None, dry_run: bool = False, non_interactive: bool = False, ) -> CommandResult: cmd = [self.binary, "upgrade"] if dry_run: cmd.append("--assumeno") elif non_interactive: cmd.append("-y") if packages: cmd.extend(packages) res = self.runner.run(cmd) if dry_run and res.return_code == 1: combined = res.stdout + res.stderr if "Operation aborted" in combined or "Nothing to do" in combined: return CommandResult( return_code=0, stdout=res.stdout, stderr=res.stderr, success=True, ) return res def release_upgrade( self, target_release: Optional[str] = None, dry_run: bool = False, non_interactive: bool = False, ) -> CommandResult: import shutil if shutil.which("leapp"): cmd = ["leapp", "preupgrade" if dry_run else "upgrade"] return self.runner.run(cmd) # Check if dnf system-upgrade is available check = self.runner.run([self.binary, "system-upgrade", "--help"]) if check.return_code != 0: # Try to install system-upgrade plugin if interactive or root self.runner.run([self.binary, "install", "-y", "dnf-plugins-core"]) check = self.runner.run([self.binary, "system-upgrade", "--help"]) if check.return_code != 0: if dry_run: return CommandResult( return_code=0, stdout=f"[DRY-RUN] Release upgrade simulation for target release {target_release or 'latest'}", stderr="", success=True, ) return CommandResult( return_code=1, stdout="", stderr="Major release upgrade requires 'dnf system-upgrade' (Fedora) or 'leapp' (RHEL/AlmaLinux). Please install the required upgrade framework.", success=False, ) cmd = [self.binary, "system-upgrade", "download"] if dry_run: cmd.append("--assumeno") elif non_interactive: cmd.append("-y") if target_release: cmd.append(f"--releasever={target_release}") res = self.runner.run(cmd) if dry_run and res.return_code == 1: combined = res.stdout + res.stderr if "Operation aborted" in combined or "Nothing to do" in combined: return CommandResult( return_code=0, stdout=res.stdout, stderr=res.stderr, success=True, ) return res def clean_cache(self) -> CommandResult: return self.runner.run([self.binary, "clean", "all"]) def autoremove(self) -> CommandResult: cmd = [self.binary, "autoremove"] cmd.append("-y") return self.runner.run(cmd) def is_reboot_required(self) -> RebootStatus: # Check needs-restarting -r if shutil.which("needs-restarting"): res = self.runner.run(["needs-restarting", "-r"]) # Exit code 1 indicates reboot is required if res.return_code == 1: return RebootStatus(required=True, reason="needs-restarting reported pending reboot") return RebootStatus(required=False) def get_lock_files(self) -> List[Path]: return [ Path("/var/lib/rpm/.rpm.lock"), Path("/var/run/dnf.pid"), ]