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

@ -3,6 +3,7 @@
import re, sys
from argparse import ArgumentParser
from ..lib.Distro import Distro
from ..App import App
from .Cmd import Cmd as Base
@ -12,7 +13,16 @@ class DistroBase(Base): # export
self.__id = None
super().__init__(parent, name, help)
self._add_subcommands()
self.__interactive: bool|None = None
self.__distro: Distro|None = None
@property
def distro(self) -> Distro:
if self.__distro is None:
ret = Distro.instantiate(self.distro_id, ec=self.app.exec_context)
self.__distro = ret
return self.__distro
# --------------- legacy methods
@property
def distro_id(self):
@ -28,19 +38,6 @@ class DistroBase(Base): # export
def add_arguments(self, p: ArgumentParser) -> None:
super().add_arguments(p)
p.add_argument('--id', default=None, help='Distribution ID (default is taken from /etc/os-release)')
p.add_argument('--interactive', choices=['true', 'false', 'auto'], default='true', help="Wait for user input or try to proceed unattended")
@property
def interactive(self) -> bool:
if self.__interactive is None:
match self.app.args.interactive:
case 'true':
self.__interactive = True
case 'false':
self.__interactive = False
case 'auto':
self.__interactive = sys.stdin.isatty()
return self.__interactive
async def _run(self, args):
# Missing subcommand