Skip to content

Commit ddfc68d

Browse files
committed
Implement release pattern matching
1 parent 72c68c8 commit ddfc68d

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/poetry/core/constraints/version/parser.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def parse_single_constraint(
7777
constraint: str, *, is_marker_constraint: bool = False
7878
) -> VersionConstraint:
7979
from poetry.core.constraints.version.patterns import BASIC_CONSTRAINT
80+
from poetry.core.constraints.version.patterns import BASIC_RELEASE_CONSTRAINT
8081
from poetry.core.constraints.version.patterns import CARET_CONSTRAINT
8182
from poetry.core.constraints.version.patterns import TILDE_CONSTRAINT
8283
from poetry.core.constraints.version.patterns import TILDE_PEP440_CONSTRAINT
@@ -185,6 +186,36 @@ def parse_single_constraint(
185186

186187
return version
187188

189+
# These below should be reserved for comparing non python packages such as OS
190+
# versions using `platform_release`
191+
m = BASIC_RELEASE_CONSTRAINT.match(constraint)
192+
if m:
193+
op = m.group("op")
194+
release_string = m.group("release")
195+
build = m.group("build")
196+
197+
try:
198+
version = Version(
199+
release=Version.parse(release_string).release,
200+
local=build,
201+
)
202+
except InvalidVersion as e:
203+
raise ParseConstraintError(
204+
f"Could not parse version constraint: {constraint}"
205+
) from e
206+
207+
if op == "<":
208+
return VersionRange(max=version)
209+
if op == "<=":
210+
return VersionRange(max=version, include_max=True)
211+
if op == ">":
212+
return VersionRange(min=version)
213+
if op == ">=":
214+
return VersionRange(min=version, include_min=True)
215+
if op == "!=":
216+
return VersionUnion(VersionRange(max=version), VersionRange(min=version))
217+
return version
218+
188219
raise ParseConstraintError(f"Could not parse version constraint: {constraint}")
189220

190221

src/poetry/core/constraints/version/patterns.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,16 @@
2626
rf"^(?P<op><>|!=|>=?|<=?|==?)?\s*(?P<version>{VERSION_PATTERN}|dev)(?P<wildcard>\.\*)?$",
2727
re.VERBOSE | re.IGNORECASE,
2828
)
29+
30+
RELEASE_PATTERN = r"""
31+
(?P<release>[0-9]+(?:\.[0-9]+)*)
32+
(?:(\+|-)(?P<build>
33+
[0-9a-zA-Z-]+
34+
(?:\.[0-9a-zA-Z-]+)*
35+
))?
36+
"""
37+
38+
BASIC_RELEASE_CONSTRAINT = re.compile(
39+
rf"^(?P<op><>|!=|>=?|<=?|==?)?\s*(?P<version>{RELEASE_PATTERN})$",
40+
re.VERBOSE | re.IGNORECASE,
41+
)

tests/version/test_markers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,34 @@ def test_multi_marker_removes_duplicates() -> None:
960960
{"platform_machine": "x86_64"},
961961
False,
962962
),
963+
('"tegra" in platform_release', {"platform_release": "5.10.120-tegra"}, True),
964+
('"tegra" in platform_release', {"platform_release": "5.10.120"}, False),
965+
(
966+
'"tegra" not in platform_release',
967+
{"platform_release": "5.10.120-tegra"},
968+
False,
969+
),
970+
('"tegra" not in platform_release', {"platform_release": "5.10.120"}, True),
971+
(
972+
"platform_machine == 'aarch64' and 'tegra' in platform_release",
973+
{"platform_release": "5.10.120-tegra", "platform_machine": "aarch64"},
974+
True,
975+
),
976+
(
977+
"platform_release != '4.9.253-tegra'",
978+
{"platform_release": "4.9.254-tegra"},
979+
True,
980+
),
981+
(
982+
"platform_release >= '6.6.0+rpt-rpi-v8'",
983+
{"platform_release": "6.6.20+rpt-rpi-v8"},
984+
True,
985+
),
986+
(
987+
"platform_release < '5.10.123-tegra' and platform_release >= '4.9.254-tegra'",
988+
{"platform_release": "4.9.254-tegra"},
989+
True,
990+
),
963991
# extras
964992
# single extra
965993
("extra == 'security'", {"extra": "quux"}, False),

0 commit comments

Comments
 (0)