mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
lib.ExecContext: Code beautification
- Move _sudo() above sudo()
To have a pattern in lib.ExecContext and avoid future churn: If a
public wrapper calls a protected method, define the protected
method above the respective wrapper.
- sudo(): Make cmd_input default equal to run(): InputMode.OptInteractive
- CallContext: Expose parameters throw, wd, cmd as properties for
later use
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
3ce893b74b
commit
8ef478e63f
1 changed files with 27 additions and 15 deletions
|
|
@ -11,7 +11,6 @@ if TYPE_CHECKING:
|
|||
from types import TracebackType
|
||||
|
||||
from .log import *
|
||||
from .util import pretty_cmd
|
||||
|
||||
class InputMode(Enum):
|
||||
Interactive = auto()
|
||||
|
|
@ -49,14 +48,15 @@ class ExecContext(abc.ABC):
|
|||
throw: bool,
|
||||
verbose: bool
|
||||
) -> None:
|
||||
self.__parent = parent
|
||||
self.__title = title
|
||||
self.__delim = title if title is not None else f'---- {parent.uri}: Running {pretty_cmd(cmd, wd)} -'
|
||||
delim_len = 120
|
||||
self.__delim += '-' * max(0, delim_len - len(self.__delim))
|
||||
self.__cmd = cmd
|
||||
self.__wd = wd
|
||||
self.__log_prefix = log_prefix
|
||||
self.__parent = parent
|
||||
self.__title = title
|
||||
self.__pretty_cmd: str|None = None
|
||||
self.__delim = title if title is not None else f'---- {parent.uri}: Running {self.pretty_cmd} -'
|
||||
delim_len = 120
|
||||
self.__delim += '-' * max(0, delim_len - len(self.__delim))
|
||||
|
||||
# -- At the end of this dance, interactive needs to be either True
|
||||
# or False
|
||||
|
|
@ -80,7 +80,6 @@ class ExecContext(abc.ABC):
|
|||
self.__cmd_input = cmd_input if not isinstance(cmd_input, InputMode) else None
|
||||
self.__throw = throw
|
||||
self.__verbose = verbose if verbose is not None else parent.verbose_default
|
||||
self.__pretty_cmd: str|None = None
|
||||
|
||||
def __enter__(self) -> CallContext:
|
||||
self.log_delim(start=True)
|
||||
|
|
@ -110,9 +109,22 @@ class ExecContext(abc.ABC):
|
|||
def cmd_input(self) -> bool:
|
||||
return self.__cmd_input
|
||||
|
||||
@property
|
||||
def throw(self) -> bool:
|
||||
return self.__throw
|
||||
|
||||
@property
|
||||
def wd(self) -> str|None:
|
||||
return self.__wd
|
||||
|
||||
@property
|
||||
def cmd(self) -> list[str]:
|
||||
return self.__cmd
|
||||
|
||||
@property
|
||||
def pretty_cmd(self) -> str:
|
||||
if self.__pretty_cmd is None:
|
||||
from .util import pretty_cmd
|
||||
self.__pretty_cmd = pretty_cmd(self.__cmd, self.__wd)
|
||||
return self.__pretty_cmd
|
||||
|
||||
|
|
@ -204,19 +216,23 @@ class ExecContext(abc.ABC):
|
|||
log_prefix='|', throw=throw, verbose=verbose) as cc:
|
||||
try:
|
||||
ret = await self._run(
|
||||
cmd=cmd,
|
||||
cmd=cc.cmd,
|
||||
wd=wd,
|
||||
verbose=cc.verbose,
|
||||
cmd_input=cc.cmd_input,
|
||||
env=env,
|
||||
interactive=cc.interactive,
|
||||
log_prefix = cc.log_prefix
|
||||
log_prefix=cc.log_prefix
|
||||
)
|
||||
except Exception as e:
|
||||
return cc.exception(ret, e)
|
||||
cc.check_exit_code(ret)
|
||||
return ret
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _sudo(self, *args, **kwargs) -> Result:
|
||||
pass
|
||||
|
||||
async def sudo(
|
||||
self,
|
||||
cmd: list[str],
|
||||
|
|
@ -225,7 +241,7 @@ class ExecContext(abc.ABC):
|
|||
wd: str|None = None,
|
||||
throw: bool = True,
|
||||
verbose: bool|None = None,
|
||||
cmd_input: str|None = None,
|
||||
cmd_input: Input = InputMode.OptInteractive,
|
||||
env: dict[str, str]|None = None,
|
||||
title: str=None,
|
||||
) -> Result:
|
||||
|
|
@ -239,7 +255,7 @@ class ExecContext(abc.ABC):
|
|||
log_prefix='|', throw=throw, verbose=verbose) as cc:
|
||||
try:
|
||||
ret = await self._sudo(
|
||||
cmd=cmd,
|
||||
cmd=cc.cmd,
|
||||
mod_env=mod_env,
|
||||
opts=opts,
|
||||
wd=wd,
|
||||
|
|
@ -247,17 +263,13 @@ class ExecContext(abc.ABC):
|
|||
cmd_input=cc.cmd_input,
|
||||
env=env,
|
||||
interactive=cc.interactive,
|
||||
log_prefix = cc.log_prefix,
|
||||
log_prefix=cc.log_prefix,
|
||||
)
|
||||
except Exception as e:
|
||||
return cc.exception(ret, e)
|
||||
cc.check_exit_code(ret)
|
||||
return ret
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _sudo(self, *args, **kwargs) -> Result:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def create(cls, uri: str, *args, **kwargs) -> Self:
|
||||
tokens = re.split(r'://', uri)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue