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
2 changes: 1 addition & 1 deletion python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_library.bzl", "py_library")
load("//python:versions.bzl", "print_toolchains_checksums")
load(":print_toolchain_checksums.bzl", "print_toolchains_checksums")
load(":py_exec_tools_toolchain.bzl", "current_interpreter_executable")
load(":sentinel.bzl", "sentinel")
load(":stamp.bzl", "stamp_build_setting")
Expand Down
92 changes: 92 additions & 0 deletions python/private/print_toolchain_checksums.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Print the toolchain versions.
"""

load("//python:versions.bzl", "TOOL_VERSIONS", "get_release_info")
load("//python/private:text_util.bzl", "render")
load("//python/private:version.bzl", "version")

def print_toolchains_checksums(name):
"""A macro to print checksums for a particular Python interpreter version.

Args:
name: {type}`str`: the name of the runnable target.
"""
by_version = {}

for python_version, metadata in TOOL_VERSIONS.items():
by_version[python_version] = _commands_for_version(
python_version = python_version,
metadata = metadata,
)

all_commands = sorted(
by_version.items(),
key = lambda x: version.key(version.parse(x[0], strict = True)),
)
all_commands = [x[1] for x in all_commands]

template = """\
cat > "$@" <<'EOF'
#!/bin/bash

set -o errexit -o nounset -o pipefail

echo "Fetching hashes..."

{commands}
EOF
"""

native.genrule(
name = name,
srcs = [],
outs = ["print_toolchains_checksums.sh"],
cmd = select({
"//python/config_settings:is_python_{}".format(version_str): template.format(
commands = commands,
)
for version_str, commands in by_version.items()
} | {
"//conditions:default": template.format(commands = "\n".join(all_commands)),
}),
executable = True,
)

def _commands_for_version(*, python_version, metadata):
lines = []
lines += [
"cat <<EOB", # end of block
" \"{python_version}\": {{".format(python_version = python_version),
" \"url\": \"{url}\",".format(url = metadata["url"]),
" \"sha256\": {",
]

for platform in metadata["sha256"].keys():
for release_url in get_release_info(platform, python_version)[1]:
# Do lines one by one so that the progress is seen better and use cat for ease of quotation
lines += [
"EOB",
"cat <<EOB",
" \"{platform}\": \"$$({get_sha256})\",".format(
platform = platform,
get_sha256 = "curl --silent --show-error --location --fail {release_url_sha256}".format(
release_url = release_url,
release_url_sha256 = release_url + ".sha256",
),
),
]

prefix = metadata["strip_prefix"]
prefix = render.indent(
render.dict(prefix) if type(prefix) == type({}) else repr(prefix),
indent = " " * 8,
).lstrip()

lines += [
" },",
" \"strip_prefix\": {strip_prefix},".format(strip_prefix = prefix),
" },",
"EOB",
]

return "\n".join(lines)
54 changes: 3 additions & 51 deletions python/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ DEFAULT_RELEASE_BASE_URL = "https://github.com/astral-sh/python-build-standalone
# the hashes:
# bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version={major}.{minor}.{patch}
#
# To print hashes for all of the specified versions, run:
# bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version=""
#
# Note, to users looking at how to specify their tool versions, coverage_tool version for each
# interpreter can be specified by:
# "3.8.10": {
Expand Down Expand Up @@ -1073,57 +1076,6 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U

return (release_filename, rendered_urls, strip_prefix, patches, patch_strip)

def print_toolchains_checksums(name):
"""A macro to print checksums for a particular Python interpreter version.

Args:
name: {type}`str`: the name of the runnable target.
"""
all_commands = []
by_version = {}
for python_version in TOOL_VERSIONS.keys():
by_version[python_version] = _commands_for_version(python_version)
all_commands.append(_commands_for_version(python_version))

template = """\
cat > "$@" <<'EOF'
#!/bin/bash

set -o errexit -o nounset -o pipefail

echo "Fetching hashes..."

{commands}
EOF
"""

native.genrule(
name = name,
srcs = [],
outs = ["print_toolchains_checksums.sh"],
cmd = select({
"//python/config_settings:is_python_{}".format(version): template.format(
commands = commands,
)
for version, commands in by_version.items()
} | {
"//conditions:default": template.format(commands = "\n".join(all_commands)),
}),
executable = True,
)

def _commands_for_version(python_version):
return "\n".join([
"echo \"{python_version}: {platform}: $$(curl --location --fail {release_url_sha256} 2>/dev/null || curl --location --fail {release_url} 2>/dev/null | shasum -a 256 | awk '{{ print $$1 }}')\"".format(
python_version = python_version,
platform = platform,
release_url = release_url,
release_url_sha256 = release_url + ".sha256",
)
for platform in TOOL_VERSIONS[python_version]["sha256"].keys()
for release_url in get_release_info(platform, python_version)[1]
])

def gen_python_config_settings(name = ""):
for platform in PLATFORMS.keys():
native.config_setting(
Expand Down