Skip to content

Commit

Permalink
Attempt to expand macros before processing conditions (#394)
Browse files Browse the repository at this point in the history
Attempt to expand macros before processing conditions

Fixes #391.
RELEASE NOTES BEGIN
specfile now tries to expand macros before processing conditions to be able to resolve conditional expressions defined by macros, for example OpenSUSE Tumbleweed defines %ifpython3 macro as %if "%{python_flavor}" == "python3".
RELEASE NOTES END

Reviewed-by: Maja Massarini
  • Loading branch information
2 parents 85a0b40 + 82c3d92 commit 1a40ae6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
20 changes: 17 additions & 3 deletions specfile/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def expand(s):
return True
elif keyword.endswith("arch"):
target_cpu = expand("%{_target_cpu}")
match = any(t for t in expression.split() if t == target_cpu)
match = any(t for t in expand(expression).split() if t == target_cpu)
return not match if keyword == "%ifnarch" else match
elif keyword.endswith("os"):
target_os = expand("%{_target_os}")
match = any(t for t in expression.split() if t == target_os)
match = any(t for t in expand(expression).split() if t == target_os)
return not match if keyword == "%ifnos" else match
return False

Expand All @@ -78,6 +78,15 @@ def process_conditions(
Returns:
List of tuples in the form of (line, validity).
"""

def expand(s):
if not context:
return Macros.expand(s)
result = context.expand(s, skip_parsing=getattr(expand, "skip_parsing", False))
# parse only once
expand.skip_parsing = True
return result

excluded_lines = []
if macro_definitions:
for md in macro_definitions:
Expand Down Expand Up @@ -110,7 +119,12 @@ def process_conditions(
if any(index in r for r in excluded_lines):
result.append((line, branches[-1]))
continue
m = condition_regex.match(line)
try:
expanded_line = expand(line)
except RPMException:
# ignore failed expansion and use the original line
expanded_line = line
m = condition_regex.match(expanded_line)
if not m:
result.append((line, branches[-1]))
continue
Expand Down
38 changes: 26 additions & 12 deletions tests/unit/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
import pytest
from flexmock import flexmock

import specfile.conditions
from specfile.conditions import process_conditions
from specfile.macro_definitions import MacroDefinitions
from specfile.macros import Macros


@pytest.mark.parametrize(
"lines, validity, resolve_func",
"lines, validity, expand_func",
[
(
["%ifarch %{power64}", "export ARCH=PPC64", "%endif"],
[True, True, True],
lambda kwd, exp: True,
lambda expr: (
"ppc64"
if expr == "%{_target_cpu}"
else "ppc64 ppc64p7 ppc64le" if expr == "%{power64}" else expr
),
),
(
["%ifarch %{power64}", "export ARCH=PPC64", "%endif"],
[True, False, True],
lambda kwd, exp: False,
lambda expr: (
"x86_64"
if expr == "%{_target_cpu}"
else "ppc64 ppc64p7 ppc64le" if expr == "%{power64}" else expr
),
),
(
[
Expand All @@ -33,7 +41,11 @@
"%endif",
],
[True, False, True, True, True, False, True],
lambda kwd, exp: "rhel" in exp,
lambda expr: (
"0"
if expr == "%{expr:0%{?fedora} > 38}"
else "1" if expr == "%{expr:0%{?rhel} > 8}" else expr
),
),
(
[
Expand Down Expand Up @@ -64,17 +76,19 @@
True,
True,
],
lambda kwd, exp: "fedora" in exp,
lambda expr: (
"0"
if expr.startswith("%{expr:%{with_")
else "1" if expr == "%{expr:0%{?fedora}}" else expr
),
),
],
)
def test_process_conditions(lines, validity, resolve_func):
def resolve_expression(kwd, exp, *_, **__):
return resolve_func(kwd, exp)
def test_process_conditions(lines, validity, expand_func):
def expand(expr):
return expand_func(expr)

flexmock(specfile.conditions).should_receive("resolve_expression").replace_with(
resolve_expression
)
flexmock(Macros).should_receive("expand").replace_with(expand)
processed_lines, processed_validity = zip(*process_conditions(lines))
assert list(processed_lines) == lines
assert list(processed_validity) == validity
Expand Down

0 comments on commit 1a40ae6

Please sign in to comment.