Skip to content

Commit

Permalink
Add test_prefix_files (#5260)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Apr 18, 2024
1 parent 683ea91 commit e35daf4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
28 changes: 15 additions & 13 deletions conda_build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,22 +1784,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))

0 comments on commit e35daf4

Please sign in to comment.