|
| 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) |
0 commit comments