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>
2026-03-05 17:33:52 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import Iterable
|
|
|
|
|
|
|
|
|
|
import abc, importlib
|
|
|
|
|
|
|
|
|
|
from .ExecContext import ExecContext, Result
|
|
|
|
|
from .Package import Package
|
|
|
|
|
from .log import *
|
|
|
|
|
|
|
|
|
|
class Distro(abc.ABC):
|
|
|
|
|
|
|
|
|
|
def __init__(self, ec: ExecContext):
|
|
|
|
|
assert ec is not None
|
|
|
|
|
self.__exec_context = ec
|
2026-03-07 11:15:16 +01:00
|
|
|
self.__id: str|None = None
|
|
|
|
|
|
|
|
|
|
def __set_id(self, id: str) -> None:
|
|
|
|
|
self.__id = id
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
|
|
|
|
|
# == Load
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def instantiate(cls, distro_id: str, *args, **kwargs):
|
|
|
|
|
backend_id = distro_id.lower().replace('-', '_')
|
|
|
|
|
match backend_id:
|
|
|
|
|
case 'ubuntu' | 'raspbian' | 'kali':
|
|
|
|
|
backend_id = 'debian'
|
|
|
|
|
case 'centos':
|
|
|
|
|
backend_id = 'redhat'
|
|
|
|
|
case 'opensuse' | 'suse':
|
|
|
|
|
backend_id = 'suse'
|
|
|
|
|
module_path = 'jw.pkg.lib.distros.' + backend_id + '.Distro'
|
|
|
|
|
try:
|
|
|
|
|
module = importlib.import_module(module_path)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log(ERR, f'Failed to import Distro module {module_path} ({str(e)})')
|
|
|
|
|
raise
|
|
|
|
|
cls = getattr(module, 'Distro')
|
2026-03-07 11:15:16 +01:00
|
|
|
ret = cls(*args, **kwargs)
|
|
|
|
|
ret.__set_id(backend_id)
|
|
|
|
|
return ret
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
|
|
|
|
|
# == Convenience methods
|
|
|
|
|
|
2026-03-07 11:15:16 +01:00
|
|
|
@property
|
|
|
|
|
def id(self) -> str:
|
|
|
|
|
return self.__id
|
|
|
|
|
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
@property
|
|
|
|
|
def ctx(self) -> ExecContext:
|
|
|
|
|
return self.__exec_context
|
|
|
|
|
|
|
|
|
|
def run(self, *args, **kwargs) -> Result:
|
|
|
|
|
return self.__exec_context.run(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def sudo(self, *args, **kwargs) -> Result:
|
|
|
|
|
return self.__exec_context.sudo(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def interactive(self) -> bool:
|
|
|
|
|
return self.__exec_context.interactive
|
|
|
|
|
|
|
|
|
|
# == Distribution specific methods
|
|
|
|
|
|
|
|
|
|
# -- ref
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _ref(self) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def ref(self) -> None:
|
|
|
|
|
return await self._ref()
|
|
|
|
|
|
|
|
|
|
# -- dup
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _dup(self, download_only: bool) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def dup(self, download_only: bool=False) -> None:
|
|
|
|
|
return await self._dup(download_only=download_only)
|
|
|
|
|
|
|
|
|
|
# -- reboot_required
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _reboot_required(self, verbose: bool) -> bool:
|
|
|
|
|
pass
|
|
|
|
|
|
2026-03-06 16:50:27 +01:00
|
|
|
async def reboot_required(self, verbose: bool|None=None) -> bool:
|
|
|
|
|
if verbose is None:
|
|
|
|
|
verbose = self.ctx.verbose_default
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
return await self._reboot_required(verbose=verbose)
|
|
|
|
|
|
|
|
|
|
# -- select
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _select(self, names: Iterable[str]) -> Iterable[Package]:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def select(self, names: Iterable[str] = []) -> Iterable[Package]:
|
|
|
|
|
return await self._select(names)
|
|
|
|
|
|
|
|
|
|
# -- install
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _install(self, names: Iterable[str], only_update: bool) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def install(self, names: Iterable[str], only_update: bool=False) -> None:
|
2026-03-15 15:14:56 +01:00
|
|
|
if not names:
|
|
|
|
|
log(WARNING, f'No packages specified for installation')
|
|
|
|
|
return
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
return await self._install(names, only_update=only_update)
|
|
|
|
|
|
|
|
|
|
# -- delete
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _delete(self, names: Iterable[str]) -> None:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def delete(self, names: Iterable[str]) -> None:
|
2026-03-15 15:14:56 +01:00
|
|
|
if not names:
|
|
|
|
|
log(WARNING, f'No packages specified for deletion')
|
|
|
|
|
return
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
return await self._delete(names)
|
|
|
|
|
|
|
|
|
|
# -- pkg_files
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _pkg_files(self, name: str) -> Iterable[str]:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def pkg_files(self, name: str) -> Iterable[str]:
|
2026-03-17 07:27:52 +01:00
|
|
|
if not name:
|
|
|
|
|
log(WARNING, f'No package specified for inspection')
|
2026-03-15 15:14:56 +01:00
|
|
|
return []
|
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>
2026-03-05 17:33:52 +01:00
|
|
|
return await self._pkg_files(name)
|