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-06 11:45:15 +01:00
|
|
|
import os, sys, subprocess, asyncio
|
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
|
|
|
|
|
|
|
|
from ..ExecContext import ExecContext as Base
|
|
|
|
|
from ..ExecContext import Result
|
|
|
|
|
|
2026-03-06 11:45:15 +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 Local(Base):
|
|
|
|
|
|
2026-03-18 07:09:01 +01:00
|
|
|
def __init__(self, uri='local', *args, **kwargs) -> None:
|
|
|
|
|
super().__init__(uri, *args, **kwargs)
|
|
|
|
|
|
2026-03-06 11:45:15 +01:00
|
|
|
async def _run(
|
|
|
|
|
self,
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
cmd: list[str],
|
2026-03-06 17:01:05 +01:00
|
|
|
wd: str|None,
|
|
|
|
|
verbose: bool,
|
2026-04-15 14:02:44 +02:00
|
|
|
cmd_input: bytes|None,
|
2026-03-06 17:01:05 +01:00
|
|
|
env: dict[str, str]|None,
|
2026-03-18 10:22:21 +01:00
|
|
|
interactive: bool,
|
|
|
|
|
log_prefix: str
|
2026-03-06 17:01:05 +01:00
|
|
|
) -> Result:
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
def __log(prio, *args, verbose=verbose):
|
|
|
|
|
if verbose:
|
2026-03-18 10:22:21 +01:00
|
|
|
log(prio, log_prefix, *args)
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
def __make_pty_reader(collector: list[bytes], enc_for_verbose: str):
|
|
|
|
|
def _read(fd):
|
|
|
|
|
ret = os.read(fd, 1024)
|
|
|
|
|
if not ret:
|
|
|
|
|
return ret
|
|
|
|
|
collector.append(ret)
|
|
|
|
|
return ret
|
|
|
|
|
return _read
|
|
|
|
|
|
|
|
|
|
cwd: str|None = None
|
|
|
|
|
if wd is not None:
|
|
|
|
|
cwd = os.getcwd()
|
|
|
|
|
os.chdir(wd)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
# -- interactive mode
|
|
|
|
|
|
|
|
|
|
if interactive:
|
|
|
|
|
|
|
|
|
|
import pty
|
|
|
|
|
|
|
|
|
|
def _spawn():
|
|
|
|
|
# Apply env in PTY mode by temporarily updating os.environ around spawn.
|
|
|
|
|
if env:
|
|
|
|
|
old_env = os.environ.copy()
|
|
|
|
|
try:
|
|
|
|
|
os.environ.update(env)
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
return pty.spawn(cmd, master_read=reader)
|
2026-03-06 11:45:15 +01:00
|
|
|
finally:
|
|
|
|
|
os.environ.clear()
|
|
|
|
|
os.environ.update(old_env)
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
return pty.spawn(cmd, master_read=reader)
|
2026-03-06 11:45:15 +01:00
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
stdout_chunks: list[bytes] = []
|
|
|
|
|
enc_for_verbose = sys.stdout.encoding or "utf-8"
|
|
|
|
|
reader = __make_pty_reader(stdout_chunks, enc_for_verbose)
|
|
|
|
|
|
2026-03-06 11:45:15 +01:00
|
|
|
exit_code = await asyncio.to_thread(_spawn)
|
|
|
|
|
|
|
|
|
|
# PTY merges stdout/stderr
|
2026-03-18 10:22:21 +01:00
|
|
|
stdout = b"".join(stdout_chunks) if stdout_chunks else None
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
return Result(stdout, None, exit_code)
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
# -- non-interactive mode
|
2026-03-18 10:22:21 +01:00
|
|
|
stdin = asyncio.subprocess.DEVNULL if cmd_input is None else asyncio.subprocess.PIPE
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
proc = await asyncio.create_subprocess_exec(
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
*cmd,
|
2026-03-06 11:45:15 +01:00
|
|
|
stdin=stdin,
|
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
|
stderr=asyncio.subprocess.PIPE,
|
|
|
|
|
env=env,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
stdout_parts: list[bytes] = []
|
|
|
|
|
stderr_parts: list[bytes] = []
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
# -- decoding for verbose output in pipe mode
|
2026-03-18 10:22:21 +01:00
|
|
|
stdout_log_enc = sys.stdout.encoding or "utf-8"
|
|
|
|
|
stderr_log_enc = sys.stderr.encoding or "utf-8"
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
async def read_stream(stream, prio, collector: list[bytes], log_enc: str):
|
|
|
|
|
buf = b""
|
|
|
|
|
while True:
|
|
|
|
|
chunk = await stream.read(4096)
|
|
|
|
|
if not chunk:
|
|
|
|
|
break
|
|
|
|
|
collector.append(chunk)
|
|
|
|
|
if verbose:
|
|
|
|
|
buf += chunk
|
|
|
|
|
while b"\n" in buf:
|
|
|
|
|
line, buf = buf.split(b"\n", 1)
|
|
|
|
|
__log(prio, line.decode(log_enc, errors="replace"))
|
|
|
|
|
if verbose and buf:
|
|
|
|
|
# flush trailing partial line (no newline)
|
|
|
|
|
__log(prio, buf.decode(log_enc, errors="replace"))
|
|
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
|
asyncio.create_task(
|
2026-03-18 10:22:21 +01:00
|
|
|
read_stream(proc.stdout, NOTICE, stdout_parts, stdout_log_enc)
|
2026-03-06 11:45:15 +01:00
|
|
|
),
|
|
|
|
|
asyncio.create_task(
|
2026-03-18 10:22:21 +01:00
|
|
|
read_stream(proc.stderr, ERR, stderr_parts, stderr_log_enc)
|
2026-03-06 11:45:15 +01:00
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if stdin is asyncio.subprocess.PIPE:
|
2026-04-15 14:02:44 +02:00
|
|
|
proc.stdin.write(cmd_input)
|
2026-03-06 11:45:15 +01:00
|
|
|
await proc.stdin.drain()
|
|
|
|
|
proc.stdin.close()
|
|
|
|
|
|
|
|
|
|
exit_code = await proc.wait()
|
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
|
|
2026-03-18 10:22:21 +01:00
|
|
|
stdout = b"".join(stdout_parts) if stdout_parts else None
|
|
|
|
|
stderr = b"".join(stderr_parts) if stderr_parts else None
|
2026-03-06 11:45:15 +01:00
|
|
|
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
return Result(stdout, stderr, exit_code)
|
2026-03-06 11:45:15 +01:00
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
if cwd is not None:
|
|
|
|
|
os.chdir(cwd)
|
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
|
|
|
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
async def _sudo(self, cmd: list[str], mod_env: dict[str, str], opts: list[str], *args, **kwargs) -> 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
|
|
|
env: dict[str, str]|None = None
|
2026-04-15 14:02:44 +02:00
|
|
|
cmd_input: bytes|None = None
|
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
|
|
|
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)
|
lib.ExecContext: Align .sudo() prototype to .run()
ExecContext's .sudo() omits many of run()'s parameters, and this
commit adds them. To avoid redundancy around repeating and massaging
the long parameter list of both functions and their return values, it
also adds some deeper changes:
- Make run(), _run(), sudo() and _sudo() always return instances of
Result. Before it was allowed to return a triplet of stdout,
stderr, and exit status.
- Have ExecContext stay out of the business of decoding the result
entirely. Result provides a convenience method .decode()
operating on stdout and stderr and leaves the decision to the
caller.
This entails miniscule adaptations in calling code, namely in
App.os_release, util.get_profile_env() and CmdListRepos._run().
- Wrap the _run() and _sudo() callbacks in a context manager object
of type CallContext to avoid code duplication.
- Consistently name the first argument to run(), _run(), sudo() and
_sudo() "cmd", not "args". The latter suggests that the caller is
omitting the executable, which is not the case.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-19 11:38:16 +01:00
|
|
|
return await self._run(cmdline, *args, **kwargs)
|