Supress tracebacks for "expected" exceptions in more places

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-10-13 13:10:57 +02:00 committed by zbyszek
parent 1b5ad32178
commit aa849b8aa5
2 changed files with 18 additions and 7 deletions

View file

@ -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()

View file

@ -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)