Coverage for cuda / pathfinder / _utils / env_vars.py: 90.48%
21 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) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
4import os
5import warnings
8def _paths_differ(a: str, b: str) -> bool:
9 """
10 Return True if paths are observably different.
12 Strategy:
13 1) Compare os.path.normcase(os.path.normpath(...)) for quick, robust textual equality.
14 - Handles trailing slashes and case-insensitivity on Windows.
15 2) If still different AND both exist, use os.path.samefile to resolve symlinks/junctions.
16 3) Otherwise (nonexistent paths or samefile unavailable), treat as different.
17 """
18 norm_a = os.path.normcase(os.path.normpath(a)) 1afghijklmnopqrstuvwxyzABCDEFGHIJKLMNObQPced
19 norm_b = os.path.normcase(os.path.normpath(b)) 1afghijklmnopqrstuvwxyzABCDEFGHIJKLMNObQPced
20 if norm_a == norm_b: 1afghijklmnopqrstuvwxyzABCDEFGHIJKLMNObQPced
21 return False 1afghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
23 try: 1bQPced
24 if os.path.exists(a) and os.path.exists(b): 1bQPced
25 # samefile raises on non-existent paths; only call when both exist.
26 return not os.path.samefile(a, b) 1Qced
27 except OSError:
28 # Fall through to "different" if samefile isn't applicable/available.
29 pass
31 # If normalized strings differ and we couldn't prove they're the same entry, treat as different.
32 return True 1bP
35def get_cuda_home_or_path() -> str | None:
36 cuda_home = os.environ.get("CUDA_HOME") 1aRSfTghijklmnopqrstuvwxyzABCDEFGHIJKUVWXYZL0M1NObc2e3d
37 cuda_path = os.environ.get("CUDA_PATH") 1aRSfTghijklmnopqrstuvwxyzABCDEFGHIJKUVWXYZL0M1NObc2e3d
39 if cuda_home and cuda_path and _paths_differ(cuda_home, cuda_path): 1aRSfTghijklmnopqrstuvwxyzABCDEFGHIJKUVWXYZL0M1NObc2e3d
40 warnings.warn( 1bcd
41 "Both CUDA_HOME and CUDA_PATH are set but differ:\n"
42 f" CUDA_HOME={cuda_home}\n"
43 f" CUDA_PATH={cuda_path}\n"
44 "Using CUDA_HOME (higher priority).",
45 UserWarning,
46 stacklevel=2,
47 )
49 if cuda_home is not None: 1aRSfTghijklmnopqrstuvwxyzABCDEFGHIJKUVWXYZL0M1NObc2e3d
50 return cuda_home 1aRfTghijklmnopqrstuvwxyzABCDEFGHIJKWYL0M1NObced
51 return cuda_path 1SUVXZ23