Skip to content

Commit

Permalink
Consolidate pypi_name_to_conda_name in lookup module
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Sep 15, 2024
1 parent 90fe793 commit a828961
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
31 changes: 29 additions & 2 deletions conda_lock/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from filelock import FileLock, Timeout
from packaging.utils import NormalizedName, canonicalize_name
from packaging.utils import canonicalize_name as canonicalize_pypi_name
from platformdirs import user_cache_path
from typing_extensions import TypedDict

Expand All @@ -28,7 +29,7 @@ class MappingEntry(TypedDict):


@lru_cache(maxsize=None)
def get_pypi_lookup(mapping_url: str) -> Dict[NormalizedName, MappingEntry]:
def _get_pypi_lookup(mapping_url: str) -> Dict[NormalizedName, MappingEntry]:
url = mapping_url
if url.startswith("http://") or url.startswith("https://"):
content = cached_download_file(url)
Expand All @@ -52,13 +53,39 @@ def get_pypi_lookup(mapping_url: str) -> Dict[NormalizedName, MappingEntry]:
return lookup


def pypi_name_to_conda_name(name: str, mapping_url: str) -> str:
"""Convert a PyPI package name to a conda package name.
>>> from conda_lock.lookup import DEFAULT_MAPPING_URL
>>> pypi_name_to_conda_name("build", mapping_url=DEFAULT_MAPPING_URL)
'python-build'
>>> pypi_name_to_conda_name("zpfqzvrj", mapping_url=DEFAULT_MAPPING_URL)
'zpfqzvrj'
"""
cname = canonicalize_pypi_name(name)
if cname in _get_pypi_lookup(mapping_url):
lookup = _get_pypi_lookup(mapping_url)[cname]
res = lookup.get("conda_name") or lookup.get("conda_forge")
if res is not None:
return res
else:
logging.warning(
f"Could not find conda name for {cname}. Assuming identity."
)
return cname
else:
return cname


@lru_cache(maxsize=None)
def _get_conda_lookup(mapping_url: str) -> Dict[str, MappingEntry]:
"""
Reverse grayskull name mapping to map conda names onto PyPI
"""
return {
record["conda_name"]: record for record in get_pypi_lookup(mapping_url).values()
record["conda_name"]: record
for record in _get_pypi_lookup(mapping_url).values()
}


Expand Down
27 changes: 1 addition & 26 deletions conda_lock/src_parser/pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from conda_lock.common import get_in
from conda_lock.interfaces.vendored_grayskull import encode_poetry_version
from conda_lock.lookup import get_pypi_lookup as get_lookup
from conda_lock.lookup import pypi_name_to_conda_name
from conda_lock.models.lock_spec import (
Dependency,
LockSpecification,
Expand Down Expand Up @@ -70,31 +70,6 @@
)


def pypi_name_to_conda_name(name: str, mapping_url: str) -> str:
"""Convert a PyPI package name to a conda package name.
>>> from conda_lock.lookup import DEFAULT_MAPPING_URL
>>> pypi_name_to_conda_name("build", mapping_url=DEFAULT_MAPPING_URL)
'python-build'
>>> pypi_name_to_conda_name("zpfqzvrj", mapping_url=DEFAULT_MAPPING_URL)
'zpfqzvrj'
"""
cname = canonicalize_pypi_name(name)
if cname in get_lookup(mapping_url):
lookup = get_lookup(mapping_url)[cname]
res = lookup.get("conda_name") or lookup.get("conda_forge")
if res is not None:
return res
else:
logging.warning(
f"Could not find conda name for {cname}. Assuming identity."
)
return cname
else:
return cname


def poetry_version_to_conda_version(version_string: Optional[str]) -> Optional[str]:
"""Convert a Poetry-style version string to a Conda-compatible version string.
Expand Down

0 comments on commit a828961

Please sign in to comment.