- Refactor tests to achieve 100% Zero-Mocks compliance (remove unittest.mock, add tests.helpers.env_override and stdlib redirects) - Extract testable parsing methods in package managers and inspector - Add tests/test_security.py with 5 negative AppSec test cases (Zip Slip, Root Gate, Tampering SHA-256, Secret Sanitization, and AST check for zero shell=True) - Expand is_sanitized_target to redact all private SSH/TLS keys (*.key, *.pem, id_*) - Split Gitea CI/CD pipelines into test-and-lint.yaml and build-and-release.yaml - Add .gitleaks.toml configuration - Formally close v1.0.0 genesis plan in docs/plans/linux-backuper/00-status.md as immutable ADR - Bump version to v2.0.0 across __init__.py, man page, tests, and documentation
42 lines
1.5 KiB
Python
42 lines
1.5 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 parse_package_output(self, output: str) -> List[PackageItem]:
|
|
"""Parse raw dpkg-query stdout into a sorted list of PackageItem instances."""
|
|
packages: List[PackageItem] = []
|
|
for line in output.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
|
|
|
|
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 []
|
|
return self.parse_package_output(res.stdout)
|
|
except (OSError, FileNotFoundError):
|
|
return []
|