39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
"""DPKG package manager adapter (Debian, Ubuntu)."""
|
|
|
|
import subprocess
|
|
from typing import List
|
|
|
|
from linux_backuper.models import PackageItem
|
|
from linux_backuper.managers.base import BasePackageManager
|
|
|
|
class DpkgPackageManager(BasePackageManager):
|
|
"""Inspector for DPKG-based Linux distributions."""
|
|
|
|
def get_manager_name(self) -> str:
|
|
return "dpkg-query"
|
|
|
|
def list_packages(self) -> List[PackageItem]:
|
|
"""List installed packages via dpkg-query."""
|
|
cmd = ["dpkg-query", "-W", "-f=${Package}|${Version}|${Architecture}\n"]
|
|
try:
|
|
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
|
|
if res.returncode != 0:
|
|
return []
|
|
except (OSError, FileNotFoundError):
|
|
return []
|
|
|
|
packages: List[PackageItem] = []
|
|
for line in res.stdout.splitlines():
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
parts = line.split("|")
|
|
if len(parts) >= 2:
|
|
name = parts[0]
|
|
version = parts[1]
|
|
arch = parts[2] if len(parts) > 2 else ""
|
|
packages.append(PackageItem(name=name, version=version, arch=arch))
|
|
|
|
packages.sort(key=lambda p: p.name)
|
|
return packages
|