Skip to content

Commit

Permalink
Merge pull request #2135 from conda-forge/undef-jinja2
Browse files Browse the repository at this point in the history
fix: ensure we use CBC to parse recipe names for conda-forge
  • Loading branch information
beckermr authored Nov 16, 2024
2 parents b591810 + 2b80f58 commit a132847
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 5 deletions.
13 changes: 11 additions & 2 deletions conda_smithy/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from github.Organization import Organization
from github.Team import Team

from conda_smithy.configure_feedstock import _load_forge_config
from conda_smithy.configure_feedstock import (
_load_forge_config,
get_cached_cfp_file_path,
)
from conda_smithy.utils import (
_get_metadata_from_feedstock_dir,
get_feedstock_name_from_meta,
Expand Down Expand Up @@ -150,7 +153,13 @@ def create_github_repo(args):
# Load the conda-forge config and read metadata from the feedstock recipe
forge_config = _load_forge_config(args.feedstock_directory, None)
metadata = _get_metadata_from_feedstock_dir(
args.feedstock_directory, forge_config
args.feedstock_directory,
forge_config,
conda_forge_pinning_file=(
get_cached_cfp_file_path(".")
if args.user is None and args.organization == "conda-forge"
else None
),
)

feedstock_name = get_feedstock_name_from_meta(metadata)
Expand Down
13 changes: 11 additions & 2 deletions conda_smithy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jinja2.sandbox
import ruamel.yaml
from conda_build.api import render as conda_build_render
from conda_build.config import Config
from conda_build.render import MetaData
from rattler_build_conda_compat.render import MetaData as RattlerBuildMetaData

Expand All @@ -23,7 +24,9 @@


def _get_metadata_from_feedstock_dir(
feedstock_directory: Union[str, os.PathLike], forge_config: Dict[str, Any]
feedstock_directory: Union[str, os.PathLike],
forge_config: Dict[str, Any],
conda_forge_pinning_file: Union[str, os.PathLike, None] = None,
) -> Union[MetaData, RattlerBuildMetaData]:
"""
Return either the conda-build metadata or rattler-build metadata from the feedstock directory
Expand All @@ -35,9 +38,15 @@ def _get_metadata_from_feedstock_dir(
feedstock_directory,
)
else:
if conda_forge_pinning_file:
config = Config(
variant_config_files=[conda_forge_pinning_file],
)
else:
config = None
meta = conda_build_render(
feedstock_directory,
permit_undefined_jinja=True,
config=config,
finalize=False,
bypass_env_check=True,
trim_skip=False,
Expand Down
23 changes: 23 additions & 0 deletions news/noarch_create_feedstocks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed a bug where creating feedstocks with `noarch: python` would not work correctly. (#2135)

**Security:**

* <news item>
43 changes: 42 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def recipe_dirname():

@pytest.fixture(scope="function", params=["conda-build", "rattler-build"])
def config_yaml(testing_workdir, recipe_dirname, request):
config = {"python": ["2.7", "3.5"], "r_base": ["3.3.2", "3.4.2"]}
config = {
"python": ["2.7", "3.5"],
"r_base": ["3.3.2", "3.4.2"],
"python_min": ["2.7"],
}
os.makedirs(os.path.join(testing_workdir, recipe_dirname))
with open(os.path.join(testing_workdir, "config.yaml"), "w") as f:
f.write("docker:\n")
Expand Down Expand Up @@ -151,6 +155,43 @@ def noarch_recipe(config_yaml: ConfigYAML, recipe_dirname):
)


@pytest.fixture(scope="function")
def noarch_recipe_with_python_min(config_yaml: ConfigYAML, recipe_dirname):
if config_yaml.type == "rattler-build":
jinjatxt = "${{ python_min }}"
else:
jinjatxt = "{{ python_min }}"
with open(
os.path.join(
config_yaml.workdir, recipe_dirname, config_yaml.recipe_name
),
"w",
) as fh:
fh.write(
f"""\
package:
name: python-noarch-test
version: 1.0.0
build:
noarch: python
requirements:
host:
- python {jinjatxt}
run:
- python >={jinjatxt}
"""
)
return RecipeConfigPair(
str(config_yaml.workdir),
_load_forge_config(
config_yaml.workdir,
exclusive_config_file=os.path.join(
config_yaml.workdir, recipe_dirname, "default_config.yaml"
),
),
)


@pytest.fixture(scope="function")
def r_recipe(config_yaml: ConfigYAML):
with open(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ def test_get_metadata_from_feedstock_dir(noarch_recipe):
assert isinstance(metadata, expected_metadata_type)


def test_get_metadata_from_feedstock_dir_jinja2(noarch_recipe_with_python_min):
feedstock_dir = noarch_recipe_with_python_min[0]

build_tool = noarch_recipe_with_python_min[1]["conda_build_tool"]
metadata = _get_metadata_from_feedstock_dir(
feedstock_dir,
noarch_recipe_with_python_min[1],
conda_forge_pinning_file=noarch_recipe_with_python_min[1][
"exclusive_config_file"
],
)

expected_metadata_type = (
RatlerBuildMetadata if build_tool == RATTLER_BUILD else MetaData
)

if build_tool == RATTLER_BUILD:
assert metadata.meta["requirements"]["host"] == [
"python ${{ python_min }}"
]
else:
assert metadata.meta["requirements"]["host"] == ["python 2.7"]

assert isinstance(metadata, expected_metadata_type)


def test_get_feedstock_name_from_metadata(noarch_recipe):
feedstock_dir = noarch_recipe[0]
metadata = _get_metadata_from_feedstock_dir(
Expand Down

0 comments on commit a132847

Please sign in to comment.