Skip to content

Commit

Permalink
Use Grayskull to parse versions
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Sep 6, 2024
1 parent 37f46a2 commit 5ce0113
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions conda_lock/src_parser/pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Optional,
Sequence,
Tuple,
Union,
)
from urllib.parse import urldefrag

Expand All @@ -29,6 +28,9 @@
from packaging.utils import canonicalize_name as canonicalize_pypi_name
from typing_extensions import Literal

from conda_lock._vendor.grayskull.strategy.parse_poetry_version import (
encode_poetry_version,
)
from conda_lock.common import get_in
from conda_lock.lookup import get_forward_lookup as get_lookup
from conda_lock.models.lock_spec import (
Expand Down Expand Up @@ -69,10 +71,6 @@
)


def join_version_components(pieces: Sequence[Union[str, int]]) -> str:
return ".".join(str(p) for p in pieces)


def normalize_pypi_name(name: str) -> str:
cname = canonicalize_pypi_name(name)
if cname in get_lookup():
Expand All @@ -90,32 +88,25 @@ def normalize_pypi_name(name: str) -> str:


def poetry_version_to_conda_version(version_string: Optional[str]) -> Optional[str]:
"""Convert a Poetry-style version string to a Conda-compatible version string.
>>> poetry_version_to_conda_version("1.2.3.4")
'1.2.3.4'
>>> poetry_version_to_conda_version("1.2.3, 2.3, <=3.4")
'1.2.3,2.3,<=3.4'
>>> poetry_version_to_conda_version("~0.14.2")
'>=0.14.2,<0.15.0'
>>> poetry_version_to_conda_version(None)
>>> poetry_version_to_conda_version("~1.2.3, ^2.3")
'>=1.2.3,<1.3.0,>=2.3.0,<3.0.0'
"""
if version_string is None:
return None
components = [c.replace(" ", "").strip() for c in version_string.split(",")]
output_components = []

for c in components:
if len(c) == 0:
continue
version_pieces = c.lstrip("<>=^~!").split(".")
if c[0] == "^":
upper_version = [int(version_pieces[0]) + 1]
for i in range(1, len(version_pieces)):
upper_version.append(0)

output_components.append(f">={join_version_components(version_pieces)}")
output_components.append(f"<{join_version_components(upper_version)}")
elif c[0] == "~":
upper_version = [int(version_pieces[0]), int(version_pieces[1]) + 1]
for i in range(2, len(version_pieces)):
upper_version.append(0)

output_components.append(f">={join_version_components(version_pieces)}")
output_components.append(f"<{join_version_components(upper_version)}")
else:
output_components.append(c.replace("===", "=").replace("==", "="))
return ",".join(output_components)
return encode_poetry_version(version_string)


def handle_mapping(
Expand Down

0 comments on commit 5ce0113

Please sign in to comment.