Skip to content

Commit ee2f98d

Browse files
committed
refactor(toolchain): use bazel to extract zstd archives
Here we remove the dependence on the `zstd` archive downloaded from GH for extracting `zstd` toolchains because bazel now supports `zstd` extraction for a long time. This mainly cleans up the code which is again used a lot more due to bazel-contrib#2386.
1 parent c8ccd22 commit ee2f98d

File tree

4 files changed

+36
-56
lines changed

4 files changed

+36
-56
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Unreleased changes template.
5252

5353
{#v0-0-0-changed}
5454
### Changed
55-
* Nothing yet.
55+
* (python_repository) Start honouring the `strip_prefix` field for `zstd` archives.
5656

5757
{#v0-0-0-fixed}
5858
### Fixed
@@ -66,6 +66,11 @@ Unreleased changes template.
6666
### Removed
6767
* (publish) Remove deprecated `requirements.txt` for the `twine` dependencies.
6868
Please use `requirements_linux.txt` instead.
69+
* (python_repository) Use bazel's built in `zstd` support and remove attributes
70+
for customizing the `zstd` binary to be used for `zstd` archives in the
71+
{bzl:obj}`python_repository` repository_rule. This affects the
72+
{bzl:obj}`python_register_toolchains` and
73+
{bzl:obj}`python_register_multi_toolchains` callers in the `WORKSPACE`.
6974

7075
{#v0-39-0}
7176
## [0.39.0] - 2024-11-13

python/private/python.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
462462
"strip_prefix": {
463463
platform: item["strip_prefix"]
464464
for platform in item["sha256"]
465-
},
465+
} if type(item["strip_prefix"]) == type("") else item["strip_prefix"],
466466
"url": {
467467
platform: [item["url"]]
468468
for platform in item["sha256"]

python/private/python_repository.bzl

+10-51
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""This file contains repository rules and macros to support toolchain registration.
1616
"""
1717

18-
load("//python:versions.bzl", "FREETHREADED", "PLATFORMS")
18+
load("//python:versions.bzl", "FREETHREADED", "INSTALL_ONLY", "PLATFORMS")
1919
load(":auth.bzl", "get_auth")
2020
load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
2121
load(":text_util.bzl", "render")
@@ -72,51 +72,13 @@ def _python_repository_impl(rctx):
7272
urls = rctx.attr.urls or [rctx.attr.url]
7373
auth = get_auth(rctx, urls)
7474

75-
if release_filename.endswith(".zst"):
76-
rctx.download(
75+
if INSTALL_ONLY in release_filename:
76+
rctx.download_and_extract(
7777
url = urls,
7878
sha256 = rctx.attr.sha256,
79-
output = release_filename,
79+
stripPrefix = rctx.attr.strip_prefix,
8080
auth = auth,
8181
)
82-
unzstd = rctx.which("unzstd")
83-
if not unzstd:
84-
url = rctx.attr.zstd_url.format(version = rctx.attr.zstd_version)
85-
rctx.download_and_extract(
86-
url = url,
87-
sha256 = rctx.attr.zstd_sha256,
88-
auth = auth,
89-
)
90-
working_directory = "zstd-{version}".format(version = rctx.attr.zstd_version)
91-
92-
repo_utils.execute_checked(
93-
rctx,
94-
op = "python_repository.MakeZstd",
95-
arguments = [
96-
repo_utils.which_checked(rctx, "make"),
97-
"--jobs=4",
98-
],
99-
timeout = 600,
100-
quiet = True,
101-
working_directory = working_directory,
102-
logger = logger,
103-
)
104-
zstd = "{working_directory}/zstd".format(working_directory = working_directory)
105-
unzstd = "./unzstd"
106-
rctx.symlink(zstd, unzstd)
107-
108-
repo_utils.execute_checked(
109-
rctx,
110-
op = "python_repository.ExtractRuntime",
111-
arguments = [
112-
repo_utils.which_checked(rctx, "tar"),
113-
"--extract",
114-
"--strip-components=2",
115-
"--use-compress-program={unzstd}".format(unzstd = unzstd),
116-
"--file={}".format(release_filename),
117-
],
118-
logger = logger,
119-
)
12082
else:
12183
rctx.download_and_extract(
12284
url = urls,
@@ -125,6 +87,12 @@ def _python_repository_impl(rctx):
12587
auth = auth,
12688
)
12789

90+
# Strip the things that are not present in the INSTALL_ONLY builds
91+
# NOTE: if the dirs are not present, we will not fail here
92+
rctx.delete("python/build")
93+
rctx.delete("python/licenses")
94+
rctx.delete("python/PYTHON.json")
95+
12896
patches = rctx.attr.patches
12997
if patches:
13098
for patch in patches:
@@ -378,15 +346,6 @@ function defaults (e.g. `single_version_override` for `MODULE.bazel` files.
378346
"urls": attr.string_list(
379347
doc = "The URL of the interpreter to download. Exactly one of url and urls must be set.",
380348
),
381-
"zstd_sha256": attr.string(
382-
default = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0",
383-
),
384-
"zstd_url": attr.string(
385-
default = "https://github.com/facebook/zstd/releases/download/v{version}/zstd-{version}.tar.gz",
386-
),
387-
"zstd_version": attr.string(
388-
default = "1.5.2",
389-
),
390349
"_rule_name": attr.string(default = "python_repository"),
391350
},
392351
environ = [REPO_DEBUG_ENV_VAR],

python/versions.bzl

+19-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ MACOS_NAME = "mac os"
2020
LINUX_NAME = "linux"
2121
WINDOWS_NAME = "windows"
2222
FREETHREADED = "freethreaded"
23+
INSTALL_ONLY = "install_only"
2324

2425
DEFAULT_RELEASE_BASE_URL = "https://github.com/indygreg/python-build-standalone/releases/download"
2526

@@ -52,7 +53,7 @@ TOOL_VERSIONS = {
5253
"x86_64-apple-darwin": "8d06bec08db8cdd0f64f4f05ee892cf2fcbc58cfb1dd69da2caab78fac420238",
5354
"x86_64-unknown-linux-gnu": "aec8c4c53373b90be7e2131093caa26063be6d9d826f599c935c0e1042af3355",
5455
},
55-
"strip_prefix": "python",
56+
"strip_prefix": "python/install",
5657
},
5758
"3.8.12": {
5859
"url": "20220227/cpython-{python_version}+20220227-{platform}-{build}.tar.gz",
@@ -579,7 +580,22 @@ TOOL_VERSIONS = {
579580
"x86_64-pc-windows-msvc-freethreaded": "bfd89f9acf866463bc4baf01733da5e767d13f5d0112175a4f57ba91f1541310",
580581
"x86_64-unknown-linux-gnu-freethreaded": "a73adeda301ad843cce05f31a2d3e76222b656984535a7b87696a24a098b216c",
581582
},
582-
"strip_prefix": "python",
583+
"strip_prefix": {
584+
"aarch64-apple-darwin": "python",
585+
"aarch64-unknown-linux-gnu": "python",
586+
"ppc64le-unknown-linux-gnu": "python",
587+
"s390x-unknown-linux-gnu": "python",
588+
"x86_64-apple-darwin": "python",
589+
"x86_64-pc-windows-msvc": "python",
590+
"x86_64-unknown-linux-gnu": "python",
591+
"aarch64-apple-darwin-freethreaded": "python/install",
592+
"aarch64-unknown-linux-gnu-freethreaded": "python/install",
593+
"ppc64le-unknown-linux-gnu-freethreaded": "python/install",
594+
"s390x-unknown-linux-gnu-freethreaded": "python/install",
595+
"x86_64-apple-darwin-freethreaded": "python/install",
596+
"x86_64-pc-windows-msvc-freethreaded": "python/install",
597+
"x86_64-unknown-linux-gnu-freethreaded": "python/install",
598+
},
583599
},
584600
}
585601

@@ -777,7 +793,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
777793
}[p],
778794
)
779795
else:
780-
build = "install_only"
796+
build = INSTALL_ONLY
781797

782798
if WINDOWS_NAME in platform:
783799
build = "shared-" + build

0 commit comments

Comments
 (0)