From: Daan De Meyer Date: Fri, 13 Oct 2023 11:13:07 +0000 (+0200) Subject: Extend spawn() slightly X-Git-Tag: v19~79^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb2aaad7c2309a4f10af93c2b24ea660e433d1cf;p=thirdparty%2Fmkosi.git Extend spawn() slightly Let's support more of the options we also support in run() and clean up the function a bit. --- diff --git a/mkosi/run.py b/mkosi/run.py index 9f726a79a..c54b5dfd0 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -18,7 +18,7 @@ import sys import tempfile import textwrap import threading -from collections.abc import Awaitable, Mapping, Sequence +from collections.abc import Awaitable, Collection, Mapping, Sequence from pathlib import Path from types import TracebackType from typing import Any, Optional @@ -231,10 +231,14 @@ def spawn( stderr: _FILE = None, user: Optional[int] = None, group: Optional[int] = None, - pass_fds: Sequence[int] = (), + pass_fds: Collection[int] = (), + env: Mapping[str, str] = {}, + log: bool = True, ) -> Popen: + cmdline = [os.fspath(x) for x in cmdline] + if ARG_DEBUG.get(): - logging.info(f"+ {shlex.join(os.fspath(s) for s in cmdline)}") + logging.info(f"+ {shlex.join(cmdline)}") if not stdout and not stderr: # Unless explicit redirection is done, print all subprocess @@ -252,13 +256,17 @@ def spawn( user=user, group=group, pass_fds=pass_fds, + env=env, preexec_fn=foreground, ) except FileNotFoundError: die(f"{cmdline[0]} not found in PATH.") except subprocess.CalledProcessError as e: - logging.error(f"\"{shlex.join(os.fspath(s) for s in cmdline)}\" returned non-zero exit code {e.returncode}.") + if log: + logging.error(f"\"{shlex.join(cmdline)}\" returned non-zero exit code {e.returncode}.") raise e + finally: + foreground(new_process_group=False) # https://github.com/torvalds/linux/blob/master/include/uapi/linux/capability.h