From 9e4accec04314e9aa893148e7814b4873c30716c Mon Sep 17 00:00:00 2001 From: Alexander Tsamutali Date: Thu, 30 Jun 2016 21:05:37 +0300 Subject: [PATCH] Fail if specified file doesn't exist Currently if epoch file or requirements file doesn't exist, renderspec will silently proceed without this data. It's better to fail and notify user in this case. Change-Id: Ifa324b1bcc5e7926e61434adc568e0f90aa31dc7 --- renderspec/__init__.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/renderspec/__init__.py b/renderspec/__init__.py index 684246a..a79d567 100644 --- a/renderspec/__init__.py +++ b/renderspec/__init__.py @@ -183,20 +183,20 @@ def _get_default_template(): def _get_epochs(filename): """get a dictionary with pkg-name->epoch mapping""" - if os.path.exists(filename): + epochs = {} + if filename is not None: with open(filename, 'r') as f: data = yaml.safe_load(f.read()) - return dict(data['epochs']) - return {} + epochs.update(data['epochs']) + return epochs def _get_requirements(filenames): """get a dictionary with pkg-name->min-version mapping""" reqs = {} for filename in filenames: - if os.path.exists(filename): - with open(filename, 'r') as f: - reqs.update(versions.get_requirements(f.readlines())) + with open(filename, 'r') as f: + reqs.update(versions.get_requirements(f.readlines())) return reqs @@ -210,9 +210,7 @@ def process_args(): parser.add_argument("--spec-style", help="distro style you want to use. " "default: %s" % (distro), default=distro, choices=['suse', 'fedora']) - parser.add_argument("--epochs", help="yaml file with epochs listed. " - "default: %s-epochs.yaml" % (distro), - default="%s-epochs.yaml" % distro) + parser.add_argument("--epochs", help="yaml file with epochs listed.") parser.add_argument("input-template", nargs='?', help="specfile jinja2 template to render. " "default: *.spec.j2") @@ -242,8 +240,12 @@ def main(): return 2 output_fn, _, _ = input_template.rpartition('.') - epochs = _get_epochs(args['epochs']) - requirements = _get_requirements(args['requirements']) + try: + epochs = _get_epochs(args['epochs']) + requirements = _get_requirements(args['requirements']) + except IOError as e: + print(e) + return 3 spec = generate_spec(args['spec_style'], epochs, requirements, input_template) if output_fn and output_fn != '-':