exec/cmd: run_cmd(): add new params: stdin, stdin_input, check asddsadsa

This commit is contained in:
InsanePrawn 2023-07-08 18:45:09 +02:00
parent 1e446e6f80
commit d6900172fe
2 changed files with 27 additions and 4 deletions

View file

@ -60,9 +60,12 @@ class AbstractChroot(Protocol):
capture_output: bool,
cwd: str,
fail_inactive: bool,
switch_user: Optional[str],
stdout: Optional[FileDescriptor],
stderr: Optional[FileDescriptor],
switch_user: Optional[str],
stdin: Optional[FileDescriptor],
stdin_input: Optional[str],
check: Optional[bool],
):
pass
@ -226,9 +229,12 @@ class Chroot(AbstractChroot):
capture_output: bool = False,
cwd: Optional[str] = None,
fail_inactive: bool = True,
switch_user: Optional[str] = None,
stdout: Optional[FileDescriptor] = None,
stderr: Optional[FileDescriptor] = None,
switch_user: Optional[str] = None,
stdin: Optional[FileDescriptor] = None,
stdin_input: Optional[str] = None,
check: Optional[bool] = None,
) -> Union[int, subprocess.CompletedProcess]:
if not self.active and fail_inactive:
raise Exception(f'Chroot {self.name} is inactive, not running command! Hint: pass `fail_inactive=False`')
@ -245,13 +251,23 @@ class Chroot(AbstractChroot):
script = flatten_shell_script(script, shell_quote_items=False, wrap_in_shell_quote=False)
if cwd:
script = f"cd {shell_quote(cwd)} && ( {script} )"
if switch_user:
if switch_user and switch_user != 'root':
inner_cmd = generate_cmd_su(script, switch_user=switch_user, elevation_method='none', force_su=True)
else:
inner_cmd = wrap_in_bash(script, flatten_result=False)
cmd = flatten_shell_script(['chroot', self.path] + env_cmd + inner_cmd, shell_quote_items=True)
return run_root_cmd(cmd, env=outer_env, attach_tty=attach_tty, capture_output=capture_output, stdout=stdout, stderr=stderr)
return run_root_cmd(
cmd,
env=outer_env,
attach_tty=attach_tty,
capture_output=capture_output,
stdout=stdout,
stderr=stderr,
stdin=stdin,
stdin_input=stdin_input,
check=check,
)
def mount_pkgbuilds(self, fail_if_mounted: bool = False) -> str:
return self.mount(

View file

@ -97,6 +97,9 @@ def run_cmd(
elevation_method: Optional[ElevationMethod] = None,
stdout: Optional[FileDescriptor] = None,
stderr: Optional[FileDescriptor] = None,
stdin: Optional[FileDescriptor] = None,
stdin_input: Optional[str] = None,
check: Optional[bool] = None,
) -> Union[CompletedProcess, int]:
"execute `script` as `switch_user`, elevating and su'ing as necessary"
kwargs: dict = {}
@ -111,6 +114,10 @@ def run_cmd(
for name, fd in {'stdout': stdout, 'stderr': stderr}.items():
if fd is not None:
kwargs[name] = fd
for name, value in {'stdin': stdin, 'input': stdin_input, 'check': check}.items():
if value is not None:
kwargs[name] = value
script = flatten_shell_script(script)
if cwd:
kwargs['cwd'] = cwd