From aa849b8aa5ef82dacbc652e91f3affa6966de3ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 13 Oct 2021 13:10:57 +0200 Subject: [PATCH] Supress tracebacks for "expected" exceptions in more places MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit afbee03f26 tried to suppress tracebacks, but I was testing it by calling 'PYTHONPATH=… python -m rust2rpm' and completely missed the fact that the setuptools entrypoint wrapper calls main() directly and is not covered by this. Also, the same should be done for cargo-inspector. --- rust2rpm/__main__.py | 23 ++++++++++++++++------- rust2rpm/inspector.py | 2 ++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index e065df1..c44b976 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -299,6 +299,20 @@ def detect_rpmautospec(default_target, spec_file): autochangelog_re = r"^\s*%(?:autochangelog|\{\??autochangelog\})\b" return any(re.match(autochangelog_re, line) for line in text.splitlines()) + +@contextlib.contextmanager +def exit_on_common_errors(): + """Suppress tracebacks on common "expected" exceptions""" + try: + yield + except requests.exceptions.HTTPError as e: + sys.exit(f'Failed to download metadata: {e}') + except subprocess.CalledProcessError as e: + cmd = shlex.join(e.cmd) + sys.exit(f'Subcommand failed with code {e.returncode}: {cmd}') + + +@exit_on_common_errors() def main(): default_target = get_default_target() @@ -473,11 +487,6 @@ def main(): with open(patch_file, "w") as fobj: fobj.writelines(diff) + if __name__ == "__main__": - try: - main() - except requests.exceptions.HTTPError as e: - sys.exit(f'Failed to download metadata: {e}') - except subprocess.CalledProcessError as e: - cmd = shlex.join(e.cmd) - sys.exit(f'Subcommand failed with code {e.returncode}: {cmd}') + main() diff --git a/rust2rpm/inspector.py b/rust2rpm/inspector.py index b59edcd..d50393e 100644 --- a/rust2rpm/inspector.py +++ b/rust2rpm/inspector.py @@ -5,6 +5,7 @@ import sys from . import Metadata from .metadata import normalize_deps, Dependency +from .__main__ import exit_on_common_errors def _get_binaries(cargo_toml): @@ -49,6 +50,7 @@ def _cargo_toml(source_path, path, exclude_vendor=True): raise FileNotFoundError(f'Cargo.toml not found for binary {binary_or_cargo_toml}') +@exit_on_common_errors() def main(): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True)