nixos-rebuild-ng: remove support for env in process.run_wrapper

This commit is contained in:
Thiago Kenji Okada 2024-11-25 18:49:03 +00:00
parent 866e1786e3
commit f443299c58
2 changed files with 13 additions and 26 deletions

View File

@ -42,36 +42,35 @@ def run_wrapper(
args: Sequence[str | bytes | os.PathLike[str] | os.PathLike[bytes]],
*,
check: bool, # make it explicit so we always know if the code is handling errors
env: dict[str, str] | None = None, # replaces the current environment
extra_env: dict[str, str] | None = None, # appends to the current environment
extra_env: dict[str, str] | None = None,
allow_tty: bool = True,
remote: Remote | None = None,
sudo: bool = False,
**kwargs: Unpack[RunKwargs],
) -> subprocess.CompletedProcess[str]:
"Wrapper around `subprocess.run` that supports extra functionality."
if remote and allow_tty:
# SSH's TTY will redirect all output to stdout, that may cause
# unwanted effects when used
assert not kwargs.get(
"capture_output"
), "SSH's TTY is incompatible with capture_output"
assert not kwargs.get("stderr"), "SSH's TTY is incompatible with stderr"
assert not kwargs.get("stdout"), "SSH's TTY is incompatible with stdout"
env = None
if remote:
assert env is None, "'env' can't be used with 'remote'"
if extra_env:
extra_env_args = [f"{env}={value}" for env, value in extra_env.items()]
args = ["env", *extra_env_args, *args]
if sudo:
args = ["sudo", *args]
if allow_tty:
# SSH's TTY will redirect all output to stdout, that may cause
# unwanted effects when used
assert not kwargs.get(
"capture_output"
), "SSH's TTY is incompatible with capture_output"
assert not kwargs.get("stderr"), "SSH's TTY is incompatible with stderr"
assert not kwargs.get("stdout"), "SSH's TTY is incompatible with stdout"
if allow_tty and remote.tty:
args = ["ssh", "-t", *remote.opts, remote.host, "--", *args]
else:
args = ["ssh", *remote.opts, remote.host, "--", *args]
else:
if extra_env:
env = (env or os.environ) | extra_env
env = os.environ | extra_env
if sudo:
args = ["sudo", *args]

View File

@ -51,19 +51,6 @@ def test_run(mock_run: Any) -> None:
env=None,
)
p.run_wrapper(
["test", "--with", "flags"],
check=False,
env={"FOO": "bar"},
)
mock_run.assert_called_with(
["test", "--with", "flags"],
check=False,
env={"FOO": "bar"},
text=True,
errors="surrogateescape",
)
p.run_wrapper(
["test", "--with", "flags"],
check=True,
@ -96,6 +83,7 @@ def test_run(mock_run: Any) -> None:
p.run_wrapper(
["test", "--with", "flags"],
check=False,
env={"foo": "bar"},
allow_tty=True,
remote=m.Remote("user@localhost", [], False),
capture_output=True,
)