Skip to content

Commit bb838c0

Browse files
committed
more cleanup
1 parent b00d7a2 commit bb838c0

File tree

7 files changed

+70
-104
lines changed

7 files changed

+70
-104
lines changed

python/private/pypi/BUILD.bazel

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,6 @@ bzl_library(
263263
],
264264
)
265265

266-
bzl_library(
267-
name = "pep508_platform_bzl",
268-
srcs = ["pep508_platform.bzl"],
269-
)
270-
271266
bzl_library(
272267
name = "pep508_requirement_bzl",
273268
srcs = ["pep508_requirement.bzl"],
@@ -341,6 +336,9 @@ bzl_library(
341336
bzl_library(
342337
name = "python_tag_bzl",
343338
srcs = ["python_tag.bzl"],
339+
deps = [
340+
"//python/private:version_bzl",
341+
],
344342
)
345343

346344
bzl_library(

python/private/pypi/extension.bzl

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,17 @@ def _platforms(*, python_version, minor_mapping, config):
7878
)
7979

8080
for platform, values in config.platforms.items():
81-
implementation = python_tag(values.env["implementation_name"])
82-
83-
# TODO @aignas 2025-07-07: move the abi construction somewhere else
84-
abi = "{impl}{0}{1}.{2}".format(
85-
impl = implementation,
86-
*python_version.release
87-
)
88-
key = "{}_{}".format(abi, platform)
89-
90-
env_ = env(struct(
91-
abi = abi,
92-
os = values.os_name,
93-
arch = values.arch_name,
94-
)) | values.env
95-
platforms[key] = struct(
96-
env = env_,
81+
# TODO @aignas 2025-07-07: this is probably doing the parsing of the version too
82+
# many times.
83+
pytag = python_tag(values.env["implementation_name"], python_version.string)
84+
85+
platforms["{}.{}_{}".format(pytag, python_version.release[2], platform)] = struct(
86+
env = env(
87+
env = values.env,
88+
os = values.os_name,
89+
arch = values.arch_name,
90+
python_version = python_version.string,
91+
),
9792
want_abis = [
9893
v.format(
9994
major = python_version.release[0],

python/private/pypi/pep508_env.bzl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""This module is for implementing PEP508 environment definition.
1616
"""
1717

18+
load("//python/private:version.bzl", "version")
19+
1820
_DEFAULT = "//conditions:default"
1921

2022
# See https://stackoverflow.com/a/45125525
@@ -144,37 +146,38 @@ def _get_from_map(m, key):
144146
else:
145147
return m[key]
146148

147-
def env(target_platform, *, extra = None):
149+
def env(*, env = None, os, arch, python_version = "", extra = None):
148150
"""Return an env target platform
149151
150152
NOTE: This is for use during the loading phase. For the analysis phase,
151153
`env_marker_setting()` constructs the env dict.
152154
153155
Args:
154-
target_platform: {type}`str` the target platform identifier, e.g.
155-
`cp33_linux_aarch64`
156+
env: {type}`str` the environment.
157+
os: {type}`str` the OS name.
158+
arch: {type}`str` the CPU name.
159+
python_version: {type}`str` the full python version.
156160
extra: {type}`str` the extra value to be added into the env.
157161
158162
Returns:
159163
A dict that can be used as `env` in the marker evaluation.
160164
"""
161-
env = create_env()
165+
env = env or {}
166+
env = env | create_env()
162167
if extra != None:
163168
env["extra"] = extra
164169

165-
if target_platform.abi:
166-
# TODO @aignas 2025-07-07: do not parse the version like this here
167-
minor_version, _, micro_version = target_platform.abi[3:].partition(".")
168-
micro_version = micro_version or "0"
170+
if python_version:
171+
v = version.parse(python_version)
169172
env = env | {
170-
"implementation_version": "3.{}.{}".format(minor_version, micro_version),
171-
"python_full_version": "3.{}.{}".format(minor_version, micro_version),
172-
"python_version": "3.{}".format(minor_version),
173+
"implementation_version": "{}.{}.{}".format(v.release[0], v.release[1], v.release[2]),
174+
"python_full_version": "{}.{}.{}".format(v.release[0], v.release[1], v.release[2]),
175+
"python_version": "{}.{}".format(v.release[0], v.release[1]),
173176
}
174177

175-
if target_platform.os and target_platform.arch:
176-
os = "@platforms//os:{}".format(target_platform.os)
177-
arch = "@platforms//cpu:{}".format(target_platform.arch)
178+
if os and arch:
179+
os = "@platforms//os:{}".format(os)
180+
arch = "@platforms//cpu:{}".format(arch)
178181
env = env | {
179182
"os_name": _get_from_map(os_name_select_map, os),
180183
"platform_machine": _get_from_map(platform_machine_select_map, arch),

python/private/pypi/pep508_platform.bzl

Lines changed: 0 additions & 57 deletions
This file was deleted.

python/private/pypi/python_tag.bzl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"A simple utility function to get the python_tag from the implementation name"
22

3+
load("//python/private:version.bzl", "version")
4+
35
# Taken from
46
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#python-tag
57
_PY_TAGS = {
@@ -8,16 +10,32 @@ _PY_TAGS = {
810
"ironpython": "ip",
911
"jython": "jy",
1012
"pypy": "pp",
13+
"python": "py",
1114
}
1215
PY_TAG_GENERIC = "py"
1316

14-
def python_tag(implementation_name):
17+
def python_tag(implementation_name, python_version = ""):
1518
"""Get the python_tag from the implementation_name.
1619
1720
Args:
1821
implementation_name: {type}`str` the implementation name, e.g. "cpython"
22+
python_version: {type}`str` a version who can be parsed using PEP440 compliant
23+
parser.
1924
2025
Returns:
21-
A {type}`str` that represents the python_tag.
26+
A {type}`str` that represents the python_tag with a version if the
27+
python_version is given.
2228
"""
23-
return _PY_TAGS.get(implementation_name, implementation_name)
29+
if python_version:
30+
v = version.parse(python_version, strict = True)
31+
suffix = "{}{}".format(
32+
v.release[0],
33+
v.release[1] if len(v.release) > 1 else "",
34+
)
35+
else:
36+
suffix = ""
37+
38+
return "{}{}".format(
39+
_PY_TAGS.get(implementation_name, implementation_name),
40+
suffix,
41+
)

tests/pypi/parse_requirements/parse_requirements_tests.bzl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
load("@rules_testing//lib:test_suite.bzl", "test_suite")
1818
load("//python/private/pypi:parse_requirements.bzl", "parse_requirements", "select_requirement") # buildifier: disable=bzl-visibility
1919
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility
20-
load("//python/private/pypi:pep508_platform.bzl", "platform_from_str") # buildifier: disable=bzl-visibility
2120

2221
def _mock_ctx():
2322
testdata = {
@@ -527,12 +526,20 @@ def _test_overlapping_shas_with_index_results(env):
527526
platforms = {
528527
"cp39_linux_x86_64": struct(
529528
platform_tags = ["any"],
530-
env = pep508_env(platform_from_str("cp39_linux_x86_64", "")),
529+
env = pep508_env(
530+
python_version = "3.9.0",
531+
os = "linux",
532+
arch = "x86_64",
533+
),
531534
want_abis = ["none"],
532535
),
533536
"cp39_osx_x86_64": struct(
534537
platform_tags = ["macosx_*"],
535-
env = pep508_env(platform_from_str("cp39_linux_x86_64", "")),
538+
env = pep508_env(
539+
python_version = "3.9.0",
540+
os = "osx",
541+
arch = "x86_64",
542+
),
536543
want_abis = ["none"],
537544
),
538545
},

tests/pypi/pep508/evaluate_tests.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
load("@rules_testing//lib:test_suite.bzl", "test_suite")
1717
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility
1818
load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize") # buildifier: disable=bzl-visibility
19-
load("//python/private/pypi:pep508_platform.bzl", "platform_from_str") # buildifier: disable=bzl-visibility
2019

2120
_tests = []
2221

@@ -244,26 +243,29 @@ _tests.append(_evaluate_partial_only_extra)
244243

245244
def _evaluate_with_aliases(env):
246245
# When
247-
for target_platform, tests in {
246+
for (os, cpu), tests in {
248247
# buildifier: @unsorted-dict-items
249-
"osx_aarch64": {
248+
("osx", "aarch64"): {
250249
"platform_system == 'Darwin' and platform_machine == 'arm64'": True,
251250
"platform_system == 'Darwin' and platform_machine == 'aarch64'": True,
252251
"platform_system == 'Darwin' and platform_machine == 'amd64'": False,
253252
},
254-
"osx_x86_64": {
253+
("osx", "x86_64"): {
255254
"platform_system == 'Darwin' and platform_machine == 'amd64'": True,
256255
"platform_system == 'Darwin' and platform_machine == 'x86_64'": True,
257256
},
258-
"osx_x86_32": {
257+
("osx", "x86_32"): {
259258
"platform_system == 'Darwin' and platform_machine == 'i386'": True,
260259
"platform_system == 'Darwin' and platform_machine == 'i686'": True,
261260
"platform_system == 'Darwin' and platform_machine == 'x86_32'": True,
262261
"platform_system == 'Darwin' and platform_machine == 'x86_64'": False,
263262
},
264263
}.items(): # buildifier: @unsorted-dict-items
265264
for input, want in tests.items():
266-
_check_evaluate(env, input, want, pep508_env(platform_from_str(target_platform, "")))
265+
_check_evaluate(env, input, want, pep508_env(
266+
os = os,
267+
arch = cpu,
268+
))
267269

268270
_tests.append(_evaluate_with_aliases)
269271

0 commit comments

Comments
 (0)