From f392ea79caf5f7b99cd405d5300e4f9e089c9b91 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Feb 19 2016 17:41:40 +0000 Subject: [PATCH 1/2] When cwd contains just one spec file, just use it At least with dist-git there's almost always just one spec file in given directory, so there's no need to force the user to give the name. --- diff --git a/spectool b/spectool index 31d44be..0e3178a 100755 --- a/spectool +++ b/spectool @@ -1,6 +1,7 @@ #!/usr/bin/python3 import argparse import operator +import glob import re import sys from subprocess import CalledProcessError, PIPE, Popen, TimeoutExpired @@ -196,7 +197,7 @@ def parseopts(): epilog="Files:\n/etc/rpmdevrools/curlrc\n optional curl(1) configuration", formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False) - parser.add_argument('spec', help='The specfile to be parsed') + parser.add_argument('spec', help='The specfile to be parsed', nargs='?') mode = parser.add_argument_group('Operating mode') mode1 = mode.add_mutually_exclusive_group() @@ -241,6 +242,16 @@ def parseopts(): opts = parser.parse_args() + 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) + if len(files) > 1: + print("Spec-file was not specified and current directory contains multiple") + sys.exit(1) + opts.spec = files[0] + # Can argparse do this for me? if opts.allsources or opts.allpatches: opts.all = False From 75b6e125d246699fc3c7b5f4af6283918207d547 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Feb 19 2016 17:46:47 +0000 Subject: [PATCH 2/2] 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()