Skip to content

Commit

Permalink
fix: when extracting name of rendered recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Aug 2, 2024
1 parent 32a4ce3 commit f6c8a67
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/rattler_build_conda_compat/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def name(self) -> str:
- ValueError: If the name is not lowercase or missing.
"""
name = _get_recipe_metadata(self.meta, "name")
name = _get_recipe_metadata(self.meta, "name", rendered=self._rendered)

if not name:
raise ValueError(f"Error: package/name missing in: {self.meta_path!r}")
Expand All @@ -105,7 +105,7 @@ def version(self) -> str:
- CondaBuildUserError: If the `version` contains bad characters.
- ValueError: If the version starts with a period or version is missing.
"""
version = _get_recipe_metadata(self.meta, "version")
version = _get_recipe_metadata(self.meta, "version", rendered=self._rendered)

if not version:
raise ValueError(f"Error: package/version missing in: {self.meta_path!r}")
Expand Down Expand Up @@ -255,9 +255,9 @@ def render_recipe(


def render(
recipe_path,
config=None,
variants=None,
recipe_path: os.PathLike,
config: Optional[Config] = None,
variants: Optional[Dict[str, Any] | None] = None,
**kwargs,
):
"""Given path to a recipe, return the MetaData object(s) representing that recipe, with jinja2
Expand Down
8 changes: 6 additions & 2 deletions src/rattler_build_conda_compat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ def has_recipe(recipe_dir: Path) -> bool:
_Metadata = Literal["name", "version"]


def _get_recipe_metadata(meta: dict[str, Any], field: _Metadata) -> str:
def _get_recipe_metadata(meta: dict[str, Any], field: _Metadata, rendered: bool = False) -> str:
"""
Get recipe metadata ( name or version ).
Get recipe metadata ( name or version )
It will extract from recipe or package section, depending on the presence of multiple outputs.
If recipe was rendered by rattler-build, they are always present under recipe.package field.
"""
if rendered:
return meta.get("recipe", {}).get("package", {}).get(field, "")

if "outputs" in meta:
return meta.get("recipe", {}).get(field, "")
else:
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def rich_recipe() -> Path:
return Path("tests/data/rich_recipe.yaml")


@pytest.fixture()
def multiple_outputs() -> Path:
return Path("tests/data/multiple_outputs.yaml")


@pytest.fixture()
def feedstock_dir_with_recipe(tmpdir: Path) -> Path:
feedstock_dir = tmpdir / "feedstock"
Expand Down
2 changes: 1 addition & 1 deletion tests/data/mamba_recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ outputs:
run:
- libsolv >=0.7.23
run_exports:
- ${{ pin_subpackage('libmamba', max_pin='x.x') }}
- ${{ pin_subpackage('libmamba', upper_bound='x.x') }}
ignore_run_exports:
by_name:
- spdlog
Expand Down
121 changes: 121 additions & 0 deletions tests/data/multiple_outputs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/prefix-dev/recipe-format/main/schema.json

context:
name: mamba
libmamba_version: "1.5.8"
libmambapy_version: "1.5.8"
mamba_version: "1.5.8"
release: "2024.03.25"
build_number: 2

recipe:
name: mamba-split
version: ${{ mamba_version }}

source:
url: https://github.com/mamba-org/mamba/archive/refs/tags/${{ release }}.tar.gz
sha256: 6ddaf4b0758eb7ca1250f427bc40c2c3ede43257a60bac54e4320a4de66759a6

build:
number: ${{ build_number }}

outputs:
- package:
name: libmamba
version: ${{ libmamba_version }}
build:
script:
- ${{ "build_mamba.sh" if unix }}
- ${{ "build_mamba.bat" if win }}
requirements:
build:
- ${{ compiler('cxx') }}
- cmake
- ninja
- ${{ "python" if win }}
host:
- libsolv >=0.7.23
- libcurl >=8.4.0
- fmt
- ${{ "winreg" if win }}
run:
- libsolv >=0.7.23
run_exports:
- ${{ pin_subpackage('libmamba', upper_bound='x.x') }}
ignore_run_exports:
by_name:
- spdlog
- python
tests:
- script:
- if: unix
then:
- test -d ${PREFIX}/include/mamba # [unix]
else:
- if not exist %LIBRARY_PREFIX%\include\mamba\version.hpp (exit 1) # [win]

- package:
name: libmambapy
version: ${{ libmambapy_version }}
build:
script:
- ${{ "build_mamba.sh" if unix }}
- ${{ "build_mamba.bat" if win }}
# string: py_sup${{ python | version_to_buildstring }}h${{ hash }}_${{ build_number }}
requirements:
build:
- ${{ compiler('cxx') }}
- cmake
- ninja
- if: build_platform != target_platform
then:
- python
- cross-python_${{ target_platform }}
- pybind11
- pybind11-abi
host:
- python
- nlohmann_json
- ${{ pin_subpackage('libmamba', exact=True) }}
run:
- python
- ${{ pin_subpackage('libmamba', exact=True) }}
run_exports:
- ${{ pin_subpackage('libmambapy', upper_bound='x.x') }}
ignore_run_exports:
by_name:
- spdlog

- package:
name: mamba
version: ${{ mamba_version }}
build:
script:
- ${{ "build_mamba.sh" if unix }}
- ${{ "build_mamba.bat" if win }}
- ${{ "test_mamba.bat" if target_platform == win }}
string: py${{ python | version_to_buildstring }}h${{ hash }}_${{ build_number }}
python:
entry_points:
- mamba = mamba.mamba:main
requirements:
build:
- if: build_platform != target_platform
then:
- python
- cross-python_${{ target_platform }}
run:
- python
- conda >=23.9,<24
- ${{ pin_subpackage('libmambapy', exact=True) }}


about:
homepage: https://github.com/mamba-org/mamba
license: BSD-3-Clause
license_file: LICENSE
license_family: BSD
summary: A fast drop-in alternative to conda, using libsolv for dependency resolution
description: Mamba, the Fast Cross-Platform Package Manager

repository: https://github.com/mamba-org/mamba
24 changes: 24 additions & 0 deletions tests/test_rattler_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,27 @@ def test_metadata_for_multiple_output(feedstock_dir_with_recipe: Path, mamba_rec

assert rattler_metadata.name() == "mamba-split"
assert rattler_metadata.version() == "1.5.8"


def test_metadata_when_rendering_single_output(
feedstock_dir_with_recipe: Path, rich_recipe: Path
) -> None:
recipe_path = feedstock_dir_with_recipe / "recipe" / "recipe.yaml"
(recipe_path).write_text(rich_recipe.read_text(), encoding="utf8")

rendered = render(str(recipe_path), platform="linux", arch="64")

assert rendered[0][0].name() == "rich"
assert rendered[0][0].version() == "13.4.2"


def test_metadata_when_rendering_multiple_output(
feedstock_dir_with_recipe: Path, multiple_outputs: Path
) -> None:
recipe_path = feedstock_dir_with_recipe / "recipe" / "recipe.yaml"
(recipe_path).write_text(multiple_outputs.read_text(), encoding="utf8")

rendered = render(str(recipe_path), platform="linux", arch="64")

assert rendered[0][0].name() == "libmamba"
assert rendered[0][0].version() == "1.5.8"

0 comments on commit f6c8a67

Please sign in to comment.