Skip to content

Commit 001a6a2

Browse files
committed
first round of Cursor refactoring (about 4 iterations until all tests passed), followed by ruff auto-fixes
1 parent bc0137a commit 001a6a2

File tree

5 files changed

+364
-138
lines changed

5 files changed

+364
-138
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2024-2025 NVIDIA Corporation. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4+
5+
import os
6+
from typing import List, Optional
7+
8+
from .cuda_paths import get_cuda_paths
9+
from .sys_path_find_sub_dirs import sys_path_find_sub_dirs
10+
11+
12+
def no_such_file_in_sub_dirs(
13+
sub_dirs: tuple[str, ...], file_wild: str, error_messages: List[str], attachments: List[str]
14+
) -> None:
15+
"""Report that a file was not found in the given subdirectories.
16+
17+
Args:
18+
sub_dirs: Tuple of subdirectory names to search
19+
file_wild: The file pattern to search for
20+
error_messages: List to append error messages to
21+
attachments: List to append directory listings to
22+
"""
23+
error_messages.append(f"No such file: {file_wild}")
24+
for sub_dir in sys_path_find_sub_dirs(sub_dirs):
25+
attachments.append(f' listdir("{sub_dir}"):')
26+
for node in sorted(os.listdir(sub_dir)):
27+
attachments.append(f" {node}")
28+
29+
30+
def get_cuda_paths_info(key: str, error_messages: List[str]) -> Optional[str]:
31+
"""Get information from cuda_paths for a given key.
32+
33+
Args:
34+
key: The key to look up in cuda_paths
35+
error_messages: List to append error messages to
36+
37+
Returns:
38+
The path info if found, None otherwise
39+
"""
40+
env_path_tuple = get_cuda_paths()[key]
41+
if not env_path_tuple:
42+
error_messages.append(f'Failure obtaining get_cuda_paths()["{key}"]')
43+
return None
44+
if not env_path_tuple.info:
45+
error_messages.append(f'Failure obtaining get_cuda_paths()["{key}"].info')
46+
return None
47+
return env_path_tuple.info
48+
49+
50+
class FindResult:
51+
"""Result of a library search operation.
52+
53+
Attributes:
54+
abs_path: The absolute path to the found library, or None if not found
55+
error_messages: List of error messages encountered during the search
56+
attachments: List of additional information (e.g. directory listings)
57+
lib_searched_for: The library name that was searched for
58+
"""
59+
60+
def __init__(self, lib_searched_for: str):
61+
self.abs_path: Optional[str] = None
62+
self.error_messages: List[str] = []
63+
self.attachments: List[str] = []
64+
self.lib_searched_for = lib_searched_for
65+
66+
def raise_if_abs_path_is_None(self) -> str:
67+
"""Raise an error if no library was found.
68+
69+
Returns:
70+
The absolute path to the found library
71+
72+
Raises:
73+
RuntimeError: If no library was found
74+
"""
75+
if self.abs_path:
76+
return self.abs_path
77+
err = ", ".join(self.error_messages)
78+
att = "\n".join(self.attachments)
79+
raise RuntimeError(f'Failure finding "{self.lib_searched_for}": {err}\n{att}')
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2024-2025 NVIDIA Corporation. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4+
5+
import os
6+
7+
from .find_dl_common import FindResult, get_cuda_paths_info, no_such_file_in_sub_dirs
8+
from .sys_path_find_sub_dirs import sys_path_find_sub_dirs
9+
10+
11+
def find_so_using_nvidia_lib_dirs(lib_searched_for: str) -> FindResult:
12+
"""Find a .so file using NVIDIA library directories.
13+
14+
Args:
15+
lib_searched_for: The library name to search for
16+
17+
Returns:
18+
FindResult containing the search results
19+
"""
20+
result = FindResult(lib_searched_for)
21+
file_wild = f"lib{lib_searched_for}.so*"
22+
sub_dirs = ("lib", "lib64")
23+
24+
for sub_dir in sys_path_find_sub_dirs(sub_dirs):
25+
for node in sorted(os.listdir(sub_dir)):
26+
if node.startswith(f"lib{lib_searched_for}.so"):
27+
result.abs_path = os.path.join(sub_dir, node)
28+
return result
29+
30+
no_such_file_in_sub_dirs(sub_dirs, file_wild, result.error_messages, result.attachments)
31+
return result
32+
33+
34+
def find_so_using_cudalib_dir(lib_searched_for: str) -> FindResult:
35+
"""Find a .so file using the CUDA library directory.
36+
37+
Args:
38+
lib_searched_for: The library name to search for
39+
40+
Returns:
41+
FindResult containing the search results
42+
"""
43+
result = FindResult(lib_searched_for)
44+
cudalib_dir = get_cuda_paths_info("cudalib_dir", result.error_messages)
45+
if not cudalib_dir:
46+
return result
47+
48+
file_wild = f"lib{lib_searched_for}.so*"
49+
for node in sorted(os.listdir(cudalib_dir)):
50+
if node.startswith(f"lib{lib_searched_for}.so"):
51+
result.abs_path = os.path.join(cudalib_dir, node)
52+
return result
53+
54+
result.error_messages.append(f"No such file: {file_wild}")
55+
result.attachments.append(f' listdir("{cudalib_dir}"):')
56+
for node in sorted(os.listdir(cudalib_dir)):
57+
result.attachments.append(f" {node}")
58+
return result
59+
60+
61+
def find_so_using_cuda_path(lib_searched_for: str) -> FindResult:
62+
"""Find a .so file using the CUDA path.
63+
64+
Args:
65+
lib_searched_for: The library name to search for
66+
67+
Returns:
68+
FindResult containing the search results
69+
"""
70+
result = FindResult(lib_searched_for)
71+
cuda_path = get_cuda_paths_info("cuda_path", result.error_messages)
72+
if not cuda_path:
73+
return result
74+
75+
file_wild = f"lib{lib_searched_for}.so*"
76+
for sub_dir in ("lib", "lib64"):
77+
path = os.path.join(cuda_path, sub_dir)
78+
if not os.path.isdir(path):
79+
continue
80+
for node in sorted(os.listdir(path)):
81+
if node.startswith(f"lib{lib_searched_for}.so"):
82+
result.abs_path = os.path.join(path, node)
83+
return result
84+
85+
result.error_messages.append(f"No such file: {file_wild}")
86+
for sub_dir in ("lib", "lib64"):
87+
path = os.path.join(cuda_path, sub_dir)
88+
if os.path.isdir(path):
89+
result.attachments.append(f' listdir("{path}"):')
90+
for node in sorted(os.listdir(path)):
91+
result.attachments.append(f" {node}")
92+
return result
93+
94+
95+
def find_nvidia_dynamic_library(lib_searched_for: str) -> FindResult:
96+
"""Find a NVIDIA dynamic library on Linux.
97+
98+
Args:
99+
lib_searched_for: The library name to search for
100+
101+
Returns:
102+
FindResult containing the search results
103+
"""
104+
# Try NVIDIA library directories first
105+
result = find_so_using_nvidia_lib_dirs(lib_searched_for)
106+
if result.abs_path:
107+
return result
108+
109+
# Then try CUDA library directory
110+
result = find_so_using_cudalib_dir(lib_searched_for)
111+
if result.abs_path:
112+
return result
113+
114+
# Finally try CUDA path
115+
result = find_so_using_cuda_path(lib_searched_for)
116+
return result
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2024-2025 NVIDIA Corporation. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4+
5+
import os
6+
7+
from .find_dl_common import FindResult, get_cuda_paths_info
8+
9+
10+
def find_dll_under_dir(lib_searched_for: str, dir_path: str) -> FindResult:
11+
"""Find a .dll file under a specific directory.
12+
13+
Args:
14+
lib_searched_for: The library name to search for
15+
dir_path: The directory to search in
16+
17+
Returns:
18+
FindResult containing the search results
19+
"""
20+
result = FindResult(lib_searched_for)
21+
file_wild = f"{lib_searched_for}.dll"
22+
23+
if not os.path.isdir(dir_path):
24+
result.error_messages.append(f"No such directory: {dir_path}")
25+
return result
26+
27+
for node in sorted(os.listdir(dir_path)):
28+
if node.lower() == file_wild.lower():
29+
result.abs_path = os.path.join(dir_path, node)
30+
return result
31+
32+
result.error_messages.append(f"No such file: {file_wild}")
33+
result.attachments.append(f' listdir("{dir_path}"):')
34+
for node in sorted(os.listdir(dir_path)):
35+
result.attachments.append(f" {node}")
36+
return result
37+
38+
39+
def find_dll_using_cudalib_dir(lib_searched_for: str) -> FindResult:
40+
"""Find a .dll file using the CUDA library directory.
41+
42+
Args:
43+
lib_searched_for: The library name to search for
44+
45+
Returns:
46+
FindResult containing the search results
47+
"""
48+
result = FindResult(lib_searched_for)
49+
cudalib_dir = get_cuda_paths_info("cudalib_dir", result.error_messages)
50+
if not cudalib_dir:
51+
return result
52+
53+
return find_dll_under_dir(lib_searched_for, cudalib_dir)
54+
55+
56+
def find_dll_using_cuda_path(lib_searched_for: str) -> FindResult:
57+
"""Find a .dll file using the CUDA path.
58+
59+
Args:
60+
lib_searched_for: The library name to search for
61+
62+
Returns:
63+
FindResult containing the search results
64+
"""
65+
result = FindResult(lib_searched_for)
66+
cuda_path = get_cuda_paths_info("cuda_path", result.error_messages)
67+
if not cuda_path:
68+
return result
69+
70+
bin_path = os.path.join(cuda_path, "bin")
71+
return find_dll_under_dir(lib_searched_for, bin_path)
72+
73+
74+
def find_nvidia_dynamic_library(lib_searched_for: str) -> FindResult:
75+
"""Find a NVIDIA dynamic library on Windows.
76+
77+
Args:
78+
lib_searched_for: The library name to search for
79+
80+
Returns:
81+
FindResult containing the search results
82+
"""
83+
# Try CUDA library directory first
84+
result = find_dll_using_cudalib_dir(lib_searched_for)
85+
if result.abs_path:
86+
return result
87+
88+
# Then try CUDA path
89+
result = find_dll_using_cuda_path(lib_searched_for)
90+
return result

0 commit comments

Comments
 (0)