Coverage for cuda / pathfinder / _utils / env_vars.py: 90%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-10 01:19 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: Apache-2.0 

3 

4import os 

5import warnings 

6 

7 

8def _paths_differ(a: str, b: str) -> bool: 

9 """ 

10 Return True if paths are observably different. 

11 

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)) 

19 norm_b = os.path.normcase(os.path.normpath(b)) 

20 if norm_a == norm_b: 

21 return False 

22 

23 try: 

24 if os.path.exists(a) and os.path.exists(b): 

25 # samefile raises on non-existent paths; only call when both exist. 

26 return not os.path.samefile(a, b) 

27 except OSError: 

28 # Fall through to "different" if samefile isn't applicable/available. 

29 pass 

30 

31 # If normalized strings differ and we couldn't prove they're the same entry, treat as different. 

32 return True 

33 

34 

35def get_cuda_home_or_path() -> str | None: 

36 cuda_home = os.environ.get("CUDA_HOME") 

37 cuda_path = os.environ.get("CUDA_PATH") 

38 

39 if cuda_home and cuda_path and _paths_differ(cuda_home, cuda_path): 

40 warnings.warn( 

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 ) 

48 

49 if cuda_home is not None: 

50 return cuda_home 

51 return cuda_path