From 95fb02d8f71d27445f22fe81ea5661af048f3ed1 Mon Sep 17 00:00:00 2001 From: Otto Liljalaakso Date: Apr 25 2023 22:41:06 +0000 Subject: Use release's rpmdefines in unused sources check Conditional Source: tags are problematic and, in fact, forbidden in at least Fedora. However, there are packages that conditionalize packages based on macros such as %{rhel} or %{fedora}. 'x-pkg sources' did not handle such packages correctly, because when the specfile was parsed to check for unused sources, values for those macros were not set. This was different from other commands which set such macros based on the value of --release parameter or Git branch name. Improve support for conditional Source: tags by using the standard set of rpmdefines when the specfile is parsed in 'fedpkg sources'. Fixes #671 Signed-off-by: Otto Liljalaakso --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 3f934d3..817ef33 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -2261,13 +2261,22 @@ class Commands(object): sourcesf = SourcesFile(self.sources_filename, self.source_entry_type) try: - specf = SpecFile(os.path.join(self.layout.specdir, self.spec), - self.layout.sourcedir) - spec_parsed = True - except Exception: - self.log.warning("Parsing specfile for used sources failed. " - "Falling back to downloading all sources.") + # Try resolving rpmdefines separately. This produces a clear error + # message in the common failure case of custom branch name. + self.rpmdefines + except Exception as err: + self.log.warning("Parsing specfile for used sources failed: %s" % err) + self.log.warning("Falling back to downloading all sources.") spec_parsed = False + else: + try: + specf = SpecFile(os.path.join(self.layout.specdir, self.spec), + self.rpmdefines) + spec_parsed = True + except Exception: + self.log.warning("Parsing specfile for used sources failed. " + "Falling back to downloading all sources.") + spec_parsed = False args = dict() if self.lookaside_request_params: diff --git a/pyrpkg/spec.py b/pyrpkg/spec.py index d72f1fb..5400de3 100644 --- a/pyrpkg/spec.py +++ b/pyrpkg/spec.py @@ -18,16 +18,16 @@ class SpecFile(object): r'^((source[0-9]*|patch[0-9]*)\s*:\s*(?P.*))\s*$', re.IGNORECASE) - def __init__(self, spec, sourcedir): + def __init__(self, spec, rpmdefines): self.spec = spec - self.sourcedir = sourcedir + self.rpmdefines = rpmdefines self.sources = [] self.parse() def parse(self): """Call rpmspec and find source tags from the result.""" - stdout = run(self.spec, self.sourcedir) + stdout = run(self.spec, self.rpmdefines) for line in stdout.splitlines(): m = self.sourcefile_expression.match(line) if not m: @@ -38,8 +38,10 @@ class SpecFile(object): self.sources.append(val) -def run(spec, sourcedir): - cmdline = ['rpmspec', '--define', "_sourcedir %s" % sourcedir, '-P', spec] +def run(spec, rpmdefines): + cmdline = ['rpmspec'] + cmdline.extend(rpmdefines) + cmdline.extend(['-P', spec]) try: process = subprocess.Popen(cmdline, stdout=subprocess.PIPE,