lib.ExecContext: Fix ignored --interactive

Whether or not the CallContext.interactive property should be True or
False, and hence, a call should be processed interactively, depends
on multiple factors, constituting matrix of options with multiple
preferences.

  --interactive is the application default and can be true, false,
    or auto

  - A call can be explicitly invoked as interactive, non-interactive
    or auto via the cmd_input parameter to ExecContext.run()

This commit adds more "mode:" options to make the latter more
explicit. It takes preference over the global --interactive
parameter: Global --interactive is only given a chance to decide if
cmd_input is None (default) or mode:opt-interactive.

This commit also fixes a bug: --interactive is ignored because the
interactive argument passed to ExecContext's constructor is ignored
later on in calls to the wrapped _run() and _sudo() methods.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-21 10:40:37 +01:00
commit bb13aea694

View file

@ -37,7 +37,6 @@ class ExecContext(abc.ABC):
cmd_input: str|None, cmd_input: str|None,
wd: str|None, wd: str|None,
log_prefix: str, log_prefix: str,
interactive: bool|None,
throw: bool, throw: bool,
verbose: bool verbose: bool
) -> None: ) -> None:
@ -47,13 +46,29 @@ class ExecContext(abc.ABC):
delim_len = 120 delim_len = 120
self.__delim += '-' * max(0, delim_len - len(self.__delim)) self.__delim += '-' * max(0, delim_len - len(self.__delim))
self.__cmd = cmd self.__cmd = cmd
self.__cmd_input = cmd_input
self.__wd = wd self.__wd = wd
self.__log_prefix = log_prefix self.__log_prefix = log_prefix
self.__interactive = interactive if interactive is not None else (
cmd_input == "mode:interactive" # -- At the end of this dance, interactive needs to be either True
or (cmd_input == "mode:auto" and sys.stdin.isatty()) # or False
) interactive: bool|None = None
match cmd_input:
case 'mode:interactive':
interactive = True
case 'mode:non-interactive':
interactive = False
case 'mode:opt-interactive':
interactive = parent.interactive
case 'mode:auto':
interactive = sys.stdin.isatty()
if interactive is None:
interactive = parent.interactive
if interactive is None:
interactive = sys.stdin.isatty()
assert interactive in [ True, False ]
self.__interactive = interactive
self.__cmd_input = cmd_input if cmd_input[:5] != 'mode:' else None
self.__throw = throw self.__throw = throw
self.__verbose = verbose if verbose is not None else parent.verbose_default self.__verbose = verbose if verbose is not None else parent.verbose_default
self.__pretty_cmd: str|None = None self.__pretty_cmd: str|None = None
@ -116,7 +131,7 @@ class ExecContext(abc.ABC):
raise e raise e
return result return result
def __init__(self, uri: str, interactive: bool=True, verbose_default=False): def __init__(self, uri: str, interactive: bool|None=None, verbose_default=False):
self.__uri = uri self.__uri = uri
self.__interactive = interactive self.__interactive = interactive
self.__verbose_default = verbose_default self.__verbose_default = verbose_default
@ -127,7 +142,7 @@ class ExecContext(abc.ABC):
return self.__uri return self.__uri
@property @property
def interactive(self) -> bool: def interactive(self) -> bool|None:
return self.__interactive return self.__interactive
@property @property
@ -144,7 +159,7 @@ class ExecContext(abc.ABC):
wd: str|None = None, wd: str|None = None,
throw: bool = True, throw: bool = True,
verbose: bool|None = None, verbose: bool|None = None,
cmd_input: str|None = None, cmd_input: str|None = 'mode:opt-interactive',
env: dict[str, str]|None = None, env: dict[str, str]|None = None,
title: str=None title: str=None
) -> Result: ) -> Result:
@ -157,10 +172,12 @@ class ExecContext(abc.ABC):
throw: Raise an exception on non-zero exit status if True throw: Raise an exception on non-zero exit status if True
verbose: Emit log output while the command runs verbose: Emit log output while the command runs
cmd_input: cmd_input:
- None -> stdin from /dev/null - "mode:opt-interactive" -> Let --interactive govern how to handle interactivity (default)
- "mode:interactive" -> Inherit terminal stdin - "mode:interactive" -> Inherit terminal stdin
- "mode:auto" -> Inherit terminal stdin if it is a TTY - "mode:auto" -> Inherit terminal stdin if it is a TTY
- otherwise -> String fed to stdin - "mode:non-interactive" -> stdin from /dev/null
- None -> Alias for mode:non-interactive
- otherwise -> Feed cmd_input to stdin
env: The environment the command should be run in env: The environment the command should be run in
Returns: Returns:
@ -170,7 +187,7 @@ class ExecContext(abc.ABC):
ret = Result(None, None, 1) ret = Result(None, None, 1)
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, wd=wd, with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, wd=wd,
log_prefix='|', interactive=None, throw=throw, verbose=verbose) as cc: log_prefix='|', throw=throw, verbose=verbose) as cc:
try: try:
ret = await self._run( ret = await self._run(
cmd=cmd, cmd=cmd,
@ -205,7 +222,7 @@ class ExecContext(abc.ABC):
if mod_env is None: if mod_env is None:
mod_env = {} mod_env = {}
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, wd=wd, with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, wd=wd,
log_prefix='|', interactive=None, throw=throw, verbose=verbose) as cc: log_prefix='|', throw=throw, verbose=verbose) as cc:
try: try:
ret = await self._sudo( ret = await self._sudo(
cmd=cmd, cmd=cmd,