100 lines
3.3 KiB
Python
100 lines
3.3 KiB
Python
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 non_interactive:
|
|
cmd.append("-y")
|
|
if dry_run:
|
|
cmd.append("--assumeno")
|
|
if packages:
|
|
cmd.extend(packages)
|
|
|
|
return self.runner.run(cmd)
|
|
|
|
def release_upgrade(
|
|
self,
|
|
target_release: Optional[str] = None,
|
|
non_interactive: bool = False,
|
|
) -> CommandResult:
|
|
cmd = [self.binary, "system-upgrade", "download"]
|
|
if non_interactive:
|
|
cmd.append("-y")
|
|
if target_release:
|
|
cmd.append(f"--releasever={target_release}")
|
|
return self.runner.run(cmd)
|
|
|
|
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"),
|
|
]
|