From 75b6e125d246699fc3c7b5f4af6283918207d547 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Feb 19 2016 17:46:47 +0000 Subject: Print errors to stderr --- diff --git a/spectool b/spectool index 0e3178a..565550c 100755 --- a/spectool +++ b/spectool @@ -78,6 +78,11 @@ def run(*popenargs, timeout=None, **kwargs): raise ProcError(process.args, retcode, stdout=stdout, stderr=stderr) return CompletedProcess(process.args, retcode, stdout, stderr) +def error(message, exception=None): + print(message, file=sys.stderr) + if exception is not None: + print(e, file=sys.stderr) + sys.exit(1) class Spec(object): """Object to encapsulate a spec file and provide the info we want.""" @@ -102,9 +107,7 @@ class Spec(object): try: proc = run(cmdline) except ProcError as e: - print('Error: rpmspec call failed!') - print(e) - sys.exit(1) + error('Error: rpmspec call failed!', e) for line in proc.stdout.splitlines(): self.parseline(line) @@ -187,8 +190,7 @@ def parseopts(): try: out.append(int(j)) except ValueError: - print('Invalid value: {}'.format(j)) - sys.exit(1) + error('Invalid value: {}'.format(j)) return out parser = argparse.ArgumentParser( @@ -245,11 +247,9 @@ def parseopts(): if opts.spec is None: files = glob.glob('*.spec') if not files: - print("Spec-file was not specified and current directory does not contain any") - sys.exit(1) + error("Spec-file was not specified and current directory does not contain any") if len(files) > 1: - print("Spec-file was not specified and current directory contains multiple") - sys.exit(1) + error("Spec-file was not specified and current directory contains multiple") opts.spec = files[0] # Can argparse do this for me? @@ -283,8 +283,7 @@ def check_rpmspec(): try: run(['rpmspec', '--version']) except CalledProcessError: - print("rpmspec does not appear to be installed.") - sys.exit(1) + error("rpmspec does not appear to be installed.") def expand_sourcedir_macro(spec): @@ -296,9 +295,7 @@ def expand_sourcedir_macro(spec): try: proc = run(cmdline) except ProcError as e: - print('Error: rpm call failed!') - print(e) - sys.exit(1) + error('Error: rpm call failed!') return proc.stdout.strip()