Skip to content
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

Support preserve_symlinks=False for directories #1820

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ def match_skip(self) -> Callable[[Path], bool]:

def _render_template(self) -> None:
"""Render the template in the subproject root."""
for src in scantree(str(self.template_copy_root)):
follow_symlinks = not self.template.preserve_symlinks
for src in scantree(str(self.template_copy_root), follow_symlinks):
src_abspath = Path(src.path)
src_relpath = Path(src_abspath).relative_to(self.template.local_abspath)
dst_relpath = self._render_path(
Expand All @@ -612,7 +613,7 @@ def _render_template(self) -> None:
continue
if src.is_symlink() and self.template.preserve_symlinks:
self._render_symlink(src_relpath, dst_relpath)
elif src.is_dir(follow_symlinks=False):
elif src.is_dir(follow_symlinks=follow_symlinks):
self._render_folder(dst_relpath)
else:
self._render_file(src_relpath, dst_relpath)
Expand Down
6 changes: 3 additions & 3 deletions copier/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def set_git_alternates(*repos: Path, path: Path = Path(".")) -> None:
alternates_file.write_bytes(b"\n".join(map(bytes, map(get_git_objects_dir, repos))))


def scantree(path: str) -> Iterator[os.DirEntry[str]]:
def scantree(path: str, follow_symlinks: bool) -> Iterator[os.DirEntry[str]]:
"""A recursive extension of `os.scandir`."""
for entry in os.scandir(path):
yield entry
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
if entry.is_dir(follow_symlinks=follow_symlinks):
yield from scantree(entry.path, follow_symlinks)
15 changes: 15 additions & 0 deletions tests/test_symlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,18 @@ def test_recursive_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
run_copy(str(src), dst, defaults=True, overwrite=True)
assert (dst / "one" / "two" / "three" / "root").is_symlink()
assert (dst / "one" / "two" / "three" / "root").readlink() == Path("../../../")


def test_symlinked_dir_expanded(tmp_path_factory: pytest.TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
src / "a_dir" / "a_file.txt": "some content",
src / "a_symlinked_dir": Path("a_dir"),
src / "a_nested" / "symlink": Path("../a_dir"),
}
)
run_copy(str(src), dst, defaults=True, overwrite=True)
assert (dst / "a_dir" / "a_file.txt").read_text() == "some content"
assert (dst / "a_symlinked_dir" / "a_file.txt").read_text() == "some content"
assert (dst / "a_nested" / "symlink" / "a_file.txt").read_text() == "some content"
Loading