Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: omit also jinja functions #37

Merged
merged 13 commits into from
Aug 1, 2024
43 changes: 42 additions & 1 deletion src/rattler_build_conda_compat/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,28 @@ def jinja_env() -> jinja2.Environment:
"""
Create a `rattler-build` specific Jinja2 environment with modified syntax.
"""
return jinja2.Environment(

def stub_compatible_pin(*args, **kwargs) -> str: # noqa: ARG001, ANN003, ANN002
return f"compatible_pin {args[0]}"

def stub_subpackage_pin(*args, **kwargs) -> str: # noqa: ARG001, ANN003, ANN002
return f"subpackage_pin {args[0]}"

def version_to_build_string(some_string: str) -> str:
"""Converts some version by removing the . character and returning only the first two elements of the version)"""
# We first split the string by whitespace and take the first part
split = some_string.split()[0] if some_string.split() else some_string
# We then split the string by . and take the first two parts
parts = split.split(".")
major = parts[0] if len(parts) > 0 else ""
minor = parts[1] if len(parts) > 1 else ""
return f"{major}{minor}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine that it would be useful to add a test for this implementation too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added!


def split_filter(s: str, sep: str = " ") -> list[str]:
"""Filter that split a string by a separator"""
return s.split(sep)

env = jinja2.Environment(
variable_start_string="${{",
variable_end_string="}}",
trim_blocks=True,
Expand All @@ -36,6 +57,26 @@ def jinja_env() -> jinja2.Environment:
undefined=_MissingUndefined,
)

# inject rattler-build recipe functions in jinja environment
env.globals.update(
{
"compiler": lambda x: x + "_compiler_stub",
"stdlib": lambda x: x + "_stdlib_stub",
"pin_subpackage": stub_subpackage_pin,
"pin_compatible": stub_compatible_pin,
"cdt": lambda *args, **kwargs: "cdt_stub", # noqa: ARG005
}
)

# inject rattler-build recipe filters in jinja environment
env.filters.update(
{
"version_to_buildstring": version_to_build_string,
"split": split_filter,
}
)
return env
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to add some stub functions for env.get and env.get_default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the more reason tho have an actual recipe that contains some of these corner cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true , added mamba_recipe as a test one



def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment) -> dict[str, str]:
"""
Expand Down
11 changes: 7 additions & 4 deletions tests/__snapshots__/test_jinja.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# name: test_render_recipe_with_context
'''
build:
string: ${{ blas_variant }}${{ hash }}_foo-bla
string: ${{ blas_variant }}${{ hash }}_foo-1.2.3
context:
cuda_version: ${{ hash }}_cuda_12
name: foo
name_version: foo-bla
version: bla
name_version: foo-1.2.3
split_version: 1;2;3
version: 1.2.3
package:
func_result: c_compiler_stub
name: foo
version: bla
version: 1.2.3

'''
# ---
5 changes: 4 additions & 1 deletion tests/data/context.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
context:
name: "foo"
version: "bla"
version: "1.2.3"
name_version: ${{ name }}-${{ version }}
cuda_version: ${{ hash }}_cuda_${{ version | version_to_buildstring }}
split_version: ${{ version | split('.') | join(';') }}

package:
name: ${{ name }}
version: ${{ version }}
func_result: ${{ compiler('c') }}

build:
string: ${{ blas_variant }}${{ hash }}_${{ name_version }}