Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 4, 2024
1 parent 35c3a0c commit e2da4e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
46 changes: 27 additions & 19 deletions grayskull/strategy/py_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,51 +247,59 @@ def add_flit_metadata(metadata: dict, toml_metadata: dict) -> dict:


def is_pep725_present(toml_metadata: dict):
return 'external' in toml_metadata
return "external" in toml_metadata


def get_pep725_mapping(purl: str):
'''This function maps a PURL to the name in the conda ecosystem. It is expected
that this will be provided on a per-ecosystem basis (such as by conda-forge)'''
"""This function maps a PURL to the name in the conda ecosystem. It is expected
that this will be provided on a per-ecosystem basis (such as by conda-forge)"""
# taken from

package_mapping = {
'virtual:compiler/c': '${{ compiler("c") }}',
'virtual:compiler/cpp': '${{ compiler("cxx") }}',
'virtual:compiler/fortran': '${{ compiler("fortran") }}',
'virtual:compiler/rust': '${{ compiler("rust") }}',
'virtual:interface/blas': '${{ blas }}',
"virtual:compiler/c": '${{ compiler("c") }}',
"virtual:compiler/cpp": '${{ compiler("cxx") }}',
"virtual:compiler/fortran": '${{ compiler("fortran") }}',
"virtual:compiler/rust": '${{ compiler("rust") }}',
"virtual:interface/blas": "${{ blas }}",
}
return package_mapping.get(purl) or f'# WARNING: no matching mapping was found for PURL "{purl}". The dependency has been omitted here.'
return (
package_mapping.get(purl)
or f'# WARNING: no matching mapping was found for PURL "{purl}". The dependency has been omitted here.'
)


def add_pep725_metadata(metadata: dict, toml_metadata: dict):
if not is_pep725_present(toml_metadata):
return metadata

externals = toml_metadata['external']
externals = toml_metadata["external"]
# each of these is a list of PURLs. For each one we find,
# we need to map it to the the conda ecosystem
requirements = metadata.get('requirements', {})
requirements = metadata.get("requirements", {})
section_map = (
('build', 'build-requires'),
('host', 'host-requires'),
('run', 'dependencies')
("build", "build-requires"),
("host", "host-requires"),
("run", "dependencies"),
)
for (conda_section, pep725_section) in section_map:
requirements[conda_section] = [get_pep725_mapping(purl) for purl in externals.get(pep725_section, [])]
for conda_section, pep725_section in section_map:
requirements[conda_section] = [
get_pep725_mapping(purl) for purl in externals.get(pep725_section, [])
]
# Conda doesn't really have a notion of optional dependencies. We just insert them all.
optional_features = toml_metadata.get(f'optional-{pep725_section}', {})
optional_features = toml_metadata.get(f"optional-{pep725_section}", {})
for feature_name, feature_deps in optional_features.items():
requirements[conda_section].append(f'# OPTIONAL dependencies from feature "{feature_name}"')
requirements[conda_section].append(
f'# OPTIONAL dependencies from feature "{feature_name}"'
)
requirements[conda_section].extend(feature_deps)
if not requirements[conda_section]:
del requirements[conda_section]

if requirements:
metadata['requirements'] = requirements
metadata["requirements"] = requirements
return metadata


def get_all_toml_info(path_toml: Union[Path, str]) -> dict:
with open(path_toml, "rb") as f:
toml_metadata = tomli.load(f)
Expand Down
32 changes: 16 additions & 16 deletions tests/test_py_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from grayskull.main import generate_recipes_from_list, init_parser
from grayskull.strategy.py_toml import (
InvalidVersion,
add_poetry_metadata,
add_flit_metadata,
add_pep725_metadata,
add_poetry_metadata,
encode_poetry_version,
get_all_toml_info,
get_caret_ceiling,
Expand All @@ -27,7 +27,6 @@ def test_add_flit_metadata():
assert result == {"build": {"entry_points": ["key = value"]}}



@pytest.mark.parametrize(
"version, major, minor, patch",
[
Expand Down Expand Up @@ -209,26 +208,27 @@ def test_poetry_entrypoints():
"test": {},
}


@pytest.mark.parametrize(
"conda_section, pep725_section",
[
('build', 'build-requires'),
('host', 'host-requires'),
('run', 'dependencies')
])
[("build", "build-requires"), ("host", "host-requires"), ("run", "dependencies")],
)
@pytest.mark.parametrize(
"purl, purl_translated", [
"purl, purl_translated",
[
("virtual:compiler/c", '${{ compiler("c") }}'),
("bob", '# WARNING: no matching mapping was found for PURL "bob". The dependency has been omitted here.')
])
(
"bob",
'# WARNING: no matching mapping was found for PURL "bob". The dependency has been omitted here.',
),
],
)
def test_pep725_section_lookup(conda_section, pep725_section, purl, purl_translated):
toml_metadata = {
toml_metadata = {
"external": {
pep725_section: [purl],
}
}
assert add_pep725_metadata({}, toml_metadata) == {
"requirements": {
conda_section: [purl_translated]
}
}
assert add_pep725_metadata({}, toml_metadata) == {
"requirements": {conda_section: [purl_translated]}
}

0 comments on commit e2da4e0

Please sign in to comment.