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: read feedstock name from rattler-build feedstock #2003

Merged
merged 17 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions conda_smithy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import Optional, Union

import conda # noqa
import conda_build.api
from conda_build.metadata import MetaData
from rattler_build_conda_compat.render import MetaData as RattlerMetaData
from rattler_build_conda_compat.utils import has_recipe as has_rattler_recipe
Expand All @@ -18,9 +17,11 @@
import conda_smithy.cirun_utils
from conda_smithy import __version__, configure_feedstock, feedstock_io
from conda_smithy import lint_recipe as linter
from conda_smithy.configure_feedstock import _load_forge_config
from conda_smithy.utils import (
CONDA_BUILD,
RATTLER_BUILD,
_get_metadata_from_feedstock_dir,
get_feedstock_name_from_meta,
merge_dict,
)
Expand Down Expand Up @@ -328,14 +329,15 @@ def __call__(self, args):
from conda_smithy import ci_register

owner = args.user or args.organization
meta = conda_build.api.render(
args.feedstock_directory,
permit_undefined_jinja=True,
finalize=False,
bypass_env_check=True,
trim_skip=False,
)[0][0]
feedstock_name = get_feedstock_name_from_meta(meta)

# 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
)

feedstock_name = get_feedstock_name_from_meta(metadata)

repo = f"{feedstock_name}-feedstock"

if args.feedstock_config is None:
Expand Down
26 changes: 15 additions & 11 deletions conda_smithy/github.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import os
from random import choice

import conda_build.api
import github
from git import Repo
from github import Github
from github.GithubException import GithubException
from github.Organization import Organization
from github.Team import Team

from conda_smithy.utils import get_feedstock_name_from_meta
from conda_smithy.configure_feedstock import _load_forge_config
from conda_smithy.utils import (
_get_metadata_from_feedstock_dir,
get_feedstock_name_from_meta,
)


def gh_token():
Expand Down Expand Up @@ -101,15 +104,14 @@ def get_cached_team(org, team_name, description=""):

def create_github_repo(args):
token = gh_token()
meta = conda_build.api.render(
args.feedstock_directory,
permit_undefined_jinja=True,
finalize=False,
bypass_env_check=True,
trim_skip=False,
)[0][0]

feedstock_name = get_feedstock_name_from_meta(meta)
# 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
)

feedstock_name = get_feedstock_name_from_meta(metadata)

gh = Github(token)
user_or_org = None
Expand Down Expand Up @@ -159,7 +161,9 @@ def create_github_repo(args):

if args.add_teams:
if isinstance(user_or_org, Organization):
configure_github_team(meta, gh_repo, user_or_org, feedstock_name)
configure_github_team(
metadata, gh_repo, user_or_org, feedstock_name
)


def accept_all_repository_invitations(gh):
Expand Down
34 changes: 32 additions & 2 deletions conda_smithy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,47 @@
from collections import defaultdict
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Dict, Union

import jinja2
import jinja2.sandbox
import ruamel.yaml
from conda_build.api import render as conda_build_render
from conda_build.render import MetaData
from rattler_build_conda_compat.render import MetaData as RattlerBuildMetaData

RATTLER_BUILD = "rattler-build"
CONDA_BUILD = "conda-build"


def get_feedstock_name_from_meta(meta):
"""Resolve the feedtstock name from the parsed meta.yaml."""
def _get_metadata_from_feedstock_dir(
feedstock_directory: Union[str, os.PathLike], forge_config: Dict[str, Any]
) -> Union[MetaData, RattlerBuildMetaData]:
"""
Return either the conda-build metadata or rattler-build metadata from the feedstock directory
based on conda_build_tool value from forge_config.
Raises OsError if no meta.yaml or recipe.yaml is found in feedstock_directory.
"""
if forge_config and forge_config.get("conda_build_tool") == RATTLER_BUILD:
Copy link
Member

Choose a reason for hiding this comment

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

Will this work with a fresh feedstock? Because I am not sure if we will have the right forge_config.

Copy link
Member

Choose a reason for hiding this comment

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

@nichmor did you see this comment?

meta = RattlerBuildMetaData(
beckermr marked this conversation as resolved.
Show resolved Hide resolved
feedstock_directory,
)
else:
meta = conda_build_render(
feedstock_directory,
permit_undefined_jinja=True,
finalize=False,
bypass_env_check=True,
trim_skip=False,
)[0][0]

return meta


def get_feedstock_name_from_meta(
meta: Union[MetaData, RattlerBuildMetaData]
) -> str:
"""Get the feedstock name from a parsed meta.yaml or recipe.yaml."""
if "feedstock-name" in meta.meta["extra"]:
return meta.meta["extra"]["feedstock-name"]
elif "parent_recipe" in meta.meta["extra"]:
Expand Down
94 changes: 94 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,97 @@ def jinja_env():
return SandboxedEnvironment(
extensions=["jinja2.ext.do"], loader=FileSystemLoader([tmplt_dir])
)


@pytest.fixture(scope="function")
def rattler_noarch_recipe_with_context(testing_workdir: Path, recipe_dirname):
with open(os.path.join(testing_workdir, "conda-forge.yml"), "w") as f:
config = {
"recipe_dir": recipe_dirname,
}
config["conda_build_tool"] = "rattler-build"
yaml.dump(config, f, default_flow_style=False)

os.mkdir(os.path.join(testing_workdir, recipe_dirname))
with open(
os.path.join(testing_workdir, recipe_dirname, "recipe.yaml"),
"w",
) as fh:
fh.write(
"""
context:
name: python-noarch-test-from-context
version: 9.0.0
package:
name: ${{ name }}
version: ${{ version }}
build:
noarch: python
requirements:
build:
- python
run:
- python
"""
)

return RecipeConfigPair(
testing_workdir,
_load_forge_config(testing_workdir, exclusive_config_file=None),
)


@pytest.fixture(scope="function")
def rattler_recipe_with_multiple_outputs(
testing_workdir: Path, recipe_dirname
):
with open(os.path.join(testing_workdir, "conda-forge.yml"), "w") as f:
config = {
"recipe_dir": recipe_dirname,
}
config["conda_build_tool"] = "rattler-build"
yaml.dump(config, f, default_flow_style=False)

os.mkdir(os.path.join(testing_workdir, recipe_dirname))

with open(
os.path.join(testing_workdir, recipe_dirname, "recipe.yaml"),
"w",
) as fh:
fh.write(
"""
context:
name: mamba
mamba_version: "1.5.8"
libmamba_version: "1.5.9"
libmambapy_version: "1.5.9"

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: 1

outputs:
- package:
name: libmamba
version: ${{ libmamba_version }}

- package:
name: libmambapy
version: ${{ libmambapy_version }}

- package:
name: mamba
version: ${{ mamba_version }}
"""
)
return RecipeConfigPair(
testing_workdir,
_load_forge_config(testing_workdir, exclusive_config_file=None),
)
60 changes: 60 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from conda_build.metadata import MetaData
from rattler_build_conda_compat.render import MetaData as RatlerBuildMetadata

from conda_smithy.utils import (
RATTLER_BUILD,
_get_metadata_from_feedstock_dir,
get_feedstock_name_from_meta,
)


def test_get_metadata_from_feedstock_dir(noarch_recipe):
feedstock_dir = noarch_recipe[0]

build_tool = noarch_recipe[1]["conda_build_tool"]
metadata = _get_metadata_from_feedstock_dir(
feedstock_dir, noarch_recipe[1]
)

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

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(
feedstock_dir, noarch_recipe[1]
)

feedstock_name = get_feedstock_name_from_meta(metadata)

assert feedstock_name == "python-noarch-test"


def test_get_feedstock_name_from_rattler_metadata(
rattler_noarch_recipe_with_context,
):
feedstock_dir = rattler_noarch_recipe_with_context[0]
metadata = _get_metadata_from_feedstock_dir(
feedstock_dir, rattler_noarch_recipe_with_context[1]
)

feedstock_name = get_feedstock_name_from_meta(metadata)

assert feedstock_name == "python-noarch-test-from-context"


def test_get_feedstock_name_from_rattler_metadata_multiple_outputs(
rattler_recipe_with_multiple_outputs,
):
feedstock_dir = rattler_recipe_with_multiple_outputs[0]
metadata = _get_metadata_from_feedstock_dir(
feedstock_dir, rattler_recipe_with_multiple_outputs[1]
)

feedstock_name = get_feedstock_name_from_meta(metadata)

assert feedstock_name == "mamba-split"
Loading