Skip to content

Commit a32790f

Browse files
feat: use bazel extract() for tar/zst unpack
1 parent bab5aaf commit a32790f

File tree

1 file changed

+4
-26
lines changed

1 file changed

+4
-26
lines changed

cloud_archive.bzl

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,11 @@ def validate_checksum(repo_ctx, url, local_path, expected_sha256):
2727

2828
def extract_archive(repo_ctx, local_path, strip_prefix, build_file, build_file_contents):
2929
bash_path = repo_ctx.os.environ.get("BAZEL_SH", "bash")
30-
if local_path.endswith(".tar.zst") or local_path.endswith(".tzst"):
31-
# Recent TAR supports zstd, if the compressor is installed.
32-
zst_path = repo_ctx.which("zstd")
33-
if zst_path == None:
34-
fail("To decompress .tar.zst, install zstd.")
35-
tar_path = repo_ctx.which("tar")
36-
if tar_path == None:
37-
fail("To decompress .tar.zst, install tar.")
38-
extra_tar_params = []
39-
if strip_prefix != None and strip_prefix:
40-
# Trick: we need to extract a subdir, and remove its components
41-
# from the path. We do so via `tar xvf file.tar.zst sub/dir
42-
# --strip-components=N`. Here we figure out the N.
43-
num_components = 0
44-
prefix = strip_prefix.strip("/")
45-
for c in prefix.split("/"):
46-
if len(c) > 0:
47-
num_components += 1
48-
extra_tar_params = [prefix, "--strip-components=" + str(num_components)]
4930

50-
# Decompress with tar, piping through zstd internally, and stripping prefix
51-
# if requested.
52-
tar_cmd = [tar_path, "-x", "-f", local_path] + extra_tar_params
53-
repo_ctx.execute(tar_cmd)
54-
else:
55-
# Extract the downloaded archive using Bazel's built-in decompressors.
56-
repo_ctx.extract(local_path, stripPrefix = strip_prefix)
31+
# Recent TAR supports zstd, if the compressor is installed.
32+
# Better, since bazel-5.0 via bazelbuild/bazel#15087 bazel's extract can handle zstd.
33+
# Extract the downloaded archive using Bazel's built-in decompressors.
34+
repo_ctx.extract(local_path, stripPrefix = strip_prefix)
5735

5836
# Provide external BUILD file if requested; `build_file_contents` takes
5937
# priority.

0 commit comments

Comments
 (0)