Skip to content

Commit 9712d5d

Browse files
committed
bazel 7 rename helper
1 parent 90931ce commit 9712d5d

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

python/private/pypi/whl_extract.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def whl_extract(rctx, *, whl_path, logger):
7272

7373
for (src, dest) in merge_trees(src, rctx.path(dest_prefix)):
7474
logger.debug(lambda: "Renaming: {} -> {}".format(src, dest))
75-
rctx.rename(src, dest)
75+
repo_utils.rename(rctx, src, dest)
7676

7777
# TODO @aignas 2025-12-16: when moving scripts to `bin`, rewrite the #!python
7878
# shebang to be something else, for inspiration look at the hermetic

python/private/repo_utils.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,31 @@ def _extract(mrctx, *, archive, supports_whl_extraction = False, **kwargs):
522522
if not mrctx.delete(archive):
523523
fail("Failed to remove the symlink after extracting")
524524

525+
def _rename(mrctx, src, dest):
526+
"""Rename a file or directory.
527+
528+
TODO: remove when the earliest supported bazel version is at least 8.0.
529+
530+
Args:
531+
mrctx: The repository context.
532+
src: The source path.
533+
dest: The destination path.
534+
"""
535+
if hasattr(mrctx, "rename"):
536+
mrctx.rename(src, dest)
537+
return
538+
539+
# Fallback for Bazel versions that don't have mrctx.rename.
540+
os_name = _get_platforms_os_name(mrctx)
541+
if os_name == "windows":
542+
# On Windows, use 'cmd.exe /c move'
543+
result = mrctx.execute(["cmd.exe", "/C", "move", "/Y", str(src), str(dest)])
544+
else:
545+
result = mrctx.execute(["mv", str(src), str(dest)])
546+
547+
if result.return_code != 0:
548+
fail("Failed to rename {} to {}: {}".format(src, dest, result.stderr))
549+
525550
repo_utils = struct(
526551
# keep sorted
527552
execute_checked = _execute_checked,
@@ -535,6 +560,7 @@ repo_utils = struct(
535560
mkdir = _mkdir,
536561
norm_path = _norm_path,
537562
relative_to = _relative_to,
563+
rename = _rename,
538564
is_relative_to = _is_relative_to,
539565
repo_root_relative_path = _repo_root_relative_path,
540566
which_checked = _which_checked,

0 commit comments

Comments
 (0)