Coverage for cuda / pathfinder / _utils / find_sub_dirs.py: 95.45%
44 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
4import functools
5import os
6import site
7import sys
8from collections.abc import Sequence
11def find_sub_dirs_no_cache(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
12 results = [] 1ahijklmnopqrstuvwxyzABCDEFGHeMLgIbcdJKf
13 for base in parent_dirs: 1ahijklmnopqrstuvwxyzABCDEFGHeMLgIbcdJKf
14 stack = [(base, 0)] # (current_path, index into sub_dirs) 1ahijklmnopqrstuvwxyzABCDEFGHeLgIbcdJKf
15 while stack: 1ahijklmnopqrstuvwxyzABCDEFGHeLgIbcdJKf
16 current_path, idx = stack.pop() 1ahijklmnopqrstuvwxyzABCDEFGHeLgIbcdJKf
17 if idx == len(sub_dirs): 1ahijklmnopqrstuvwxyzABCDEFGHeLgIbcdJKf
18 if os.path.isdir(current_path): 1eLgbcdf
19 results.append(current_path) 1eLgbcdf
20 continue 1eLgbcdf
22 sub = sub_dirs[idx] 1ahijklmnopqrstuvwxyzABCDEFGHegIbcdJKf
23 if sub == "*": 1ahijklmnopqrstuvwxyzABCDEFGHegIbcdJKf
24 try: 1ef
25 entries = sorted(os.listdir(current_path)) 1ef
26 except OSError:
27 continue
28 for entry in entries: 1ef
29 entry_path = os.path.join(current_path, entry) 1ef
30 if os.path.isdir(entry_path): 1ef
31 stack.append((entry_path, idx + 1)) 1ef
32 else:
33 next_path = os.path.join(current_path, sub) 1ahijklmnopqrstuvwxyzABCDEFGHegIbcdJKf
34 if os.path.isdir(next_path): 1ahijklmnopqrstuvwxyzABCDEFGHegIbcdJKf
35 stack.append((next_path, idx + 1)) 1egbcdf
36 return results 1ahijklmnopqrstuvwxyzABCDEFGHeMLgIbcdJKf
39@functools.cache
40def find_sub_dirs_cached(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
41 return find_sub_dirs_no_cache(parent_dirs, sub_dirs) 1ahijklmnopqrstuvwxyzABCDEFGHeMLgIbcdJKf
44def find_sub_dirs(parent_dirs: Sequence[str], sub_dirs: Sequence[str]) -> list[str]:
45 return find_sub_dirs_cached(tuple(parent_dirs), tuple(sub_dirs)) 1ahiNOPQRjklSmnTUVWXopqrstuvwxyzABCDEFGHeMLgIbcdJKf
48def find_sub_dirs_sys_path(sub_dirs: Sequence[str]) -> list[str]:
49 return find_sub_dirs(sys.path, sub_dirs) 1J
52def find_sub_dirs_all_sitepackages(sub_dirs: Sequence[str]) -> list[str]:
53 parent_dirs = list(site.getsitepackages()) 1ahiNOPQRjklSmnTUVWXopqrstuvwxyzABCDEFGHIbcd
54 if site.ENABLE_USER_SITE: 1ahiNOPQRjklSmnTUVWXopqrstuvwxyzABCDEFGHIbcd
55 user_site = site.getusersitepackages() 1bcd
56 if user_site: 1bcd
57 # Determine insertion index based on whether we're in a virtual environment (fixes #1716):
58 # - In venv (PEP 405): venv site-packages should come first, then user-site-packages,
59 # then system site-packages. Insert at index 1 (after venv, before system).
60 # - Not in venv (PEP 370): user-site-packages should come before system site-packages.
61 # Insert at index 0.
62 # Detect venv by checking if sys.prefix differs from sys.base_prefix
63 insert_idx = 1 if sys.prefix != sys.base_prefix else 0 1bcd
64 parent_dirs.insert(insert_idx, user_site) 1bcd
65 return find_sub_dirs(parent_dirs, sub_dirs) 1ahiNOPQRjklSmnTUVWXopqrstuvwxyzABCDEFGHIbcd