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

@ -69,9 +69,9 @@ async def run_cmd(
if code == 0:
return
if (throw or verbose):
msg = f'Command returned error {code}: {pretty_cmd(args, wd)}: '
msg = f'Command returned error {code}: {pretty_cmd(args, wd)}'
if stderr:
msg += stderr.strip()
msg += ': ' + stderr.strip()
if throw:
raise RuntimeError(msg)
@ -279,21 +279,9 @@ async def run_askpass(askpass_env: list[str], key: AskpassKey, host: str|None=No
return None
async def run_sudo(cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[], interactive: bool=True, verbose=True):
env: dict[str, str]|None = None
cmd_input: str|None = None
if mod_env:
env = os.environ.copy()
env.update(mod_env)
cmdline = []
if os.getuid() != 0:
cmdline.append('/usr/bin/sudo')
if env is not None:
cmdline.append('--preserve-env=' + ','.join(mod_env.keys()))
cmdline.extend(opts)
cmdline.extend(cmd)
if interactive:
cmd_input = "mode:interactive"
return await run_cmd(cmdline, throw=True, verbose=verbose, env=env, cmd_input=cmd_input)
from .ec.Local import Local
ec = Local()
return await ec.sudo(cmd, mod_env, opts, interactive, verbose)
async def get_username(args: Namespace|None=None, url: str|None=None, askpass_env: list[str]=[]) -> str: # export
url_user = None if url is None else urlparse(url).username