-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update overlay_directories.py #3158
Open
r0cketdyne
wants to merge
1
commit into
llvm:main
Choose a base branch
from
r0cketdyne:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,6 @@ | |
# 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 | ||
|
@@ -20,77 +18,77 @@ | |
import sys | ||
|
||
|
||
def _check_python_version(): | ||
if sys.version_info[0] < 3: | ||
raise RuntimeError( | ||
"Must be invoked with a python 3 interpreter but was %s" % | ||
sys.executable) | ||
def check_python_version(): | ||
"""Checks if the Python version is at least 3.""" | ||
if sys.version_info[0] < 3: | ||
raise RuntimeError( | ||
"Must be invoked with a python 3 interpreter but was %s" % | ||
sys.executable) | ||
|
||
|
||
def _check_dir_exists(path): | ||
if not os.path.isdir(path): | ||
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path) | ||
def check_dir_exists(path): | ||
"""Checks if the directory exists.""" | ||
if not os.path.isdir(path): | ||
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path) | ||
|
||
|
||
def parse_arguments(): | ||
parser = argparse.ArgumentParser(description=""" | ||
"""Parses command-line arguments.""" | ||
parser = argparse.ArgumentParser(description=""" | ||
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. | ||
""") | ||
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.") | ||
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() | ||
args = parser.parse_args() | ||
|
||
_check_dir_exists(args.target) | ||
_check_dir_exists(args.overlay) | ||
_check_dir_exists(args.src) | ||
check_dir_exists(args.target) | ||
check_dir_exists(args.overlay) | ||
check_dir_exists(args.src) | ||
|
||
return args | ||
return args | ||
|
||
|
||
def _symlink_abs(from_path, to_path): | ||
if not os.path.exists(to_path): | ||
os.symlink(os.path.abspath(from_path), os.path.abspath(to_path)) | ||
def symlink_abs(from_path, to_path): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
"""Creates an absolute symlink from 'from_path' to 'to_path'.""" | ||
if not os.path.exists(to_path): | ||
os.symlink(os.path.abspath(from_path), os.path.abspath(to_path)) | ||
|
||
|
||
def main(args): | ||
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)) | ||
"""Main function to overlay directories using symlinks.""" | ||
for root, dirs, files in os.walk(args.overlay): | ||
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)) | ||
Comment on lines
+74
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any changes other than indentation here? I tried squinting but couldn't find. |
||
|
||
|
||
if __name__ == "__main__": | ||
_check_python_version() | ||
main(parse_arguments()) | ||
check_python_version() | ||
main(parse_arguments()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd stick to what's used in llvm here, as it makes "syncing" with upstream changes easier.
https://sourcegraph.com/github.com/llvm/llvm-project@main/-/blob/utils/bazel/overlay_directories.py