diff --git a/cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py b/cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py index ac038aadfe7..803abecaaae 100644 --- a/cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py @@ -4,6 +4,7 @@ import functools import os from dataclasses import dataclass +from pathlib import Path from typing import NoReturn, TypedDict from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home @@ -35,7 +36,7 @@ class _BitcodeLibInfo(TypedDict): _SUPPORTED_BITCODE_LIBS_INFO: dict[str, _BitcodeLibInfo] = { "device": { "filename": "libdevice.10.bc", - "rel_path": os.path.join("nvvm", "libdevice"), + "rel_path": "nvvm/libdevice", "site_packages_dirs": ( "nvidia/cu13/nvvm/libdevice", "nvidia/cuda_nvcc/nvvm/libdevice", @@ -64,14 +65,14 @@ class _BitcodeLibInfo(TypedDict): ) -def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None: - error_messages.append(f"No such file: {os.path.join(dir_path, filename)}") - if os.path.isdir(dir_path): - attachments.append(f' listdir("{dir_path}"):') - for node in sorted(os.listdir(dir_path)): +def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None: + error_messages.append(f"No such file: {directory / filename}") + if directory.is_dir(): + attachments.append(f' listdir("{directory}"):') + for node in sorted(node_path.name for node_path in directory.iterdir()): attachments.append(f" {node}") else: - attachments.append(f' Directory does not exist: "{dir_path}"') + attachments.append(f' Directory does not exist: "{directory}"') class _FindBitcodeLib: @@ -86,38 +87,39 @@ def __init__(self, name: str) -> None: self.error_messages: list[str] = [] self.attachments: list[str] = [] - def try_site_packages(self) -> str | None: + def try_site_packages(self) -> Path | None: for rel_dir in self.site_packages_dirs: sub_dir = tuple(rel_dir.split("/")) for abs_dir in find_sub_dirs_all_sitepackages(sub_dir): - file_path = os.path.join(abs_dir, self.filename) - if os.path.isfile(file_path): + file_path = Path(abs_dir, self.filename) + if file_path.is_file(): return file_path return None - def try_with_conda_prefix(self) -> str | None: + def try_with_conda_prefix(self) -> Path | None: conda_prefix = os.environ.get("CONDA_PREFIX") if not conda_prefix: return None - anchor = os.path.join(conda_prefix, "Library") if IS_WINDOWS else conda_prefix - file_path = os.path.join(anchor, self.rel_path, self.filename) - if os.path.isfile(file_path): + anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix) + file_path = anchor / self.rel_path / self.filename + if file_path.is_file(): return file_path return None - def try_with_cuda_home(self) -> str | None: + def try_with_cuda_home(self) -> Path | None: cuda_home = get_cuda_path_or_home() if cuda_home is None: self.error_messages.append("CUDA_HOME/CUDA_PATH not set") return None - file_path = os.path.join(cuda_home, self.rel_path, self.filename) - if os.path.isfile(file_path): + anchor = Path(cuda_home) + file_path = anchor / self.rel_path / self.filename + if file_path.is_file(): return file_path _no_such_file_in_dir( - os.path.join(cuda_home, self.rel_path), + anchor / self.rel_path, self.filename, self.error_messages, self.attachments, @@ -143,7 +145,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib: if abs_path is not None: return LocatedBitcodeLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="site-packages", ) @@ -152,7 +154,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib: if abs_path is not None: return LocatedBitcodeLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="conda", ) @@ -161,7 +163,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib: if abs_path is not None: return LocatedBitcodeLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="CUDA_PATH", ) diff --git a/cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py b/cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py index ea5a740aec4..2339599a878 100644 --- a/cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py @@ -4,6 +4,7 @@ import functools import os from dataclasses import dataclass +from pathlib import Path from typing import NoReturn, TypedDict from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home @@ -47,8 +48,8 @@ def _cudadevrt_info() -> _StaticLibInfo: conda_fallback_dirs = ("lib",) if arch_dir == "x64" else () return { "filename": "cudadevrt.lib", - "ctk_rel_paths": (os.path.join("lib", arch_dir),), - "conda_rel_paths": (os.path.join("lib", arch_dir), *conda_fallback_dirs), + "ctk_rel_paths": (str(Path("lib", arch_dir)),), + "conda_rel_paths": (str(Path("lib", arch_dir)), *conda_fallback_dirs), "site_packages_dirs": (f"nvidia/cu13/lib/{arch_dir}", *component_wheel_dirs), } @@ -60,14 +61,14 @@ def _cudadevrt_info() -> _StaticLibInfo: SUPPORTED_STATIC_LIBS: tuple[str, ...] = tuple(sorted(_SUPPORTED_STATIC_LIBS_INFO.keys())) -def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None: - error_messages.append(f"No such file: {os.path.join(dir_path, filename)}") - if os.path.isdir(dir_path): - attachments.append(f' listdir("{dir_path}"):') - for node in sorted(os.listdir(dir_path)): +def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None: + error_messages.append(f"No such file: {directory / filename}") + if directory.is_dir(): + attachments.append(f' listdir("{directory}"):') + for node in sorted(node_path.name for node_path in directory.iterdir()): attachments.append(f" {node}") else: - attachments.append(f' Directory does not exist: "{dir_path}"') + attachments.append(f' Directory does not exist: "{directory}"') class _FindStaticLib: @@ -83,40 +84,41 @@ def __init__(self, name: str) -> None: self.error_messages: list[str] = [] self.attachments: list[str] = [] - def try_site_packages(self) -> str | None: + def try_site_packages(self) -> Path | None: for rel_dir in self.site_packages_dirs: sub_dir = tuple(rel_dir.split("/")) for abs_dir in find_sub_dirs_all_sitepackages(sub_dir): - file_path = os.path.join(abs_dir, self.filename) - if os.path.isfile(file_path): + file_path = Path(abs_dir, self.filename) + if file_path.is_file(): return file_path return None - def try_with_conda_prefix(self) -> str | None: + def try_with_conda_prefix(self) -> Path | None: conda_prefix = os.environ.get("CONDA_PREFIX") if not conda_prefix: return None - anchor = os.path.join(conda_prefix, "Library") if IS_WINDOWS else conda_prefix + anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix) for rel_path in self.conda_rel_paths: - file_path = os.path.join(anchor, rel_path, self.filename) - if os.path.isfile(file_path): + file_path = anchor / rel_path / self.filename + if file_path.is_file(): return file_path return None - def try_with_cuda_home(self) -> str | None: + def try_with_cuda_home(self) -> Path | None: cuda_home = get_cuda_path_or_home() if cuda_home is None: self.error_messages.append("CUDA_HOME/CUDA_PATH not set") return None + anchor = Path(cuda_home) for rel_path in self.ctk_rel_paths: - file_path = os.path.join(cuda_home, rel_path, self.filename) - if os.path.isfile(file_path): + file_path = anchor / rel_path / self.filename + if file_path.is_file(): return file_path _no_such_file_in_dir( - os.path.join(cuda_home, self.ctk_rel_paths[0]), + anchor / self.ctk_rel_paths[0], self.filename, self.error_messages, self.attachments, @@ -142,7 +144,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib: if abs_path is not None: return LocatedStaticLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="site-packages", ) @@ -151,7 +153,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib: if abs_path is not None: return LocatedStaticLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="conda", ) @@ -160,7 +162,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib: if abs_path is not None: return LocatedStaticLib( name=name, - abs_path=abs_path, + abs_path=str(abs_path), filename=finder.filename, found_via="CUDA_PATH", )