mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-25 17:45:55 +02:00
lib.ExecContext.run(): Push code up into base class
Take implementation burden from the derived classes _run() callback by moving the respective code into the run() wrapper methods of the base class. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
f2ffe85b61
commit
52dd3b8f21
3 changed files with 103 additions and 129 deletions
|
|
@ -2,12 +2,15 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import abc, re
|
||||
import abc, re, sys
|
||||
from typing import NamedTuple, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Self
|
||||
|
||||
from .log import *
|
||||
from .util import pretty_cmd
|
||||
|
||||
class Result(NamedTuple):
|
||||
|
||||
stdout: str|None
|
||||
|
|
@ -48,7 +51,7 @@ class ExecContext(abc.ABC):
|
|||
args: list[str],
|
||||
wd: str|None = None,
|
||||
throw: bool = True,
|
||||
verbose: bool = False,
|
||||
verbose: bool|None = None,
|
||||
cmd_input: str|None = None,
|
||||
env: dict[str, str]|None = None,
|
||||
title: str=None,
|
||||
|
|
@ -78,19 +81,73 @@ class ExecContext(abc.ABC):
|
|||
In PTY mode stderr is always None because PTY merges stdout/stderr.
|
||||
"""
|
||||
|
||||
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())
|
||||
)
|
||||
|
||||
if verbose is None:
|
||||
verbose = self.__verbose_default
|
||||
|
||||
return await self._run(
|
||||
args=args,
|
||||
wd=wd,
|
||||
throw=throw,
|
||||
verbose=self._verbose(verbose),
|
||||
cmd_input=cmd_input,
|
||||
env=env,
|
||||
title=title,
|
||||
output_encoding=output_encoding
|
||||
)
|
||||
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 + ' <')
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _sudo(self, cmd: list[str], mod_env: dict[str, str], opts: list[str], verbose: bool) -> Result:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue