Skip to content

Commit ef899ad

Browse files
committed
fix: take custom tools versions into account when selecting Python interpreter (bazel-contrib#654)
1 parent 62ebe03 commit ef899ad

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

python/repositories.bzl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def _python_repository_impl(rctx):
4141

4242
platform = rctx.attr.platform
4343
python_version = rctx.attr.python_version
44-
base_url = rctx.attr.base_url
45-
(release_filename, url) = get_release_url(platform, python_version, base_url)
44+
release_filename = rctx.attr.release_filename
45+
url = rctx.attr.url
4646

4747
if release_filename.endswith(".zst"):
4848
rctx.download(
@@ -101,7 +101,7 @@ def _python_repository_impl(rctx):
101101
if exec_result.return_code:
102102
fail(exec_result.stderr)
103103

104-
python_bin = "python.exe" if ("windows" in rctx.attr.platform) else "bin/python3"
104+
python_bin = "python.exe" if ("windows" in platform) else "bin/python3"
105105

106106
build_content = """\
107107
# Generated by python/repositories.bzl
@@ -155,17 +155,15 @@ py_runtime_pair(
155155
"name": rctx.attr.name,
156156
"platform": platform,
157157
"python_version": python_version,
158+
"release_filename": release_filename,
158159
"sha256": rctx.attr.sha256,
160+
"url": url,
159161
}
160162

161163
python_repository = repository_rule(
162164
_python_repository_impl,
163165
doc = "Fetches the external tools needed for the Python toolchain.",
164166
attrs = {
165-
"base_url": attr.string(
166-
doc = "The base URL used for releases, will be joined to the templated 'url' field in the tool_versions dict",
167-
default = DEFAULT_RELEASE_BASE_URL,
168-
),
169167
"distutils": attr.label(
170168
allow_single_file = True,
171169
doc = "A distutils.cfg file to be included in the Python installation. " +
@@ -187,10 +185,18 @@ python_repository = repository_rule(
187185
mandatory = True,
188186
values = TOOL_VERSIONS.keys() + MINOR_MAPPING.keys(),
189187
),
188+
"release_filename": attr.string(
189+
doc = "The filename of the interpreter to be downloaded",
190+
mandatory = True,
191+
),
190192
"sha256": attr.string(
191193
doc = "The SHA256 integrity hash for the Python interpreter tarball.",
192194
mandatory = True,
193195
),
196+
"url": attr.string(
197+
doc = "The URL of the interpreter to download",
198+
mandatory = True,
199+
),
194200
"zstd_sha256": attr.string(
195201
default = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0",
196202
),
@@ -229,6 +235,8 @@ def python_register_toolchains(
229235
in python/versions.bzl will be used
230236
**kwargs: passed to each python_repositories call.
231237
"""
238+
base_url = kwargs.pop("base_url", DEFAULT_RELEASE_BASE_URL)
239+
232240
if python_version in MINOR_MAPPING:
233241
python_version = MINOR_MAPPING[python_version]
234242

@@ -237,6 +245,8 @@ def python_register_toolchains(
237245
if not sha256:
238246
continue
239247

248+
(release_filename, url) = get_release_url(platform, python_version, base_url, tool_versions)
249+
240250
python_repository(
241251
name = "{name}_{platform}".format(
242252
name = name,
@@ -245,6 +255,8 @@ def python_register_toolchains(
245255
sha256 = sha256,
246256
platform = platform,
247257
python_version = python_version,
258+
release_filename = release_filename,
259+
url = url,
248260
distutils = distutils,
249261
distutils_content = distutils_content,
250262
**kwargs

python/versions.bzl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ WINDOWS_NAME = "windows"
2222

2323
DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/releases/download"
2424

25-
def get_release_url(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL):
26-
release_filename = TOOL_VERSIONS[python_version]["url"].format(
27-
platform = platform,
28-
python_version = python_version,
29-
build = "static-install_only" if (WINDOWS_NAME in platform) else "install_only",
30-
)
31-
url = "/".join([base_url, release_filename])
32-
return (release_filename, url)
33-
3425
# When updating the versions and releases, run the following command to get
3526
# the hashes:
3627
# bazel run //python/private:print_toolchains_checksums
@@ -47,6 +38,7 @@ TOOL_VERSIONS = {
4738
"3.8.12": {
4839
"url": "20220227/cpython-{python_version}+20220227-{platform}-{build}.tar.gz",
4940
"sha256": {
41+
"aarch64-apple-darwin": "f9a3cbb81e0463d6615125964762d133387d561b226a30199f5b039b20f1d944",
5042
"x86_64-apple-darwin": "f323fbc558035c13a85ce2267d0fad9e89282268ecb810e364fff1d0a079d525",
5143
"x86_64-pc-windows-msvc": "924f9fd51ff6ccc533ed8e96c5461768da5781eb3dfc11d846f9e300fab44eda",
5244
"x86_64-unknown-linux-gnu": "5be9c6d61e238b90dfd94755051c0d3a2d8023ebffdb4b0fa4e8fedd09a6cab6",
@@ -116,6 +108,32 @@ PLATFORMS = {
116108
),
117109
}
118110

111+
def get_release_url(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS):
112+
"""Resolve the release URL for the requested interpreter version
113+
114+
Args:
115+
platform: The platform string for the interpreter
116+
python_version: The version of the intterpreter to get
117+
base_url: The URL to prepend to the 'url' attr in the tool_versions dict
118+
tool_versions: A dict listing the interpreter versions, their SHAs and URL
119+
120+
Returns:
121+
filename and url pair
122+
"""
123+
124+
url = tool_versions[python_version]["url"]
125+
126+
if type(url) == type({}):
127+
url = url[platform]
128+
129+
release_filename = url.format(
130+
platform = platform,
131+
python_version = python_version,
132+
build = "static-install_only" if (WINDOWS_NAME in platform) else "install_only",
133+
)
134+
url = "/".join([base_url, release_filename])
135+
return (release_filename, url)
136+
119137
def print_toolchains_checksums(name):
120138
native.genrule(
121139
name = name,

0 commit comments

Comments
 (0)