lib.ExecContext.run(), .sudo(): Rename env

The name of the env parameter to ExecContext.run() and .sudo() is not
descriptive enough for which environment is supposed to be modified
and how, so rename and split it up as follows:

  - .run(): env -> mod_env

  - .sudo(): env -> mod_env_sudo and mod_env_cmd

The parameters have the following meaning:

  - "mod_env*" means that the environment is modified, not replaced

  - "mod_env" and "mod_env_cmd" modify the environment "cmd" runs in

  - "mod_env_sudo" modifies the environment sudo runs in

Fix the fallout of the API change all over jw-pkg.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-19 14:04:35 +02:00
commit 54aecff8e4
9 changed files with 164 additions and 159 deletions

View file

@ -108,7 +108,7 @@ class ExecContext(Base):
title: str|None,
cmd: list[str],
cmd_input: Input,
env: dict[str, str]|None,
mod_env: dict[str, str]|None,
wd: str|None,
log_prefix: str,
throw: bool,
@ -123,7 +123,7 @@ class ExecContext(Base):
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))
self.__env = {'LC_ALL': 'C'} if env is None else env
self.__mod_env = {'LC_ALL': 'C'} if mod_env is None else mod_env
# -- At the end of this dance, interactive needs to be either True
# or False
@ -185,8 +185,8 @@ class ExecContext(Base):
return self.__cmd_input
@property
def env(self) -> dict[str, str]:
return self.__env
def mod_env(self) -> dict[str, str]:
return self.__mod_env
@property
def throw(self) -> bool:
@ -254,8 +254,8 @@ class ExecContext(Base):
throw: bool = True,
verbose: bool|None = None,
cmd_input: Input = InputMode.OptInteractive,
env: dict[str, str]|None = None,
title: str=None
mod_env: dict[str, str]|None = None,
title: str = None
) -> Result:
"""
Run a command asynchronously and return its output
@ -272,7 +272,7 @@ class ExecContext(Base):
- "InputMode.NonInteractive" -> stdin from /dev/null
- None -> Alias for InputMode.NonInteractive
- otherwise -> Feed cmd_input to stdin
env: The environment the command should be run in
mod_env: Change set to command's environment. key: val adds a variable, key: None removes it
Returns:
A Result instance
@ -284,7 +284,7 @@ class ExecContext(Base):
assert cmd_input is not None
ret = Result(None, None, 1)
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, env=env, wd=wd,
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, mod_env=mod_env, wd=wd,
log_prefix='|', throw=throw, verbose=verbose) as cc:
try:
ret = await self._run(
@ -292,7 +292,7 @@ class ExecContext(Base):
wd=wd,
verbose=cc.verbose,
cmd_input=cc.cmd_input,
env=cc.env,
mod_env=cc.mod_env,
interactive=cc.interactive,
log_prefix=cc.log_prefix
)
@ -301,21 +301,76 @@ class ExecContext(Base):
cc.check_exit_code(ret)
return ret
@abc.abstractmethod
async def _sudo(self, *args, **kwargs) -> Result:
pass
async def _sudo(
self,
cmd: list[str],
opts: list[str]|None,
wd: str|None,
mod_env_sudo: dict[str, str]|None,
mod_env_cmd: dict[str, str]|None,
cmd_input: bytes|None,
verbose: bool,
interactive: bool,
log_prefix: str,
) -> Result:
def __check_equal_values(d1: dict[str, str], d2: dict[str, str]) -> None:
for key, val in d1.items():
if not d2.get(key, None) in [None, val]:
raise ValueError(f'Outer and inner environments differ at least for {key}: "{val}" != "{d2.get(key)}"')
fw_cmd: list[str] = []
fw_env: dict[str, str] = {}
if opts is None:
opts = {}
if mod_env_cmd:
fw_env.update(mod_env_cmd)
if self.username != 'root':
if mod_env_sudo and mod_env_cmd:
__check_equal_values(mod_env_sudo, mod_env_cmd)
__check_equal_values(mod_env_cmd, mod_env_sudo)
fw_cmd.append('/usr/bin/sudo')
if mod_env_sudo:
fw_env.update(mod_env_sudo)
if mod_env_cmd:
fw_cmd.append('--preserve-env=' + ','.join(mod_env_cmd.keys()))
if wd is not None:
opts.extend('-D', wd)
wd = None
fw_cmd.extend(opts)
mod_env = fw_env if fw_env else None
fw_cmd.extend(cmd)
return await self._run(
fw_cmd,
wd = wd,
mod_env = mod_env,
verbose = verbose,
cmd_input = cmd_input,
interactive = interactive,
log_prefix = log_prefix
)
async def sudo(
self,
cmd: list[str],
mod_env: dict[str, str]|None=None,
opts: list[str]|None=None,
opts: list[str]|None = None,
wd: str|None = None,
mod_env_sudo: dict[str, str]|None = None,
mod_env_cmd: dict[str, str]|None = None,
throw: bool = True,
verbose: bool|None = None,
cmd_input: Input = InputMode.OptInteractive,
env: dict[str, str]|None = None,
title: str=None,
title: str = None
) -> Result:
# Note that in the calls to the wrapped method, cmd_input == None can
@ -323,27 +378,39 @@ class ExecContext(Base):
assert cmd_input is not None
ret = Result(None, None, 1)
if opts is None:
opts = {}
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, env=env, wd=wd,
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input,
mod_env=mod_env_cmd, wd=wd,
log_prefix='|', throw=throw, verbose=verbose) as cc:
try:
ret = await self._sudo(
cmd=cc.cmd,
mod_env=mod_env,
opts=opts,
wd=wd,
verbose=cc.verbose,
cmd_input=cc.cmd_input,
env=cc.env,
interactive=cc.interactive,
log_prefix=cc.log_prefix,
cmd = cc.cmd,
opts = opts,
wd = cc.wd,
mod_env_sudo = mod_env_sudo,
mod_env_cmd = cc.mod_env,
verbose = cc.verbose,
cmd_input = cc.cmd_input,
interactive = cc.interactive,
log_prefix = cc.log_prefix,
)
except Exception as e:
return cc.exception(ret, e)
cc.check_exit_code(ret)
return ret
return await self._sudo(
cmd,
opts = opts,
wd = wd,
mod_env_sudo = mod_env_sudo,
mod_env_cmd = mod_env_cmd,
throw = throw,
verbose = verbose,
cmd_input = cmd_input,
title = title,
)
async def _get(
self,
path: str,
@ -356,7 +423,7 @@ class ExecContext(Base):
if wd is not None:
path = wd + '/' + path
with self.CallContext(self, title=title, cmd=['cat', path],
cmd_input=InputMode.NonInteractive, wd=None, env=None,
cmd_input=InputMode.NonInteractive, wd=None, mod_env=None,
log_prefix='|', throw=throw, verbose=verbose) as cc:
try:
ret = await self._run(
@ -364,7 +431,7 @@ class ExecContext(Base):
wd=wd,
verbose=cc.verbose,
cmd_input=cc.cmd_input,
env=cc.env,
mod_env=cc.mod_env,
interactive=cc.interactive,
log_prefix=cc.log_prefix
)
@ -444,7 +511,7 @@ class ExecContext(Base):
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
async def __stat(opts: list[str]) -> str:
env = {
mod_env = {
'LC_ALL': 'C'
}
cmd = ['stat']
@ -452,7 +519,7 @@ class ExecContext(Base):
cmd.append('-L')
cmd.extend(opts)
cmd.append(path)
return (await self.run(cmd, env=env, throw=False,
return (await self.run(cmd, mod_env=mod_env, throw=False,
cmd_input=InputMode.NonInteractive)).decode()
# GNU coreutils stat