From 063d48a6e8f88d9b17f373b8173251e462253f98 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Mon, 14 Mar 2022 11:11:38 -0400 Subject: [PATCH] fix: take custom tools versions into account when selecting Python interpreter --- python/repositories.bzl | 26 +++++++++++++++++++------- python/versions.bzl | 36 +++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/python/repositories.bzl b/python/repositories.bzl index 60ab91824d..09683261de 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -41,8 +41,8 @@ def _python_repository_impl(rctx): platform = rctx.attr.platform python_version = rctx.attr.python_version - base_url = rctx.attr.base_url - (release_filename, url) = get_release_url(platform, python_version, base_url) + release_filename = rctx.attr.release_filename + url = rctx.attr.url if release_filename.endswith(".zst"): rctx.download( @@ -101,7 +101,7 @@ def _python_repository_impl(rctx): if exec_result.return_code: fail(exec_result.stderr) - python_bin = "python.exe" if ("windows" in rctx.attr.platform) else "bin/python3" + python_bin = "python.exe" if ("windows" in platform) else "bin/python3" build_content = """\ # Generated by python/repositories.bzl @@ -155,17 +155,15 @@ py_runtime_pair( "name": rctx.attr.name, "platform": platform, "python_version": python_version, + "release_filename": release_filename, "sha256": rctx.attr.sha256, + "url": url, } python_repository = repository_rule( _python_repository_impl, doc = "Fetches the external tools needed for the Python toolchain.", attrs = { - "base_url": attr.string( - doc = "The base URL used for releases, will be joined to the templated 'url' field in the tool_versions dict", - default = DEFAULT_RELEASE_BASE_URL, - ), "distutils": attr.label( allow_single_file = True, doc = "A distutils.cfg file to be included in the Python installation. " + @@ -187,10 +185,18 @@ python_repository = repository_rule( mandatory = True, values = TOOL_VERSIONS.keys() + MINOR_MAPPING.keys(), ), + "release_filename": attr.string( + doc = "The filename of the interpreter to be downloaded", + mandatory = True, + ), "sha256": attr.string( doc = "The SHA256 integrity hash for the Python interpreter tarball.", mandatory = True, ), + "url": attr.string( + doc = "The URL of the interpreter to download", + mandatory = True, + ), "zstd_sha256": attr.string( default = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0", ), @@ -229,6 +235,8 @@ def python_register_toolchains( in python/versions.bzl will be used **kwargs: passed to each python_repositories call. """ + base_url = kwargs.pop("base_url", DEFAULT_RELEASE_BASE_URL) + if python_version in MINOR_MAPPING: python_version = MINOR_MAPPING[python_version] @@ -237,6 +245,8 @@ def python_register_toolchains( if not sha256: continue + (release_filename, url) = get_release_url(platform, python_version, base_url, tool_versions) + python_repository( name = "{name}_{platform}".format( name = name, @@ -245,6 +255,8 @@ def python_register_toolchains( sha256 = sha256, platform = platform, python_version = python_version, + release_filename = release_filename, + url = url, distutils = distutils, distutils_content = distutils_content, **kwargs diff --git a/python/versions.bzl b/python/versions.bzl index 8aaa4a00ff..c5445ec030 100644 --- a/python/versions.bzl +++ b/python/versions.bzl @@ -22,15 +22,6 @@ WINDOWS_NAME = "windows" DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/releases/download" -def get_release_url(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL): - release_filename = TOOL_VERSIONS[python_version]["url"].format( - platform = platform, - python_version = python_version, - build = "static-install_only" if (WINDOWS_NAME in platform) else "install_only", - ) - url = "/".join([base_url, release_filename]) - return (release_filename, url) - # When updating the versions and releases, run the following command to get # the hashes: # bazel run //python/private:print_toolchains_checksums @@ -47,6 +38,7 @@ TOOL_VERSIONS = { "3.8.12": { "url": "20220227/cpython-{python_version}+20220227-{platform}-{build}.tar.gz", "sha256": { + "aarch64-apple-darwin": "f9a3cbb81e0463d6615125964762d133387d561b226a30199f5b039b20f1d944", "x86_64-apple-darwin": "f323fbc558035c13a85ce2267d0fad9e89282268ecb810e364fff1d0a079d525", "x86_64-pc-windows-msvc": "924f9fd51ff6ccc533ed8e96c5461768da5781eb3dfc11d846f9e300fab44eda", "x86_64-unknown-linux-gnu": "5be9c6d61e238b90dfd94755051c0d3a2d8023ebffdb4b0fa4e8fedd09a6cab6", @@ -116,6 +108,32 @@ PLATFORMS = { ), } +def get_release_url(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS): + """Resolve the release URL for the requested interpreter version + + Args: + platform: The platform string for the interpreter + python_version: The version of the intterpreter to get + base_url: The URL to prepend to the 'url' attr in the tool_versions dict + tool_versions: A dict listing the interpreter versions, their SHAs and URL + + Returns: + filename and url pair + """ + + url = tool_versions[python_version]["url"] + + if type(url) == type({}): + url = url[platform] + + release_filename = url.format( + platform = platform, + python_version = python_version, + build = "static-install_only" if (WINDOWS_NAME in platform) else "install_only", + ) + url = "/".join([base_url, release_filename]) + return (release_filename, url) + def print_toolchains_checksums(name): native.genrule( name = name,