lib.Distro, ExecContext: Add classes, refactor lib.distro

The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.

The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance.  Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-05 17:33:52 +01:00
commit 3e897f4df8
55 changed files with 426 additions and 720 deletions

View file

@ -1,14 +0,0 @@
# -*- coding: utf-8 -*-
from argparse import Namespace
from ...Cmd import Cmd
from ..BeDelete import BeDelete as Base
class Delete(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def run(self, args: Namespace):
return await self.util.rpm(['-e', *args.names], sudo=True)

View file

@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import TYPE_CHECKING
from ...Distro import Distro as Base
from ...pm.rpm import run_rpm, query_packages, list_files
if TYPE_CHECKING:
from typing import Iterable
from ...ExecContext import Result
from ...Package import Package
class Distro(Base):
async def zypper(self, args: list[str], verbose: bool=True, sudo: bool=True) -> Result:
cmd = ['/usr/bin/zypper']
if not self.interactive:
cmd.extend(['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks'])
cmd.extend(args)
if sudo:
# Run sudo --login in case /etc/profile modifies ZYPP_CONF
return await self.sudo(cmd, opts=['--login'], verbose=verbose)
return await self.run(cmd, verbose=verbose)
async def rpm(self, *args, **kwargs) -> Result:
return await run_rpm(*args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def _ref(self) -> None:
return await self.zypper(['refresh'])
async def _dup(self, download_only: bool) -> None:
args = ['dup', '--force-resolution', '--auto-agree-with-licenses']
if download_only:
args.append('--download-only')
return await self.zypper(args)
async def _reboot_required(self, verbose: bool) -> bool:
opts = []
if not verbose:
pass
#opts.append('--quiet')
opts.append('needs-rebooting')
stdout, stderr, ret = await self.zypper(opts, sudo=False, verbose=verbose)
if ret != 0:
return True
return False
async def _select(self, names: Iterable[str]) -> Iterable[Package]:
return await query_packages(names)
async def _install(self, names: Iterable[str], only_update: bool) -> None:
cmd = 'update' if only_update else 'install'
return await self.zypper([cmd, *names])
async def _delete(self, names: Iterable[str]) -> None:
return await self.rpm(['-e', *names], sudo=True)
async def _pkg_files(self, name: str) -> Iterable[str]:
return await list_files(name)

View file

@ -1,17 +0,0 @@
# -*- coding: utf-8 -*-
from argparse import Namespace
from ...Cmd import Cmd
from ..BeDup import BeDup as Base
class Dup(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def run(self, args: Namespace):
zypper_args = ['dup', '--force-resolution', '--auto-agree-with-licenses']
if args.download_only:
zypper_args.append('--download-only')
return await self.util.zypper(zypper_args)

View file

@ -1,15 +0,0 @@
# -*- coding: utf-8 -*-
from argparse import Namespace
from ...Cmd import Cmd
from ..BeInstall import BeInstall as Base
class Install(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def run(self, args: Namespace):
zypper_cmd = 'update' if args.only_update else 'install'
return await self.util.zypper([zypper_cmd, *args.packages])

View file

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
from typing import Iterable
from argparse import Namespace
from ...Package import Package
from ...pm.rpm import list_files, query_packages
from ...Cmd import Cmd
from ..BePkg import BePkg as Base
class Pkg(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def _files(self, name: str) -> Iterable[str]:
return await list_files(name)
async def _meta_data(self, names: Iterable[str]) -> Iterable[Package]:
return await query_packages(names)

View file

@ -1,22 +0,0 @@
# -*- coding: utf-8 -*-
from argparse import Namespace
from ...Cmd import Cmd
from ..BeRebootRequired import BeRebootRequired as Base
class RebootRequired(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def run(self, args: Namespace):
opts = []
if not args.verbose:
pass
#opts.append('--quiet')
opts.append('needs-rebooting')
stdout, stderr, ret = await self.util.zypper(opts, sudo=False, verbose=args.verbose)
if ret != 0:
return 1
return 0

View file

@ -1,14 +0,0 @@
# -*- coding: utf-8 -*-
from argparse import Namespace
from ...Cmd import Cmd
from ..BeRefresh import BeRefresh as Base
class Refresh(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def run(self, args: Namespace):
return await self.util.zypper(['refresh'])

View file

@ -1,17 +0,0 @@
# -*- coding: utf-8 -*-
from typing import Iterable
from argparse import Namespace
from ...Package import Package
from ...pm.rpm import query_packages
from ...Cmd import Cmd
from ..BeSelect import BeSelect as Base
class Select(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def _all_installed_packages(self) -> Iterable[Package]:
return await query_packages()

View file

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
from ...util import run_cmd
from ...Cmd import Cmd
from ..Util import Util as Base
from ...pm.rpm import run_rpm
class Util(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def zypper(self, args: list[str], verbose: bool=False, sudo: bool=True):
cmd = ['/usr/bin/zypper']
if not self.interactive:
cmd.extend(['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks'])
cmd.extend(args)
if sudo:
# Run sudo --login in case /etc/profile modifies ZYPP_CONF
return await self._sudo(cmd, opts=['--login'], verbose=verbose)
return await run_cmd(cmd, verbose=verbose)
async def rpm(self, *args, **kwargs):
return await run_rpm(*args, **kwargs)