From e436fe83ecaa1e406c0970315ceef4ce0bc7a8b6 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: May 20 2016 19:48:54 +0000 Subject: Call rpmspec with macros defined using --define --- diff --git a/spectool b/spectool index 599cd23..4c6069b 100755 --- a/spectool +++ b/spectool @@ -1,5 +1,6 @@ #!/usr/bin/python3 import argparse +import collections import operator import glob import re @@ -34,6 +35,7 @@ dbprint = None USER_AGENT = 'spectool/' + __version__ +DEFINES = collections.OrderedDict() # Sure wish I had Python 3.5's subprocess.run(), so here's a hacked one. class CompletedProcess(object): @@ -112,11 +114,8 @@ class Spec(object): def parse(self): """Call rpmspec and process the result, looking for intersting tags.""" cmdline = ['rpmspec', '-P', self.spec] - try: - proc = run(cmdline) - except ProcError as e: - error('Error: rpmspec call failed!', e) - for line in proc.stdout.splitlines(): + stdout = run_rpm_cmd(cmdline) + for line in stdout.splitlines(): self.parseline(line) def parseline(self, line): @@ -289,26 +288,28 @@ def parseopts(): return opts - def check_rpmspec(): try: run(['rpmspec', '--version']) except CalledProcessError: error("rpmspec does not appear to be installed.") +def run_rpm_cmd(cmdline): + for macro,value in DEFINES.items(): + cmdline.extend(['--define', '{} {}'.format(macro, value)]) + try: + proc = run(cmdline) + except ProcError as e: + error('Error: {} call failed!'.format(cmdline[0])) + return proc.stdout def expand_sourcedir_macro(spec): cmdline = ['rpm'] for arg in 'epoch', 'name', 'release', 'version': cmdline.extend(['--define', '{} {}'.format(arg, getattr(spec, arg))]) cmdline.extend(['--eval', '%_sourcedir']) - - try: - proc = run(cmdline) - except ProcError as e: - error('Error: rpm call failed!') - return proc.stdout.strip() - + stdout = run_rpm_cmd(cmdline) + return stdout.split() def get_download_location(spec, opts): dir = '.' @@ -477,9 +478,19 @@ def show_parsed_data(spec, opts): print("-> Patches:\n -> {}".format('\n -> '.join(spec.patches))) print() +def update_defines(defines): + if defines is None: + return + + for define in defines: + macro, _, value = define.partition(' ') + if value is None: + error('Cannot parse definition {!r}'.format(define)) + DEFINES[macro] = value def main(): opts = parseopts() + update_defines(opts.defines) check_rpmspec() spec = Spec(opts.spec)