]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Extend spawn() slightly
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 13 Oct 2023 11:13:07 +0000 (13:13 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 13 Oct 2023 11:29:54 +0000 (13:29 +0200)
Let's support more of the options we also support in run() and clean
up the function a bit.

mkosi/run.py

index 9f726a79a00496157db49fdbb7f94e631ef667f4..c54b5dfd0253f2f608250dde0b66650e8507f8b3 100644 (file)
@@ -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