mirror of
https://gitlab.com/kupfer/kupferbootstrap.git
synced 2025-02-23 05:35:44 -05:00
exec/cmd: fix up stderr and stdout handling, fix capture_output overwriting env
This commit is contained in:
parent
785e41f8b7
commit
6d6f582b71
1 changed files with 11 additions and 7 deletions
18
exec/cmd.py
18
exec/cmd.py
|
@ -5,10 +5,12 @@ import subprocess
|
||||||
|
|
||||||
from subprocess import CompletedProcess # make it easy for users of this module
|
from subprocess import CompletedProcess # make it easy for users of this module
|
||||||
from shlex import quote as shell_quote
|
from shlex import quote as shell_quote
|
||||||
from typing import Optional, Union, TypeAlias
|
from typing import IO, Optional, Union, TypeAlias
|
||||||
|
|
||||||
ElevationMethod: TypeAlias = str
|
ElevationMethod: TypeAlias = str
|
||||||
|
|
||||||
|
FileDescriptor: TypeAlias = Union[int, IO]
|
||||||
|
|
||||||
# as long as **only** sudo is supported, hardcode the default into ELEVATION_METHOD_DEFAULT.
|
# as long as **only** sudo is supported, hardcode the default into ELEVATION_METHOD_DEFAULT.
|
||||||
# when other methods are added, all mentions of ELEVATION_METHOD_DEFAULT should be replaced by a config key.
|
# when other methods are added, all mentions of ELEVATION_METHOD_DEFAULT should be replaced by a config key.
|
||||||
|
|
||||||
|
@ -89,8 +91,8 @@ def run_cmd(
|
||||||
cwd: Optional[str] = None,
|
cwd: Optional[str] = None,
|
||||||
switch_user: Optional[str] = None,
|
switch_user: Optional[str] = None,
|
||||||
elevation_method: Optional[ElevationMethod] = None,
|
elevation_method: Optional[ElevationMethod] = None,
|
||||||
stdout: Optional[int] = None,
|
stdout: Optional[FileDescriptor] = None,
|
||||||
stderr=None,
|
stderr: Optional[FileDescriptor] = None,
|
||||||
) -> Union[CompletedProcess, int]:
|
) -> Union[CompletedProcess, int]:
|
||||||
"execute `script` as `switch_user`, elevating and su'ing as necessary"
|
"execute `script` as `switch_user`, elevating and su'ing as necessary"
|
||||||
kwargs: dict = {}
|
kwargs: dict = {}
|
||||||
|
@ -99,10 +101,12 @@ def run_cmd(
|
||||||
env_cmd = generate_env_cmd(env)
|
env_cmd = generate_env_cmd(env)
|
||||||
kwargs['env'] = env
|
kwargs['env'] = env
|
||||||
if not attach_tty:
|
if not attach_tty:
|
||||||
kwargs |= {'stdout': stdout} if stdout else {'capture_output': capture_output}
|
if (stdout, stderr) == (None, None):
|
||||||
if stderr:
|
kwargs['capture_output'] = capture_output
|
||||||
kwargs['stderr'] = stderr
|
else:
|
||||||
|
for name, fd in {'stdout': stdout, 'stderr': stderr}.items():
|
||||||
|
if fd is not None:
|
||||||
|
kwargs[name] = fd
|
||||||
script = flatten_shell_script(script)
|
script = flatten_shell_script(script)
|
||||||
if cwd:
|
if cwd:
|
||||||
kwargs['cwd'] = cwd
|
kwargs['cwd'] = cwd
|
||||||
|
|
Loading…
Add table
Reference in a new issue