Skip to content

Commit 0cba8d5

Browse files
committed
add constants
1 parent 27bc849 commit 0cba8d5

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

python/private/pypi/select_whl.bzl

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,43 @@ load("//python/private:version.bzl", "version")
44
load(":parse_whl_name.bzl", "parse_whl_name")
55
load(":python_tag.bzl", "PY_TAG_GENERIC", "python_tag")
66

7-
def _priority_by_platform(*, tag, values):
8-
if tag == "any" and tag in values:
7+
_ANDROID = "android"
8+
_ANY = "any"
9+
_IOS = "ios"
10+
_MANYLINUX = "manylinux"
11+
_MUSLLINUX = "musllinux"
12+
13+
def _value_priority(*, tag, values):
14+
keys = []
15+
for priority, wp in enumerate(values):
16+
if tag == wp:
17+
keys.append(priority)
18+
19+
return max(keys) if keys else None
20+
21+
def _platform_tag_priority(*, tag, values):
22+
if tag == _ANY and tag in values:
923
m = values.index(tag)
1024
return (m, (0, 0)) if m >= 0 else None
1125

12-
# TODO: Implement https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
13-
14-
# TODO @aignas 2025-07-18: add more tests and optimize
26+
# Implements matching platform tag
27+
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
1528

1629
if not (
17-
tag.startswith("manylinux") or
18-
tag.startswith("musllinux") or
19-
tag.startswith("android") or
20-
tag.startswith("ios")
30+
tag.startswith(_MANYLINUX) or
31+
tag.startswith(_MUSLLINUX) or
32+
tag.startswith(_ANDROID) or
33+
tag.startswith(_IOS)
2134
):
22-
res = _priority_by_abi(tag = tag, values = values)
35+
res = _value_priority(tag = tag, values = values)
2336
if res == None:
2437
return res
2538

2639
return (res, (0, 0))
2740

2841
plat, _, tail = tag.partition("_")
2942
major, _, tail = tail.partition("_")
30-
if not plat.startswith("android"):
43+
if not plat.startswith(_ANDROID):
3144
minor, _, arch = tail.partition("_")
3245
else:
3346
minor = "0"
@@ -45,10 +58,10 @@ def _priority_by_platform(*, tag, values):
4558

4659
want_major, _, tail = tail.partition("_")
4760
if want_major == "*":
48-
want_major = "9"
49-
want_minor = "9"
61+
want_major = ""
62+
want_minor = ""
5063
want_arch = tail
51-
elif plat.startswith("android"):
64+
elif plat.startswith(_ANDROID):
5265
want_minor = "0"
5366
want_arch = tail
5467
else:
@@ -57,21 +70,13 @@ def _priority_by_platform(*, tag, values):
5770
if want_arch != arch:
5871
continue
5972

60-
want_version = (int(want_major), int(want_minor))
61-
if version <= want_version:
73+
want_version = (int(want_major), int(want_minor)) if want_major else None
74+
if not want_version or version <= want_version:
6275
keys.append((priority, version))
6376

6477
return max(keys) if keys else None
6578

66-
def _priority_by_abi(*, tag, values):
67-
keys = []
68-
for priority, wp in enumerate(values):
69-
if tag == wp:
70-
keys.append(priority)
71-
72-
return max(keys) if keys else None
73-
74-
def _priority_by_version(*, tag, implementation, py_version):
79+
def _python_tag_priority(*, tag, implementation, py_version):
7580
if tag.startswith(PY_TAG_GENERIC):
7681
ver_str = tag[len(PY_TAG_GENERIC):]
7782
elif tag.startswith(implementation):
@@ -114,7 +119,7 @@ def _candidates_by_priority(*, whls, implementation, py_version, whl_abi_tags, p
114119

115120
# See https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#compressed-tag-sets
116121
for platform in parsed.platform_tag.split("."):
117-
platform = _priority_by_platform(tag = platform, values = platforms)
122+
platform = _platform_tag_priority(tag = platform, values = platforms)
118123
if platform == None:
119124
if logger:
120125
logger.debug(lambda: "The platform_tag in '{}' does not match given list: {}".format(
@@ -124,7 +129,7 @@ def _candidates_by_priority(*, whls, implementation, py_version, whl_abi_tags, p
124129
continue
125130

126131
for py in parsed.python_tag.split("."):
127-
py = _priority_by_version(
132+
py = _python_tag_priority(
128133
tag = py,
129134
implementation = implementation,
130135
py_version = py_version,
@@ -139,7 +144,7 @@ def _candidates_by_priority(*, whls, implementation, py_version, whl_abi_tags, p
139144
continue
140145

141146
for abi in parsed.abi_tag.split("."):
142-
abi = _priority_by_abi(
147+
abi = _value_priority(
143148
tag = abi,
144149
values = whl_abi_tags,
145150
)

tests/pypi/select_whl/select_whl_tests.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,26 @@ def _test_multiple_musllinux(env):
413413

414414
_tests.append(_test_multiple_musllinux)
415415

416+
def _test_android(env):
417+
got = _select_whl(
418+
whls = [
419+
"pkg-0.0.1-py3-none-android_4_x86_64.whl",
420+
"pkg-0.0.1-py3-none-android_8_x86_64.whl",
421+
],
422+
platforms = ["android_5_x86_64"],
423+
whl_abi_tags = ["none"],
424+
python_version = "3.12",
425+
limit = 2,
426+
)
427+
_match(
428+
env,
429+
got,
430+
# select the one with the highest version that is matching
431+
"pkg-0.0.1-py3-none-android_4_x86_64.whl",
432+
)
433+
434+
_tests.append(_test_android)
435+
416436
def select_whl_test_suite(name):
417437
"""Create the test suite.
418438

0 commit comments

Comments
 (0)