Skip to content
Open
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
32 changes: 3 additions & 29 deletions cuda_pathfinder/cuda/pathfinder/_utils/find_sub_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,11 @@
from collections.abc import Sequence


def find_sub_dirs_no_cache(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
results = []
for base in parent_dirs:
stack = [(base, 0)] # (current_path, index into sub_dirs)
while stack:
current_path, idx = stack.pop()
if idx == len(sub_dirs):
if os.path.isdir(current_path):
results.append(current_path)
continue

sub = sub_dirs[idx]
if sub == "*":
try:
entries = sorted(os.listdir(current_path))
except OSError:
continue
for entry in entries:
entry_path = os.path.join(current_path, entry)
if os.path.isdir(entry_path):
stack.append((entry_path, idx + 1))
else:
next_path = os.path.join(current_path, sub)
if os.path.isdir(next_path):
stack.append((next_path, idx + 1))
return results


@functools.cache
def find_sub_dirs_cached(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
return find_sub_dirs_no_cache(parent_dirs, sub_dirs)
"""Return ``parent_dir/<sub_dirs>`` for each parent directory that has it, in parent order."""
candidates = (os.path.join(parent_dir, *sub_dirs) for parent_dir in parent_dirs)
return [path for path in candidates if os.path.isdir(path)]


def find_sub_dirs(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
Expand Down
23 changes: 5 additions & 18 deletions cuda_pathfinder/tests/test_utils_find_sub_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,10 @@ def test_exact_match(test_tree):
assert result == expected


def test_single_wildcard(test_tree):
parent_paths = test_tree["parent_paths"]
base = test_tree["base"]
result = find_sub_dirs(parent_paths, ("nvidia", "*", "lib"))
expected = [
str(base / "sys1" / "nvidia" / "bar" / "lib"),
str(base / "sys1" / "nvidia" / "foo" / "lib"),
]
assert sorted(result) == sorted(expected)


def test_double_wildcard(test_tree):
parent_paths = test_tree["parent_paths"]
base = test_tree["base"]
result = find_sub_dirs(parent_paths, ("nvidia", "*", "nvvm", "lib64"))
expected = [str(base / "sys2" / "nvidia" / "baz" / "nvvm" / "lib64")]
assert result == expected
@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_sub_dir_parts_are_literal_names(test_tree):
"""No part is a pattern: ``*`` matches only a directory that is really named ``*``."""
assert find_sub_dirs(test_tree["parent_paths"], ("nvidia", "*", "lib")) == []


def test_no_match(test_tree):
Expand All @@ -69,7 +56,7 @@ def test_no_match(test_tree):


def test_empty_parent_paths():
result = find_sub_dirs((), ("nvidia", "*", "lib"))
result = find_sub_dirs((), ("nvidia", "foo", "lib"))
assert result == []


Expand Down
Loading