|
24 | 24 | import json
|
25 | 25 | import os
|
26 | 26 | import pathlib
|
| 27 | +import platform |
27 | 28 | import re
|
28 | 29 | import sys
|
29 | 30 | import subprocess
|
@@ -233,13 +234,48 @@ def extract_zip_with_permissions(zip_file_path, destination_dir):
|
233 | 234 | if entry.external_attr > 0xffff:
|
234 | 235 | os.chmod(file_path, entry.external_attr >> 16)
|
235 | 236 |
|
| 237 | +def make_symlinks_absolute(output_dir): |
| 238 | + """ |
| 239 | + Walks a directory and converts all relative symlinks to absolute ones. |
| 240 | + """ |
| 241 | + for root, dirs, files in os.walk(output_dir): |
| 242 | + for name in files + dirs: |
| 243 | + path = pathlib.Path(root) / name |
| 244 | + |
| 245 | + if not path.is_symlink(): |
| 246 | + continue |
| 247 | + |
| 248 | + target = path.readlink() |
| 249 | + |
| 250 | + if target.is_absolute(): |
| 251 | + continue |
| 252 | + |
| 253 | + # NOTE: |
| 254 | + # On Windows, we must know if the target is a directory to recreate |
| 255 | + # the symlink correctly. Check this *before* unlinking. |
| 256 | + # path.is_dir() correctly follows the symlink to check the target. |
| 257 | + is_dir = path.is_dir() |
| 258 | + |
| 259 | + # Resolve the relative target path to an absolute one |
| 260 | + absolute_target = (path.parent / target).resolve() |
| 261 | + |
| 262 | + # Replace the old relative symlink with the new absolute one |
| 263 | + path.unlink() |
| 264 | + path.symlink_to(absolute_target, target_is_directory=is_dir) |
| 265 | + |
236 | 266 | def unpack_archive(archive_file, output_dir):
|
237 | 267 | # Addressing https://github.com/bazelbuild/continuous-integration/issues/1536
|
238 | 268 | if archive_file.endswith(".zip"):
|
239 | 269 | extract_zip_with_permissions(archive_file, output_dir)
|
240 | 270 | else:
|
241 | 271 | shutil.unpack_archive(archive_file, output_dir)
|
242 | 272 |
|
| 273 | + if platform.system() == "Windows": |
| 274 | + # https://github.com/bazelbuild/bazel-central-registry/pull/5205 |
| 275 | + # Windows has issues with relative symlinks so let's make all of the |
| 276 | + # symlinks in the unpacked archive absolute. |
| 277 | + make_symlinks_absolute(output_dir) |
| 278 | + |
243 | 279 | def prepare_test_module_repo(module_name, module_version, overwrite_bazel_version=None, root=None, suppress_log=False):
|
244 | 280 | """Prepare the test module repo and the presubmit yml file it should use"""
|
245 | 281 | suppress_log or bazelci.print_collapsed_group(":information_source: Prepare test module repo")
|
|
0 commit comments