Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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. " +
Expand All @@ -187,10 +185,18 @@ python_repository = repository_rule(
mandatory = True,
values = TOOL_VERSIONS.keys() + MINOR_MAPPING.keys(),
),
"release_filename": attr.string(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm it's smelly there's no docs/ change in this PR, something must not be wired up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is exported, the macro wrapping it is, and these attrs are set in the macro

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",
),
Expand Down Expand Up @@ -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]

Expand All @@ -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,
Expand All @@ -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
Expand Down
36 changes: 27 additions & 9 deletions python/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down