From: Daan De Meyer Date: Mon, 7 Mar 2022 19:38:49 +0000 (+0000) Subject: Centralize usage of suppress_stacktrace() and propagate returncode X-Git-Tag: v13~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be90fd1d3b6e1dca7dd82c46f161e0c8571ee9bb;p=thirdparty%2Fmkosi.git Centralize usage of suppress_stacktrace() and propagate returncode Let's centralize the usage of suppress_stacktrace() to run_verb() and make sure we properly propagate the returncode of the command that failed. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index bd3e3a2b6..521335105 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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) diff --git a/mkosi/__main__.py b/mkosi/__main__.py index ce4f630df..46eaccb2d 100644 --- a/mkosi/__main__.py +++ b/mkosi/__main__.py @@ -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) diff --git a/tests/test_machine.py b/tests/test_machine.py index 5babf8081..531b494cd 100644 --- a/tests/test_machine.py +++ b/tests/test_machine.py @@ -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