Skip to content

Commit a92fea4

Browse files
committed
refactor(toolchains): better sha256 printing helper
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.
1 parent be55942 commit a92fea4

File tree

3 files changed

+88
-52
lines changed

3 files changed

+88
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
7+
def print_toolchains_checksums(name):
8+
"""A macro to print checksums for a particular Python interpreter version.
9+
10+
Args:
11+
name: {type}`str`: the name of the runnable target.
12+
"""
13+
all_commands = []
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+
all_commands.append(by_version[python_version])
22+
23+
template = """\
24+
cat > "$@" <<'EOF'
25+
#!/bin/bash
26+
27+
set -o errexit -o nounset -o pipefail
28+
29+
echo "Fetching hashes..."
30+
31+
{commands}
32+
EOF
33+
"""
34+
35+
native.genrule(
36+
name = name,
37+
srcs = [],
38+
outs = ["print_toolchains_checksums.sh"],
39+
cmd = select({
40+
"//python/config_settings:is_python_{}".format(version): template.format(
41+
commands = commands,
42+
)
43+
for version, commands in by_version.items()
44+
} | {
45+
"//conditions:default": template.format(commands = "\n".join(all_commands)),
46+
}),
47+
executable = True,
48+
)
49+
50+
def _commands_for_version(*, python_version, metadata):
51+
lines = []
52+
lines += [
53+
"cat <<EOB", # end of block
54+
" \"{python_version}\": {{".format(python_version = python_version),
55+
" \"url\": \"{url}\",".format(url = metadata["url"]),
56+
" \"sha256\": {",
57+
]
58+
59+
for platform in metadata["sha256"].keys():
60+
for release_url in get_release_info(platform, python_version)[1]:
61+
# Do lines one by one so that the progress is seen better and use cat for ease of quotation
62+
lines += [
63+
"EOB",
64+
"cat <<EOB",
65+
" \"{platform}\": \"$$({get_sha256})\",".format(
66+
platform = platform,
67+
get_sha256 = "curl --location --fail {release_url_sha256} 2>/dev/null || curl --location --fail {release_url} 2>/dev/null | shasum -a 256 | awk '{{ print $$1 }}'".format(
68+
release_url = release_url,
69+
release_url_sha256 = release_url + ".sha256",
70+
),
71+
),
72+
]
73+
74+
prefix = metadata["strip_prefix"]
75+
prefix = render.indent(
76+
render.dict(prefix) if type(prefix) == type({}) else repr(prefix),
77+
indent = " " * 8,
78+
).lstrip()
79+
80+
lines += [
81+
" },",
82+
" \"strip_prefix\": {strip_prefix},".format(strip_prefix = prefix),
83+
" },",
84+
"EOB",
85+
]
86+
87+
return "\n".join(lines)

python/versions.bzl

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,57 +1073,6 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
10731073

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

1076-
def print_toolchains_checksums(name):
1077-
"""A macro to print checksums for a particular Python interpreter version.
1078-
1079-
Args:
1080-
name: {type}`str`: the name of the runnable target.
1081-
"""
1082-
all_commands = []
1083-
by_version = {}
1084-
for python_version in TOOL_VERSIONS.keys():
1085-
by_version[python_version] = _commands_for_version(python_version)
1086-
all_commands.append(_commands_for_version(python_version))
1087-
1088-
template = """\
1089-
cat > "$@" <<'EOF'
1090-
#!/bin/bash
1091-
1092-
set -o errexit -o nounset -o pipefail
1093-
1094-
echo "Fetching hashes..."
1095-
1096-
{commands}
1097-
EOF
1098-
"""
1099-
1100-
native.genrule(
1101-
name = name,
1102-
srcs = [],
1103-
outs = ["print_toolchains_checksums.sh"],
1104-
cmd = select({
1105-
"//python/config_settings:is_python_{}".format(version): template.format(
1106-
commands = commands,
1107-
)
1108-
for version, commands in by_version.items()
1109-
} | {
1110-
"//conditions:default": template.format(commands = "\n".join(all_commands)),
1111-
}),
1112-
executable = True,
1113-
)
1114-
1115-
def _commands_for_version(python_version):
1116-
return "\n".join([
1117-
"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(
1118-
python_version = python_version,
1119-
platform = platform,
1120-
release_url = release_url,
1121-
release_url_sha256 = release_url + ".sha256",
1122-
)
1123-
for platform in TOOL_VERSIONS[python_version]["sha256"].keys()
1124-
for release_url in get_release_info(platform, python_version)[1]
1125-
])
1126-
11271076
def gen_python_config_settings(name = ""):
11281077
for platform in PLATFORMS.keys():
11291078
native.config_setting(

0 commit comments

Comments
 (0)