Skip to content

Commit

Permalink
Merge pull request #3222 from regro/fix-parser-set-end
Browse files Browse the repository at this point in the history
fix: make sure parser works for jinja2 set statements with newline ad…
  • Loading branch information
beckermr authored Nov 27, 2024
2 parents e555177 + 821ee2c commit 3e734e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
30 changes: 30 additions & 0 deletions conda_forge_tick/recipe_parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
# this one matches bad yaml syntax with a selector on a multiline string start
BAD_MULTILINE_STRING_WITH_SELECTOR = re.compile(r"[^|#]*\|\s+#")

# this one finds set statements that have adjustments to newlines via {%- or -%}
# they cause problems with the parser and so we remove those
BAD_JINJA2_SET_STATEMENT = re.compile(
r"^\s*({%-\s+set\s+.*?=.*?-?%}|{%-?\s+set\s+.*?=.*?-%})\s*(#.*)?$"
)


def _get_yaml_parser(typ="jinja2"):
"""yaml parser that is jinja2 aware"""
Expand Down Expand Up @@ -483,6 +489,27 @@ def _remove_quoted_jinja2_vars(lines):
return new_lines


def _remove_bad_jinja2_set_statements(lines):
"""Remove any jinja2 set statements that have bad newline adjustments
by removing the adjustments.
This function turns things like
{%- set var = val -%}
into
{% set var = val %}
"""
new_lines = []
for line in lines:
if BAD_JINJA2_SET_STATEMENT.match(line):
new_lines.append(line.replace("{%-", "{%").replace("-%}", "%}"))
else:
new_lines.append(line)
return new_lines


class CondaMetaYAML:
"""Crude parsing of conda recipes.
Expand Down Expand Up @@ -537,6 +564,9 @@ def __init__(self, meta_yaml: str):
# pre-munge odd syntax that we do not want
lines = list(io.StringIO(meta_yaml).readlines())

# remove bad jinja2 set statements
lines = _remove_bad_jinja2_set_statements(lines)

# remove multiline jinja2 statements
lines = _munge_multiline_jinja2(lines)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_recipe_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,9 @@ def test_recipe_parses_fftw_raises():


def test_recipe_parses_cupy():
recipe = r"""{% set name = "cupy" %}
{% set version = "10.1.0" %}
{% set sha256 = "ad28e7311b2023391f2278b7649828decdd9d9599848e18845eb4ab1b2d01936" %}
recipe = r"""{% set name = "cupy" -%}
{%- set version = "10.1.0" %}
{%- set sha256 = "ad28e7311b2023391f2278b7649828decdd9d9599848e18845eb4ab1b2d01936" -%}
{% if cuda_compiler_version in (None, "None", True, False) %}
{% set cuda_major = 0 %}
Expand Down

0 comments on commit 3e734e5

Please sign in to comment.