]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Replace _run() with an extra argument log
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 18 Apr 2023 20:11:58 +0000 (22:11 +0200)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Wed, 19 Apr 2023 07:30:18 +0000 (09:30 +0200)
Let's just add an argument log to run that's true by default which
allows us to disable logging if the command failed. We also disable
logging in run_ssh(), run_qemu() and run_shell() since we expect
those to fail and shouldn't log if they do (like we did before).

mkosi/__init__.py
mkosi/run.py

index a764c3f334b3cbdddd84021cfd30aedcead822cb..6a07c90b0e4f9c83ac43ae4e66e882e59ddd9ec7 100644 (file)
@@ -1994,7 +1994,7 @@ def run_shell(config: MkosiConfig) -> None:
         acl_toggle_remove(config, config.output, uid, allow=False)
 
     try:
-        run(cmdline, stdout=sys.stdout, env=os.environ)
+        run(cmdline, stdout=sys.stdout, env=os.environ, log=False)
     finally:
         if config.output_format == OutputFormat.directory:
             acl_toggle_remove(config, config.output, uid, allow=True)
@@ -2259,7 +2259,7 @@ def run_qemu(config: MkosiConfig) -> None:
         cmdline += config.qemu_args
         cmdline += config.cmdline
 
-        run(cmdline, stdout=sys.stdout, env=os.environ)
+        run(cmdline, stdout=sys.stdout, env=os.environ, log=False)
 
 
 def run_ssh(config: MkosiConfig) -> None:
@@ -2275,7 +2275,7 @@ def run_ssh(config: MkosiConfig) -> None:
 
     cmd += config.cmdline
 
-    run(cmd, stdout=sys.stdout, env=os.environ)
+    run(cmd, stdout=sys.stdout, env=os.environ, log=False)
 
 
 def run_serve(config: MkosiConfig) -> None:
index b6ff3f757a256becdf9f4322745989949ddfa1a6..c7b069e86430cf62cd7e5cc5f22aafee0e00308e 100644 (file)
@@ -187,12 +187,13 @@ def fork_and_wait(target: Callable[[], T]) -> T:
 
     return result
 
-def _run(
+def run(
     cmdline: Sequence[PathString],
     check: bool = True,
     stdout: _FILE = None,
     stderr: _FILE = None,
     env: Mapping[str, PathString] = {},
+    log: bool = True,
     **kwargs: Any,
 ) -> CompletedProcess:
     if "run" in ARG_DEBUG:
@@ -223,20 +224,10 @@ def _run(
                               preexec_fn=foreground)
     except FileNotFoundError as e:
         die(f"{cmdline[0]} not found in PATH.", e)
-
-
-def run(
-    cmdline: Sequence[PathString],
-    check: bool = True,
-    stdout: _FILE = None,
-    stderr: _FILE = None,
-    env: Mapping[str, PathString] = {},
-    **kwargs: Any,
-) -> CompletedProcess:
-    try:
-        return _run(cmdline, check, stdout, stderr, env, **kwargs)
     except subprocess.CalledProcessError as e:
-        die(f"\"{shlex.join(str(s) for s in cmdline)}\" returned non-zero exit code {e.returncode}.", e)
+        if log:
+            die(f"\"{shlex.join(str(s) for s in cmdline)}\" returned non-zero exit code {e.returncode}.", e)
+        raise e
 
 
 def spawn(
@@ -304,12 +295,13 @@ def run_with_apivfs(
     template = f"chmod 1777 {state.root / 'tmp'} {state.root / 'var/tmp'} {state.root / 'dev/shm'} && exec {{}} || exit $?"
 
     try:
-        return _run([*cmdline, template.format(shlex.join(str(s) for s in cmd))],
-                   text=True, stdout=stdout, env=env)
+        return run([*cmdline, template.format(shlex.join(str(s) for s in cmd))],
+                   text=True, stdout=stdout, env=env, log=False)
     except subprocess.CalledProcessError as e:
         if "run" in ARG_DEBUG:
-            _run([*cmdline, template.format("sh")], check=False, env=env)
-        raise e
+            run([*cmdline, template.format("sh")], check=False, env=env, log=False)
+        die(f"\"{shlex.join(str(s) for s in cmd)}\" returned non-zero exit code {e.returncode}.")
+
 
 def run_workspace_command(
     state: MkosiState,
@@ -361,12 +353,12 @@ def run_workspace_command(
     template = "chmod 1777 /tmp /var/tmp /dev/shm && PATH=$PATH:/usr/bin:/usr/sbin exec {} || exit $?"
 
     try:
-        return _run([*cmdline, template.format(shlex.join(str(s) for s in cmd))],
-                   text=True, stdout=stdout, env=env)
+        return run([*cmdline, template.format(shlex.join(str(s) for s in cmd))],
+                   text=True, stdout=stdout, env=env, log=False)
     except subprocess.CalledProcessError as e:
         if "run" in ARG_DEBUG:
-            _run([*cmdline, template.format("sh")], check=False, env=env)
-        raise e
+            run([*cmdline, template.format("sh")], check=False, env=env, log=False)
+        die(f"\"{shlex.join(str(s) for s in cmd)}\" returned non-zero exit code {e.returncode}.")
     finally:
         if state.workspace.joinpath("resolv.conf").is_symlink():
             resolve.unlink(missing_ok=True)