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)