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

Add test_prefix_files #5260

Merged
merged 3 commits into from
Apr 18, 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
28 changes: 15 additions & 13 deletions conda_build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,22 +1780,24 @@ def merge_dicts_of_lists(
return {k: dol1.get(k, no) + dol2.get(k, no) for k in keys}


def prefix_files(prefix):
def prefix_files(prefix: str | os.PathLike | Path) -> set[str]:
"""
Returns a set of all files in prefix.
"""
res = set()
prefix_rep = prefix + os.path.sep
for root, dirs, files in walk(prefix):
for fn in files:
# this is relpath, just hacked to be faster
res.add(join(root, fn).replace(prefix_rep, "", 1))
for dn in dirs:
path = join(root, dn)
if islink(path):
res.add(path.replace(prefix_rep, "", 1))
res.update(expand_globs((path,), prefix))
return res
prefix = f"{os.path.abspath(prefix)}{os.path.sep}"
prefix_files: set[str] = set()
for root, directories, files in walk(prefix):
# this is effectively os.path.relpath, just hacked to be faster
relroot = root[len(prefix) :].lstrip(os.path.sep)
# add all files
prefix_files.update(join(relroot, file) for file in files)
# add all symlink directories (they are "files")
prefix_files.update(
join(relroot, directory)
for directory in directories
if islink(join(root, directory))
)
return prefix_files


def mmap_mmap(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,25 @@ def test_is_conda_pkg(tmpdir, value: str, expected: bool, is_dir: bool, create:
fp.write("test")

assert utils.is_conda_pkg(value) == expected


def test_prefix_files(tmp_path: Path):
# all files within the prefix are found
(prefix := tmp_path / "prefix1").mkdir()
(file1 := prefix / "file1").touch()
(dirA := prefix / "dirA").mkdir()
(file2 := dirA / "file2").touch()
(dirB := prefix / "dirB").mkdir()
(file3 := dirB / "file3").touch()

# files outside of the prefix are not found
(prefix2 := tmp_path / "prefix2").mkdir()
(prefix2 / "file4").touch()
(dirC := prefix2 / "dirC").mkdir()
(dirC / "file5").touch()

# even if they are symlinked
(link1 := prefix / "dirC").symlink_to(dirC)

paths = {str(path.relative_to(prefix)) for path in (file1, file2, file3, link1)}
assert paths == utils.prefix_files(str(prefix))
Loading