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 -*-
|
|
|
|
|
|
2026-03-18 07:09:36 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
import abc, re, sys
|
2026-03-18 07:09:36 +01:00
|
|
|
from typing import NamedTuple, TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import Self
|
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
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
from .log import *
|
|
|
|
|
from .util import pretty_cmd
|
|
|
|
|
|
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
|
|
|
class Result(NamedTuple):
|
|
|
|
|
|
|
|
|
|
stdout: str|None
|
|
|
|
|
stderr: str|None
|
|
|
|
|
status: int|None
|
|
|
|
|
|
|
|
|
|
class ExecContext(abc.ABC):
|
|
|
|
|
|
2026-03-18 07:09:01 +01:00
|
|
|
def __init__(self, uri: str, interactive: bool=True, verbose_default=False):
|
|
|
|
|
self.__uri = uri
|
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
|
|
|
self.__interactive = interactive
|
2026-03-06 17:01:05 +01:00
|
|
|
self.__verbose_default = verbose_default
|
|
|
|
|
assert verbose_default is not None
|
|
|
|
|
|
2026-03-06 16:50:27 +01:00
|
|
|
def _verbose(self, verbose: bool|None) -> bool:
|
2026-03-06 17:01:05 +01:00
|
|
|
if verbose is not None:
|
|
|
|
|
return verbose
|
|
|
|
|
return self.__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
|
|
|
|
2026-03-18 07:09:01 +01:00
|
|
|
@property
|
|
|
|
|
def uri(self) -> str:
|
|
|
|
|
return self.__uri
|
|
|
|
|
|
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
|
2026-03-06 16:50:27 +01:00
|
|
|
def interactive(self) -> bool:
|
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 self.__interactive
|
|
|
|
|
|
2026-03-06 16:50:27 +01:00
|
|
|
@property
|
|
|
|
|
def verbose_default(self) -> bool:
|
|
|
|
|
return self.__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
|
|
|
@abc.abstractmethod
|
|
|
|
|
async def _run(self, *args, **kwargs) -> Result:
|
|
|
|
|
pass
|
|
|
|
|
|
2026-03-06 11:45:15 +01:00
|
|
|
async def run(
|
|
|
|
|
self,
|
|
|
|
|
args: list[str],
|
|
|
|
|
wd: str|None = None,
|
|
|
|
|
throw: bool = True,
|
2026-03-18 10:22:21 +01:00
|
|
|
verbose: bool|None = None,
|
2026-03-06 11:45:15 +01:00
|
|
|
cmd_input: str|None = None,
|
|
|
|
|
env: dict[str, str]|None = None,
|
|
|
|
|
title: str=None,
|
|
|
|
|
output_encoding: str|None = None, # None => unchanged; "bytes" => return raw bytes
|
|
|
|
|
) -> Result:
|
|
|
|
|
"""
|
|
|
|
|
Run a command asynchronously and return its output
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
args: Command and arguments
|
|
|
|
|
wd: Optional working directory
|
|
|
|
|
throw: Raise an exception on non-zero exit status if True
|
|
|
|
|
verbose: Emit log output while the command runs
|
|
|
|
|
cmd_input:
|
|
|
|
|
- None -> stdin from /dev/null
|
|
|
|
|
- "mode:interactive" -> Inherit terminal stdin
|
|
|
|
|
- "mode:auto" -> Inherit terminal stdin if it is a TTY
|
|
|
|
|
- otherwise -> String fed to stdin
|
|
|
|
|
output_encoding:
|
|
|
|
|
- None -> unchanged behavior (decode stdout via sys.stdout.encoding, stderr via sys.stderr.encoding)
|
|
|
|
|
- "bytes" -> return raw bytes instead of decoded strings
|
|
|
|
|
- otherwise -> decode stdout/stderr using this encoding
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
(stdout, stderr, exit_status):
|
|
|
|
|
stdout: stderr each as a string/bytes or None
|
|
|
|
|
In PTY mode stderr is always None because PTY merges stdout/stderr.
|
|
|
|
|
"""
|
2026-03-06 17:01:05 +01:00
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
def __check_exit_code(result: Result) -> None:
|
|
|
|
|
if result.status == 0:
|
|
|
|
|
return
|
|
|
|
|
if (throw or verbose):
|
|
|
|
|
msg = f'Command exited with status {code}: {pretty_cmd(args, wd)}'
|
|
|
|
|
if result.stderr:
|
|
|
|
|
msg += ': ' + result.stderr.strip()
|
|
|
|
|
if throw:
|
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
|
|
|
|
interactive = (
|
|
|
|
|
cmd_input == "mode:interactive"
|
|
|
|
|
or (cmd_input == "mode:auto" and sys.stdin.isatty())
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-06 17:01:05 +01:00
|
|
|
if verbose is None:
|
|
|
|
|
verbose = self.__verbose_default
|
|
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
if verbose:
|
|
|
|
|
delim_len = 120
|
|
|
|
|
delim = title if title is not None else f'---- {self.uri}: Running {pretty_cmd(args, wd)} -'
|
|
|
|
|
if interactive:
|
|
|
|
|
log(NOTICE, delim)
|
|
|
|
|
else:
|
|
|
|
|
delim += '-' * max(0, delim_len - len(delim))
|
|
|
|
|
log(NOTICE, ',' + delim + ' >')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
match output_encoding:
|
|
|
|
|
case 'bytes':
|
|
|
|
|
output_encoding = None
|
|
|
|
|
case None:
|
|
|
|
|
output_encoding = sys.stdout.encoding or "utf-8"
|
|
|
|
|
|
|
|
|
|
ret = Result(None, None, 1)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
ret = Result(*await self._run(
|
|
|
|
|
args=args,
|
|
|
|
|
wd=wd,
|
|
|
|
|
verbose=self._verbose(verbose),
|
|
|
|
|
cmd_input=cmd_input,
|
|
|
|
|
env=env,
|
|
|
|
|
interactive=interactive,
|
|
|
|
|
log_prefix = '|'
|
|
|
|
|
))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log(ERR, f'Failed to run {pretty_cmd(args, wd)} ({str(e)}')
|
|
|
|
|
if throw:
|
|
|
|
|
raise
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
__check_exit_code(ret)
|
|
|
|
|
|
|
|
|
|
if output_encoding is None:
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
return Result(
|
|
|
|
|
ret.stdout.decode(output_encoding, errors="replace") if ret.stdout is not None else None,
|
|
|
|
|
ret.stderr.decode(output_encoding, errors="replace") if ret.stderr is not None else None,
|
|
|
|
|
ret.status
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
if verbose and not interactive:
|
|
|
|
|
log(NOTICE, '`' + delim + ' <')
|
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
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2026-03-06 17:01:05 +01:00
|
|
|
async def _sudo(self, cmd: list[str], mod_env: dict[str, str], opts: list[str], verbose: bool) -> Result:
|
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
|
|
|
pass
|
|
|
|
|
|
2026-03-06 17:01:05 +01:00
|
|
|
async def sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[], verbose: bool|None=None) -> Result:
|
|
|
|
|
return await self._sudo(cmd, mod_env, opts, self._verbose(verbose))
|
2026-03-18 07:09:36 +01:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def create(cls, uri: str, *args, **kwargs) -> Self:
|
|
|
|
|
tokens = re.split(r'://', uri)
|
|
|
|
|
schema = tokens[0]
|
|
|
|
|
match schema:
|
|
|
|
|
case 'local':
|
|
|
|
|
from .ec.Local import Local
|
|
|
|
|
return Local(uri, *args, **kwargs)
|
|
|
|
|
case 'ssh':
|
|
|
|
|
from .SSHClient import ssh_client
|
|
|
|
|
return ssh_client(uri, *args, **kwargs)
|
|
|
|
|
case _:
|
|
|
|
|
pass
|
|
|
|
|
raise Exception(f'Can\'t create execution context for {uri} with unknown schema "{schema}"')
|