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

View file

@ -25,7 +25,7 @@ class Local(Base):
wd: str|None,
verbose: bool,
cmd_input: bytes|None,
env: dict[str, str]|None,
mod_env: dict[str, str]|None,
interactive: bool,
log_prefix: str
) -> Result:
@ -58,10 +58,10 @@ class Local(Base):
def _spawn():
# Apply env in PTY mode by temporarily updating os.environ around spawn.
if env:
if mod_env:
old_env = os.environ.copy()
try:
os.environ.update(env)
os.environ.update(mod_env)
return pty.spawn(cmd, master_read=reader)
finally:
os.environ.clear()
@ -81,17 +81,17 @@ class Local(Base):
# -- non-interactive mode
stdin = asyncio.subprocess.DEVNULL if cmd_input is None else asyncio.subprocess.PIPE
if env:
if mod_env:
new_env = os.environ.copy()
new_env.update(env)
env = new_env
new_env.update(mod_env)
mod_env = new_env
proc = await asyncio.create_subprocess_exec(
*cmd,
stdin=stdin,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=env,
env=mod_env,
)
stdout_parts: list[bytes] = []
@ -143,21 +143,6 @@ class Local(Base):
if cwd is not None:
os.chdir(cwd)
async def _sudo(self, cmd: list[str], mod_env: dict[str, str], opts: list[str], *args, **kwargs) -> Result:
env: dict[str, str]|None = None
cmd_input: bytes|None = None
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)
return await self._run(cmdline, *args, **kwargs)
async def _unlink(self, path: str) -> None:
os.unlink(path)