Files
linux-backuper/linux_backuper/managers/base.py
T

51 lines
1.8 KiB
Python

"""Base class and factory for package manager adapters."""
import shutil
from abc import ABC, abstractmethod
from typing import List
from linux_backuper.models import PackageItem
class BasePackageManager(ABC):
"""Abstract interface for Linux package manager inspectors."""
@abstractmethod
def get_manager_name(self) -> str:
"""Return the canonical name of the package manager (e.g. rpm, dpkg, pacman)."""
pass
@abstractmethod
def list_packages(self) -> List[PackageItem]:
"""Query and parse all installed packages on the system."""
pass
def is_available(self) -> bool:
"""Check if the package manager binary exists on the system."""
return shutil.which(self.get_manager_name()) is not None
def get_package_manager(distro_id: str) -> BasePackageManager:
"""Return appropriate package manager adapter based on distro_id or system binaries."""
from linux_backuper.managers.rpm import RpmPackageManager
from linux_backuper.managers.dpkg import DpkgPackageManager
from linux_backuper.managers.pacman import PacmanPackageManager
distro_id = (distro_id or "").lower()
if distro_id in ("rhel", "almalinux", "rocky", "centos", "fedora", "amzn"):
return RpmPackageManager()
elif distro_id in ("debian", "ubuntu", "raspbian", "pop", "linuxmint"):
return DpkgPackageManager()
elif distro_id in ("arch", "manjaro", "endeavouros"):
return PacmanPackageManager()
# Fallback by probing available binaries on PATH
if shutil.which("rpm"):
return RpmPackageManager()
elif shutil.which("dpkg-query"):
return DpkgPackageManager()
elif shutil.which("pacman"):
return PacmanPackageManager()
# Generic fallback
return DpkgPackageManager()