Skip to content

Commit 2690e3f

Browse files
authored
refactor(toolchains): better sha256 printing helper (bazel-contrib#3028)
Before this PR the toolchain sha256 values would be printed in a way that would require further text manipulation. Now we print the values that need to be just copy pasted. Whilst at it simplify the `curl` command to remove the conditional. Testing done: ``` $ bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version="" # And then paste all of the output into the inside of the TOOL_VERSIONS ``` Work towards bazel-contrib#2704
1 parent 29a7f6a commit 2690e3f

File tree

3 files changed

+96
-52
lines changed

3 files changed

+96
-52
lines changed

python/private/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1616
load("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
1717
load("//python:py_binary.bzl", "py_binary")
1818
load("//python:py_library.bzl", "py_library")
19-
load("//python:versions.bzl", "print_toolchains_checksums")
19+
load(":print_toolchain_checksums.bzl", "print_toolchains_checksums")
2020
load(":py_exec_tools_toolchain.bzl", "current_interpreter_executable")
2121
load(":sentinel.bzl", "sentinel")
2222
load(":stamp.bzl", "stamp_build_setting")
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Print the toolchain versions.
2+
"""
3+
4+
load("//python:versions.bzl", "TOOL_VERSIONS", "get_release_info")
5+
load("//python/private:text_util.bzl", "render")
6+
load("//python/private:version.bzl", "version")
7+
8+
def print_toolchains_checksums(name):
9+
"""A macro to print checksums for a particular Python interpreter version.
10+
11+
Args:
12+
name: {type}`str`: the name of the runnable target.
13+
"""
14+
by_version = {}
15+
16+
for python_version, metadata in TOOL_VERSIONS.items():
17+
by_version[python_version] = _commands_for_version(
18+
python_version = python_version,
19+
metadata = metadata,
20+
)
21+
22+
all_commands = sorted(
23+
by_version.items(),
24+
key = lambda x: version.key(version.parse(x[0], strict = True)),
25+
)
26+
all_commands = [x[1] for x in all_commands]
27+
28+
template = """\
29+
cat > "$@" <<'EOF'
30+
#!/bin/bash
31+
32+
set -o errexit -o nounset -o pipefail
33+
34+
echo "Fetching hashes..."
35+
36+
{commands}
37+
EOF
38+
"""
39+
40+
native.genrule(
41+
name = name,
42+
srcs = [],
43+
outs = ["print_toolchains_checksums.sh"],
44+
cmd = select({
45+
"//python/config_settings:is_python_{}".format(version_str): template.format(
46+
commands = commands,
47+
)
48+
for version_str, commands in by_version.items()
49+
} | {
50+
"//conditions:default": template.format(commands = "\n".join(all_commands)),
51+
}),
52+
executable = True,
53+
)
54+
55+
def _commands_for_version(*, python_version, metadata):
56+
lines = []
57+
lines += [
58+
"cat <<EOB", # end of block
59+
" \"{python_version}\": {{".format(python_version = python_version),
60+
" \"url\": \"{url}\",".format(url = metadata["url"]),
61+
" \"sha256\": {",
62+
]
63+
64+
for platform in metadata["sha256"].keys():
65+
for release_url in get_release_info(platform, python_version)[1]:
66+
# Do lines one by one so that the progress is seen better and use cat for ease of quotation
67+
lines += [
68+
"EOB",
69+
"cat <<EOB",
70+
" \"{platform}\": \"$$({get_sha256})\",".format(
71+
platform = platform,
72+
get_sha256 = "curl --silent --show-error --location --fail {release_url_sha256}".format(
73+
release_url = release_url,
74+
release_url_sha256 = release_url + ".sha256",
75+
),
76+
),
77+
]
78+
79+
prefix = metadata["strip_prefix"]
80+
prefix = render.indent(
81+
render.dict(prefix) if type(prefix) == type({}) else repr(prefix),
82+
indent = " " * 8,
83+
).lstrip()
84+
85+
lines += [
86+
" },",
87+
" \"strip_prefix\": {strip_prefix},".format(strip_prefix = prefix),
88+
" },",
89+
"EOB",
90+
]
91+
92+
return "\n".join(lines)

python/versions.bzl

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ DEFAULT_RELEASE_BASE_URL = "https://github.com/astral-sh/python-build-standalone
3232
# the hashes:
3333
# bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version={major}.{minor}.{patch}
3434
#
35+
# To print hashes for all of the specified versions, run:
36+
# bazel run //python/private:print_toolchains_checksums --//python/config_settings:python_version=""
37+
#
3538
# Note, to users looking at how to specify their tool versions, coverage_tool version for each
3639
# interpreter can be specified by:
3740
# "3.8.10": {
@@ -1092,57 +1095,6 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
10921095

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

1095-
def print_toolchains_checksums(name):
1096-
"""A macro to print checksums for a particular Python interpreter version.
1097-
1098-
Args:
1099-
name: {type}`str`: the name of the runnable target.
1100-
"""
1101-
all_commands = []
1102-
by_version = {}
1103-
for python_version in TOOL_VERSIONS.keys():
1104-
by_version[python_version] = _commands_for_version(python_version)
1105-
all_commands.append(_commands_for_version(python_version))
1106-
1107-
template = """\
1108-
cat > "$@" <<'EOF'
1109-
#!/bin/bash
1110-
1111-
set -o errexit -o nounset -o pipefail
1112-
1113-
echo "Fetching hashes..."
1114-
1115-
{commands}
1116-
EOF
1117-
"""
1118-
1119-
native.genrule(
1120-
name = name,
1121-
srcs = [],
1122-
outs = ["print_toolchains_checksums.sh"],
1123-
cmd = select({
1124-
"//python/config_settings:is_python_{}".format(version): template.format(
1125-
commands = commands,
1126-
)
1127-
for version, commands in by_version.items()
1128-
} | {
1129-
"//conditions:default": template.format(commands = "\n".join(all_commands)),
1130-
}),
1131-
executable = True,
1132-
)
1133-
1134-
def _commands_for_version(python_version):
1135-
return "\n".join([
1136-
"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(
1137-
python_version = python_version,
1138-
platform = platform,
1139-
release_url = release_url,
1140-
release_url_sha256 = release_url + ".sha256",
1141-
)
1142-
for platform in TOOL_VERSIONS[python_version]["sha256"].keys()
1143-
for release_url in get_release_info(platform, python_version)[1]
1144-
])
1145-
11461098
def gen_python_config_settings(name = ""):
11471099
for platform in PLATFORMS.keys():
11481100
native.config_setting(

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