Skip to content

Commit b1ae72e

Browse files
committed
refactor: cleanup and rename
1 parent 132124c commit b1ae72e

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

python/private/pypi/select_whl.bzl

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,59 @@ def _value_priority(*, tag, values):
1818

1919
return max(keys) if keys else None
2020

21-
def _platform_tag_priority(*, tag, values):
22-
# Implements matching platform tag
23-
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
24-
25-
if not (
21+
def _is_platform_tag_versioned(tag):
22+
return (
2623
tag.startswith(_ANDROID) or
2724
tag.startswith(_IOS) or
2825
tag.startswith(_MACOSX) or
2926
tag.startswith(_MANYLINUX) or
3027
tag.startswith(_MUSLLINUX)
31-
):
28+
)
29+
30+
def _parse_platform_tags(tags):
31+
"""A helper function that parses all of the platform tags.
32+
33+
The main idea is to make this more robust and have better debug messages about which will
34+
is compatible and which is not with the target platform.
35+
"""
36+
ret = []
37+
replacements = {}
38+
for tag in tags:
39+
if not _is_platform_tag_versioned(tag):
40+
ret.append(tag)
41+
continue
42+
43+
want_os, sep, tail = tag.partition("_")
44+
if not sep:
45+
fail("could not parse the tag")
46+
47+
want_major, _, tail = tail.partition("_")
48+
if want_major == "*":
49+
# the expected match is any version
50+
want_arch = tail
51+
elif want_os.startswith(_ANDROID):
52+
want_arch = tail
53+
else:
54+
# drop the minor version segment
55+
_, _, want_arch = tail.partition("_")
56+
57+
placeholder = "{}_*_{}".format(want_os, want_arch)
58+
replacements[placeholder] = tag
59+
if placeholder in ret:
60+
ret.remove(placeholder)
61+
62+
ret.append(placeholder)
63+
64+
return [
65+
replacements.get(p, p)
66+
for p in ret
67+
]
68+
69+
def _platform_tag_priority(*, tag, values):
70+
# Implements matching platform tag
71+
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
72+
73+
if not _is_platform_tag_versioned(tag):
3274
res = _value_priority(tag = tag, values = values)
3375
if res == None:
3476
return res
@@ -39,7 +81,7 @@ def _platform_tag_priority(*, tag, values):
3981

4082
os, _, tail = tag.partition("_")
4183
major, _, tail = tail.partition("_")
42-
if not os.startswith(_ANDROID):
84+
if not tag.startswith(_ANDROID):
4385
minor, _, arch = tail.partition("_")
4486
else:
4587
minor = "0"
@@ -65,7 +107,7 @@ def _platform_tag_priority(*, tag, values):
65107
want_major = ""
66108
want_minor = ""
67109
want_arch = tail
68-
elif os.startswith(_ANDROID):
110+
elif tag.startswith(_ANDROID):
69111
# we set it to `0` above, so setting the `want_minor` her to `0` will make things
70112
# consistent.
71113
want_minor = "0"
@@ -105,46 +147,6 @@ def _python_tag_priority(*, tag, implementation, py_version):
105147
version.key(ver),
106148
)
107149

108-
def _filter_platform_tags(tags):
109-
ret = []
110-
replacements = {}
111-
for tag in tags:
112-
if not (
113-
tag.startswith(_ANDROID) or
114-
tag.startswith(_IOS) or
115-
tag.startswith(_MACOSX) or
116-
tag.startswith(_MANYLINUX) or
117-
tag.startswith(_MUSLLINUX)
118-
):
119-
ret.append(tag)
120-
continue
121-
122-
want_os, sep, tail = tag.partition("_")
123-
if not sep:
124-
fail("could not parse the tag")
125-
126-
want_major, _, tail = tail.partition("_")
127-
if want_major == "*":
128-
# the expected match is any version
129-
want_arch = tail
130-
elif want_os.startswith(_ANDROID):
131-
want_arch = tail
132-
else:
133-
# drop the minor version segment
134-
_, _, want_arch = tail.partition("_")
135-
136-
placeholder = "{}_*_{}".format(want_os, want_arch)
137-
replacements[placeholder] = tag
138-
if placeholder in ret:
139-
ret.remove(placeholder)
140-
141-
ret.append(placeholder)
142-
143-
return [
144-
replacements.get(p, p)
145-
for p in ret
146-
]
147-
148150
def _candidates_by_priority(
149151
*,
150152
whls,
@@ -161,9 +163,6 @@ def _candidates_by_priority(
161163
"""
162164
py_version = version.parse(python_version, strict = True)
163165
implementation = python_tag(implementation_name)
164-
logger.debug(lambda: "input: {}".format(whl_platform_tags))
165-
whl_platform_tags = _filter_platform_tags(whl_platform_tags)
166-
logger.debug(lambda: "output: {}".format(whl_platform_tags))
167166

168167
ret = {}
169168
for whl in whls:
@@ -265,7 +264,7 @@ def select_whl(
265264
implementation_name = implementation_name,
266265
python_version = python_version,
267266
whl_abi_tags = whl_abi_tags,
268-
whl_platform_tags = whl_platform_tags,
267+
whl_platform_tags = _parse_platform_tags(whl_platform_tags),
269268
logger = logger,
270269
)
271270

0 commit comments

Comments
 (0)