Skip to content

Commit 4558ffb

Browse files
authored
refactor(repo_utils): create a helper for extracting files (#3459)
This just moves common code to the same place, so that we can better maintain extracting from the whls easier. Work towards #2945
1 parent e05cf00 commit 4558ffb

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ bzl_library(
247247
deps = [
248248
":parse_whl_name_bzl",
249249
"//python/private:repo_utils_bzl",
250+
"@rules_python_internal//:rules_python_config_bzl",
250251
],
251252
)
252253

python/private/pypi/patch_whl.bzl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ other patches ensures that the users have overview on exactly what has changed
2727
within the wheel.
2828
"""
2929

30+
load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
31+
load("//python/private:repo_utils.bzl", "repo_utils")
3032
load(":parse_whl_name.bzl", "parse_whl_name")
3133
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
3234

@@ -84,16 +86,11 @@ def patch_whl(rctx, *, python_interpreter, whl_path, patches, **kwargs):
8486
# does not support patching in another directory.
8587
whl_input = rctx.path(whl_path)
8688

87-
# symlink to a zip file to use bazel's extract so that we can use bazel's
88-
# repository_ctx patch implementation. The whl file may be in a different
89-
# external repository.
90-
#
91-
# TODO @aignas 2025-11-24: remove this symlinking workaround when we drop support for bazel 7
92-
whl_file_zip = whl_input.basename + ".zip"
93-
rctx.symlink(whl_input, whl_file_zip)
94-
rctx.extract(whl_file_zip)
95-
if not rctx.delete(whl_file_zip):
96-
fail("Failed to remove the symlink after extracting")
89+
repo_utils.extract(
90+
rctx,
91+
archive = whl_input,
92+
supports_whl_extraction = rp_config.supports_whl_extraction,
93+
)
9794

9895
if not patches:
9996
fail("Trying to patch wheel without any patches")

python/private/pypi/whl_library.bzl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,12 @@ def _whl_library_impl(rctx):
377377
#
378378
# Remove non-pipstar and config_load check when we release rules_python 2.
379379
if enable_pipstar:
380-
if rp_config.supports_whl_extraction:
381-
extract_path = whl_path
382-
else:
383-
extract_path = rctx.path(whl_path.basename + ".zip")
384-
rctx.symlink(whl_path, extract_path)
385-
rctx.extract(
386-
archive = extract_path,
380+
repo_utils.extract(
381+
rctx,
382+
archive = whl_path,
387383
output = "site-packages",
384+
supports_whl_extraction = rp_config.supports_whl_extraction,
388385
)
389-
if not rp_config.supports_whl_extraction:
390-
rctx.delete(extract_path)
391386

392387
metadata = whl_metadata(
393388
install_dir = whl_path.dirname.get_child("site-packages"),

python/private/repo_utils.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,34 @@ def _get_platforms_cpu_name(mrctx):
429429
return "riscv64"
430430
return arch
431431

432+
def _extract(mrctx, *, archive, supports_whl_extraction = False, **kwargs):
433+
"""Extract an archive
434+
435+
TODO: remove when the earliest supported bazel version is at least 8.3.
436+
437+
Note, we are using the parameter here because there is very little ways how we can detect
438+
whether we can support just extracting the whl.
439+
"""
440+
archive_original = None
441+
if not supports_whl_extraction and archive.basename.endswith(".whl"):
442+
archive_original = archive
443+
archive = mrctx.path(archive.basename + ".zip")
444+
mrctx.symlink(archive_original, archive)
445+
446+
mrctx.extract(
447+
archive = archive,
448+
**kwargs
449+
)
450+
if archive_original:
451+
if not mrctx.delete(archive):
452+
fail("Failed to remove the symlink after extracting")
453+
432454
repo_utils = struct(
433455
# keep sorted
434456
execute_checked = _execute_checked,
435457
execute_checked_stdout = _execute_checked_stdout,
436458
execute_unchecked = _execute_unchecked,
459+
extract = _extract,
437460
get_platforms_cpu_name = _get_platforms_cpu_name,
438461
get_platforms_os_name = _get_platforms_os_name,
439462
getenv = _getenv,

0 commit comments

Comments
 (0)