]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Centralize usage of suppress_stacktrace() and propagate returncode
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 7 Mar 2022 19:38:49 +0000 (19:38 +0000)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 8 Mar 2022 16:55:18 +0000 (16:55 +0000)
Let's centralize the usage of suppress_stacktrace() to run_verb()
and make sure we properly propagate the returncode of the command
that failed.

mkosi/__init__.py
mkosi/__main__.py
tests/test_machine.py

index bd3e3a2b6330112845dc833d00d5597a9feccac1..5213351055c32373e6b1aab58c2a5120485ff118 100644 (file)
@@ -7366,7 +7366,7 @@ def suppress_stacktrace() -> Iterator[None]:
         yield
     except subprocess.CalledProcessError as e:
         # MkosiException is silenced in main() so it doesn't print a stacktrace.
-        raise MkosiException(e)
+        raise MkosiException() from e
 
 
 def virt_name(args: MkosiArgs) -> str:
@@ -7469,8 +7469,7 @@ def run_shell_cmdline(args: MkosiArgs) -> List[str]:
 
 
 def run_shell(args: MkosiArgs) -> None:
-    with suppress_stacktrace():
-        run(run_shell_cmdline(args), stdout=sys.stdout, stderr=sys.stderr)
+    run(run_shell_cmdline(args), stdout=sys.stdout, stderr=sys.stderr)
 
 
 def find_qemu_binary() -> str:
@@ -7661,8 +7660,7 @@ def run_qemu_cmdline(args: MkosiArgs) -> Iterator[List[str]]:
 
 def run_qemu(args: MkosiArgs) -> None:
     with run_qemu_cmdline(args) as cmdline:
-        with suppress_stacktrace():
-            run(cmdline, stdout=sys.stdout, stderr=sys.stderr)
+        run(cmdline, stdout=sys.stdout, stderr=sys.stderr)
 
 
 def interface_exists(dev: str) -> bool:
@@ -7722,8 +7720,7 @@ def run_command_image(args: MkosiArgs, commands: Sequence[str], timeout: int, ch
         return run_ssh(args, commands, check, stdout, stderr, timeout)
     else:
         cmdline = ["systemd-run", "--quiet", "--wait", "--pipe", "-M", virt_name(args), "/usr/bin/env", *commands]
-        with suppress_stacktrace():
-            return run(cmdline, check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
+        return run(cmdline, check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
 
 
 def run_ssh_cmdline(args: MkosiArgs, commands: Optional[Sequence[str]] = None) -> Sequence[str]:
@@ -7767,8 +7764,7 @@ def run_ssh(
     stderr: _FILE = sys.stderr,
     timeout: Optional[int] = None,
 ) -> CompletedProcess:
-    with suppress_stacktrace():
-        return run(run_ssh_cmdline(args, commands), check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
+    return run(run_ssh_cmdline(args, commands), check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
 
 
 def run_serve(args: MkosiArgs) -> None:
@@ -7935,14 +7931,15 @@ def run_verb(raw: argparse.Namespace) -> None:
 
         print_output_size(args)
 
-    if args.verb in (Verb.shell, Verb.boot):
-        run_shell(args)
+    with suppress_stacktrace():
+        if args.verb in (Verb.shell, Verb.boot):
+            run_shell(args)
 
-    if args.verb == Verb.qemu:
-        run_qemu(args)
+        if args.verb == Verb.qemu:
+            run_qemu(args)
 
-    if args.verb == Verb.ssh:
-        run_ssh(args)
+        if args.verb == Verb.ssh:
+            run_ssh(args)
 
     if args.verb == Verb.serve:
         run_serve(args)
index ce4f630df1f11a08555076824e11c92553eacad8..46eaccb2da0ded3c56ebd09e7e00efeb9e052068 100644 (file)
@@ -2,6 +2,7 @@
 # PYTHON_ARGCOMPLETE_OK
 import os
 import sys
+from subprocess import CalledProcessError
 
 from . import complete_step, parse_args, run_verb
 from .backend import MkosiException, die
@@ -24,7 +25,10 @@ def main() -> None:
                     run_verb(a)
             else:
                 run_verb(a)
-    except MkosiException:
+    except MkosiException as e:
+        cause = e.__cause__
+        if cause and isinstance(cause, CalledProcessError):
+            sys.exit(cause.returncode)
         sys.exit(1)
 
 
index 5babf80815d7739fd9e362c20be7306e4e853bfa..531b494cd3eed524405f57f32dd3662b3d636c1e 100644 (file)
@@ -1,11 +1,10 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 import os
-from subprocess import TimeoutExpired
+from subprocess import CalledProcessError, TimeoutExpired
 
 import pytest
 
-from mkosi.backend import MkosiException
 from mkosi.machine import Machine, MkosiMachineTest
 
 pytestmark = [
@@ -20,9 +19,9 @@ class MkosiMachineTestCase(MkosiMachineTest):
 
     def test_wrong_command(self) -> None:
         # Check = True from mkosi.backend.run(), therefore we see if an exception is raised
-        with pytest.raises(MkosiException):
+        with pytest.raises(CalledProcessError):
             self.machine.run(["NonExisting", "Command"])
-        with pytest.raises(MkosiException):
+        with pytest.raises(CalledProcessError):
             self.machine.run(["ls", "NullDirectory"])
 
         # Check = False to see if stderr and returncode have the expected values