2022-04-07 02:20:39 +08:00
|
|
|
#!/bin/python3
|
|
|
|
|
|
|
|
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
|
|
|
# This is copied from llvm-project's utils/bazel/overlay_directories.py
|
|
|
|
|
|
|
|
"""Overlays two directories into a target directory using symlinks.
|
|
|
|
|
|
|
|
Tries to minimize the number of symlinks created (that is, does not symlink
|
|
|
|
every single file). Symlinks every file in the overlay directory. Only symlinks
|
|
|
|
individual files in the source directory if their parent directory is also
|
|
|
|
contained in the overlay directory tree.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def _check_python_version():
|
2024-04-28 05:16:31 +08:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Must be invoked with a python 3 interpreter but was %s" % sys.executable
|
|
|
|
)
|
2022-04-07 02:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _check_dir_exists(path):
|
2024-04-28 05:16:31 +08:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path)
|
2022-04-07 02:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments():
|
2024-04-28 05:16:31 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="""
|
2022-04-07 02:20:39 +08:00
|
|
|
Overlays two directories into a target directory using symlinks.
|
|
|
|
|
|
|
|
Tries to minimize the number of symlinks created (that is, does not symlink
|
|
|
|
every single file). Symlinks every file in the overlay directory. Only
|
|
|
|
symlinks individual files in the source directory if their parent directory
|
|
|
|
is also contained in the overlay directory tree.
|
2024-04-28 05:16:31 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--src",
|
|
|
|
required=True,
|
|
|
|
help="Directory that contains most of the content to symlink.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--overlay",
|
|
|
|
required=True,
|
|
|
|
help="Directory to overlay on top of the source directory.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--target",
|
|
|
|
required=True,
|
|
|
|
help="Directory in which to place the fused symlink directories.",
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
_check_dir_exists(args.target)
|
|
|
|
_check_dir_exists(args.overlay)
|
|
|
|
_check_dir_exists(args.src)
|
|
|
|
|
|
|
|
return args
|
2022-04-07 02:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _symlink_abs(from_path, to_path):
|
2024-04-28 05:16:31 +08:00
|
|
|
if not os.path.exists(to_path):
|
|
|
|
os.symlink(os.path.abspath(from_path), os.path.abspath(to_path))
|
2022-04-07 02:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
2024-04-28 05:16:31 +08:00
|
|
|
for root, dirs, files in os.walk(args.overlay):
|
|
|
|
# We could do something more intelligent here and only symlink individual
|
|
|
|
# files if the directory is present in both overlay and src. This could also
|
|
|
|
# be generalized to an arbitrary number of directories without any
|
|
|
|
# "src/overlay" distinction. In the current use case we only have two and
|
|
|
|
# the overlay directory is always small, so putting that off for now.
|
|
|
|
rel_root = os.path.relpath(root, start=args.overlay)
|
|
|
|
if rel_root != ".":
|
|
|
|
os.mkdir(os.path.join(args.target, rel_root))
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
relpath = os.path.join(rel_root, file)
|
|
|
|
_symlink_abs(
|
|
|
|
os.path.join(args.overlay, relpath), os.path.join(args.target, relpath)
|
|
|
|
)
|
|
|
|
|
|
|
|
for src_entry in os.listdir(os.path.join(args.src, rel_root)):
|
|
|
|
if src_entry not in dirs:
|
|
|
|
relpath = os.path.join(rel_root, src_entry)
|
|
|
|
_symlink_abs(
|
|
|
|
os.path.join(args.src, relpath), os.path.join(args.target, relpath)
|
|
|
|
)
|
2022-04-07 02:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-04-28 05:16:31 +08:00
|
|
|
_check_python_version()
|
|
|
|
main(parse_arguments())
|