Skip to content

Commit

Permalink
Implement release pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
npapapietro committed Apr 29, 2024
1 parent 72c68c8 commit ddfc68d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/poetry/core/constraints/version/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def parse_single_constraint(
constraint: str, *, is_marker_constraint: bool = False
) -> VersionConstraint:
from poetry.core.constraints.version.patterns import BASIC_CONSTRAINT
from poetry.core.constraints.version.patterns import BASIC_RELEASE_CONSTRAINT
from poetry.core.constraints.version.patterns import CARET_CONSTRAINT
from poetry.core.constraints.version.patterns import TILDE_CONSTRAINT
from poetry.core.constraints.version.patterns import TILDE_PEP440_CONSTRAINT
Expand Down Expand Up @@ -185,6 +186,36 @@ def parse_single_constraint(

return version

# These below should be reserved for comparing non python packages such as OS
# versions using `platform_release`
m = BASIC_RELEASE_CONSTRAINT.match(constraint)
if m:
op = m.group("op")
release_string = m.group("release")
build = m.group("build")

try:
version = Version(
release=Version.parse(release_string).release,
local=build,
)
except InvalidVersion as e:
raise ParseConstraintError(
f"Could not parse version constraint: {constraint}"
) from e

if op == "<":
return VersionRange(max=version)
if op == "<=":
return VersionRange(max=version, include_max=True)
if op == ">":
return VersionRange(min=version)
if op == ">=":
return VersionRange(min=version, include_min=True)
if op == "!=":
return VersionUnion(VersionRange(max=version), VersionRange(min=version))
return version

raise ParseConstraintError(f"Could not parse version constraint: {constraint}")


Expand Down
13 changes: 13 additions & 0 deletions src/poetry/core/constraints/version/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@
rf"^(?P<op><>|!=|>=?|<=?|==?)?\s*(?P<version>{VERSION_PATTERN}|dev)(?P<wildcard>\.\*)?$",
re.VERBOSE | re.IGNORECASE,
)

RELEASE_PATTERN = r"""
(?P<release>[0-9]+(?:\.[0-9]+)*)
(?:(\+|-)(?P<build>
[0-9a-zA-Z-]+
(?:\.[0-9a-zA-Z-]+)*
))?
"""

BASIC_RELEASE_CONSTRAINT = re.compile(
rf"^(?P<op><>|!=|>=?|<=?|==?)?\s*(?P<version>{RELEASE_PATTERN})$",
re.VERBOSE | re.IGNORECASE,
)
28 changes: 28 additions & 0 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,34 @@ def test_multi_marker_removes_duplicates() -> None:
{"platform_machine": "x86_64"},
False,
),
('"tegra" in platform_release', {"platform_release": "5.10.120-tegra"}, True),
('"tegra" in platform_release', {"platform_release": "5.10.120"}, False),
(
'"tegra" not in platform_release',
{"platform_release": "5.10.120-tegra"},
False,
),
('"tegra" not in platform_release', {"platform_release": "5.10.120"}, True),
(
"platform_machine == 'aarch64' and 'tegra' in platform_release",
{"platform_release": "5.10.120-tegra", "platform_machine": "aarch64"},
True,
),
(
"platform_release != '4.9.253-tegra'",
{"platform_release": "4.9.254-tegra"},
True,
),
(
"platform_release >= '6.6.0+rpt-rpi-v8'",
{"platform_release": "6.6.20+rpt-rpi-v8"},
True,
),
(
"platform_release < '5.10.123-tegra' and platform_release >= '4.9.254-tegra'",
{"platform_release": "4.9.254-tegra"},
True,
),
# extras
# single extra
("extra == 'security'", {"extra": "quux"}, False),
Expand Down

0 comments on commit ddfc68d

Please sign in to comment.