Skip to content

Commit 71f9fb0

Browse files
committed
fix(pypi): reuse select dicts for constructing the env
Before this PR we would be constructing slightly different environments when the `env_marker_setting` is doing it in the analysis phase and when we are doing it in the repo phase due to how the defaults are handled. In this change we simply reuse the same select statements and add an extra helper that is allowing us to process that. Work towards bazel-contrib#2949 Prep for bazel-contrib#3058
1 parent f02c9c7 commit 71f9fb0

File tree

8 files changed

+173
-135
lines changed

8 files changed

+173
-135
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
99

1010
test --test_output=errors
1111

python/private/pypi/BUILD.bazel

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ bzl_library(
252252
bzl_library(
253253
name = "pep508_env_bzl",
254254
srcs = ["pep508_env.bzl"],
255+
deps = [
256+
"//python/private:version_bzl",
257+
],
255258
)
256259

257260
bzl_library(
@@ -263,11 +266,6 @@ bzl_library(
263266
],
264267
)
265268

266-
bzl_library(
267-
name = "pep508_platform_bzl",
268-
srcs = ["pep508_platform.bzl"],
269-
)
270-
271269
bzl_library(
272270
name = "pep508_requirement_bzl",
273271
srcs = ["pep508_requirement.bzl"],

python/private/pypi/extension.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ def _platforms(*, python_version, minor_mapping, config):
7676

7777
for platform, values in config.platforms.items():
7878
key = "{}_{}".format(abi, platform)
79-
platforms[key] = env(struct(
80-
abi = abi,
79+
platforms[key] = env(
80+
env = values.env,
8181
os = values.os_name,
8282
arch = values.arch_name,
83-
)) | values.env
83+
python_version = python_version,
84+
)
8485
return platforms
8586

8687
def _create_whl_repos(

python/private/pypi/pep508_env.bzl

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,27 @@
1515
"""This module is for implementing PEP508 environment definition.
1616
"""
1717

18+
load("//python/private:version.bzl", "version")
19+
20+
_DEFAULT = "//conditions:default"
21+
22+
# Here we store the aliases in the platform so that the users can specify any valid target in
23+
# there.
24+
_cpu_aliases = {
25+
"arm": "aarch32",
26+
"arm64": "aarch64",
27+
}
28+
_os_aliases = {
29+
"macos": "osx",
30+
}
31+
1832
# See https://stackoverflow.com/a/45125525
1933
platform_machine_aliases = {
20-
# These pairs mean the same hardware, but different values may be used
21-
# on different host platforms.
34+
# These pairs mean the same hardware, but different values may be used on different host
35+
# platforms.
36+
#
37+
# What is more we ensure in this way that we have all of the values from the platforms
38+
# package when constructing the env.
2239
"amd64": "x86_64",
2340
"arm64": "aarch64",
2441
"i386": "x86_32",
@@ -32,6 +49,7 @@ platform_machine_aliases = {
3249
platform_machine_select_map = {
3350
"@platforms//cpu:aarch32": "aarch32",
3451
"@platforms//cpu:aarch64": "aarch64",
52+
# We store them above
3553
# @platforms//cpu:arm is an alias for @platforms//cpu:aarch32
3654
# @platforms//cpu:arm64 is an alias for @platforms//cpu:aarch64
3755
"@platforms//cpu:arm64_32": "arm64_32",
@@ -59,31 +77,25 @@ platform_machine_select_map = {
5977
"@platforms//cpu:x86_64": "x86_64",
6078
# The value is empty string if it cannot be determined:
6179
# https://docs.python.org/3/library/platform.html#platform.machine
62-
"//conditions:default": "",
80+
_DEFAULT: "",
6381
}
6482

6583
# Platform system returns results from the `uname` call.
66-
_platform_system_values = {
84+
platform_system_select_map = {
6785
# See https://peps.python.org/pep-0738/#platform
68-
"android": "Android",
69-
"freebsd": "FreeBSD",
86+
"@platforms//os:android": "Android",
87+
"@platforms//os:freebsd": "FreeBSD",
7088
# See https://peps.python.org/pep-0730/#platform
7189
# NOTE: Per Pep 730, "iPadOS" is also an acceptable value
72-
"ios": "iOS",
73-
"linux": "Linux",
74-
"netbsd": "NetBSD",
75-
"openbsd": "OpenBSD",
76-
"osx": "Darwin",
77-
"windows": "Windows",
78-
}
79-
80-
platform_system_select_map = {
81-
"@platforms//os:{}".format(bazel_os): py_system
82-
for bazel_os, py_system in _platform_system_values.items()
83-
} | {
90+
"@platforms//os:ios": "iOS",
91+
"@platforms//os:linux": "Linux",
92+
"@platforms//os:netbsd": "NetBSD",
93+
"@platforms//os:openbsd": "OpenBSD",
94+
"@platforms//os:osx": "Darwin",
95+
"@platforms//os:windows": "Windows",
8496
# The value is empty string if it cannot be determined:
8597
# https://docs.python.org/3/library/platform.html#platform.machine
86-
"//conditions:default": "",
98+
_DEFAULT: "",
8799
}
88100

89101
# The copy of SO [answer](https://stackoverflow.com/a/13874620) containing
@@ -111,83 +123,83 @@ platform_system_select_map = {
111123
# (**) Prior Python 3.8 could also be aix5 or aix7; use sys.platform.startswith()
112124
#
113125
# We are using only the subset that we actually support.
114-
_sys_platform_values = {
126+
sys_platform_select_map = {
115127
# These values are decided by the sys.platform docs.
116-
"android": "android",
117-
"emscripten": "emscripten",
128+
"@platforms//os:android": "android",
129+
"@platforms//os:emscripten": "emscripten",
118130
# NOTE: The below values are approximations. The sys.platform() docs
119131
# don't have documented values for these OSes. Per docs, the
120132
# sys.platform() value reflects the OS at the time Python was *built*
121133
# instead of the runtime (target) OS value.
122-
"freebsd": "freebsd",
123-
"ios": "ios",
124-
"linux": "linux",
125-
"openbsd": "openbsd",
126-
"osx": "darwin",
127-
"wasi": "wasi",
128-
"windows": "win32",
129-
}
130-
131-
sys_platform_select_map = {
132-
"@platforms//os:{}".format(bazel_os): py_platform
133-
for bazel_os, py_platform in _sys_platform_values.items()
134-
} | {
134+
"@platforms//os:freebsd": "freebsd",
135+
"@platforms//os:ios": "ios",
136+
"@platforms//os:linux": "linux",
137+
"@platforms//os:openbsd": "openbsd",
138+
# @platforms//os:macos is an alias for @platforms//os:osx
139+
"@platforms//os:osx": "darwin",
140+
"@platforms//os:wasi": "wasi",
141+
"@platforms//os:windows": "win32",
135142
# For lack of a better option, use empty string. No standard doc/spec
136143
# about sys_platform value.
137-
"//conditions:default": "",
144+
_DEFAULT: "",
138145
}
139146

140147
# The "java" value is documented, but with Jython defunct,
141148
# shouldn't occur in practice.
142149
# The os.name value is technically a property of the runtime, not the
143150
# targetted runtime OS, but the distinction shouldn't matter if
144151
# things are properly configured.
145-
_os_name_values = {
146-
"linux": "posix",
147-
"osx": "posix",
148-
"windows": "nt",
149-
}
150-
151152
os_name_select_map = {
152-
"@platforms//os:{}".format(bazel_os): py_os
153-
for bazel_os, py_os in _os_name_values.items()
154-
} | {
155-
"//conditions:default": "posix",
153+
"@platforms//os:windows": "nt",
154+
_DEFAULT: "posix",
156155
}
157156

158-
def env(target_platform, *, extra = None):
157+
def _get_from_map(m, key):
158+
if _DEFAULT in m:
159+
return m.get(key, m[_DEFAULT])
160+
else:
161+
return m[key]
162+
163+
def env(*, env = None, os, arch, python_version = "", extra = None):
159164
"""Return an env target platform
160165
161166
NOTE: This is for use during the loading phase. For the analysis phase,
162167
`env_marker_setting()` constructs the env dict.
163168
164169
Args:
165-
target_platform: {type}`str` the target platform identifier, e.g.
166-
`cp33_linux_aarch64`
170+
env: {type}`str` the environment.
171+
os: {type}`str` the OS name.
172+
arch: {type}`str` the CPU name.
173+
python_version: {type}`str` the full python version.
167174
extra: {type}`str` the extra value to be added into the env.
168175
169176
Returns:
170177
A dict that can be used as `env` in the marker evaluation.
171178
"""
172-
env = create_env()
179+
env = env or {}
180+
env = env | create_env()
173181
if extra != None:
174182
env["extra"] = extra
175183

176-
if target_platform.abi:
177-
minor_version, _, micro_version = target_platform.abi[3:].partition(".")
178-
micro_version = micro_version or "0"
184+
if python_version:
185+
v = version.parse(python_version)
186+
major = v.release[0]
187+
minor = v.release[1]
188+
micro = v.release[2] if len(v.release) > 2 else 0
179189
env = env | {
180-
"implementation_version": "3.{}.{}".format(minor_version, micro_version),
181-
"python_full_version": "3.{}.{}".format(minor_version, micro_version),
182-
"python_version": "3.{}".format(minor_version),
190+
"implementation_version": "{}.{}.{}".format(major, minor, micro),
191+
"python_full_version": "{}.{}.{}".format(major, minor, micro),
192+
"python_version": "{}.{}".format(major, minor),
183193
}
184-
if target_platform.os and target_platform.arch:
185-
os = target_platform.os
194+
195+
if os and arch:
196+
os = "@platforms//os:{}".format(_os_aliases.get(os, os))
197+
arch = "@platforms//cpu:{}".format(_cpu_aliases.get(arch, arch))
186198
env = env | {
187-
"os_name": _os_name_values.get(os, ""),
188-
"platform_machine": target_platform.arch,
189-
"platform_system": _platform_system_values.get(os, ""),
190-
"sys_platform": _sys_platform_values.get(os, ""),
199+
"os_name": _get_from_map(os_name_select_map, os),
200+
"platform_machine": _get_from_map(platform_machine_select_map, arch),
201+
"platform_system": _get_from_map(platform_system_select_map, os),
202+
"sys_platform": _get_from_map(sys_platform_select_map, os),
191203
}
192204
set_missing_env_defaults(env)
193205

python/private/pypi/pep508_platform.bzl

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

tests/pypi/pep508/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
load(":deps_tests.bzl", "deps_test_suite")
2+
load(":env_tests.bzl", "env_test_suite")
23
load(":evaluate_tests.bzl", "evaluate_test_suite")
34
load(":requirement_tests.bzl", "requirement_test_suite")
45

56
deps_test_suite(
67
name = "deps_tests",
78
)
89

10+
env_test_suite(
11+
name = "env_tests",
12+
)
13+
914
evaluate_test_suite(
1015
name = "evaluate_tests",
1116
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy