Coverage for cuda/core/utils/_cache_dir.py: 100.00%
11 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
5"""Shared user-cache-root resolution for cuda.core's on-disk caches."""
7from __future__ import annotations
9import os
10from pathlib import Path
12# Exposed as a module-level flag so tests can toggle it without monkeypatching
13# ``os.name`` itself (pathlib reads ``os.name`` at instantiation time).
14_IS_WINDOWS = os.name == "nt"
17def _default_cache_dir() -> Path:
18 """OS-conventional root for cuda.core's on-disk caches.
20 Resolves to the user-cache root for the calling user, with a
21 ``cuda-python`` vendor leaf so callers can each place their own cache
22 under a stable, shared root:
24 * Linux: ``$XDG_CACHE_HOME/cuda-python``
25 (default ``~/.cache/cuda-python`` per the XDG Base Directory spec).
26 * Windows: ``%LOCALAPPDATA%\\cuda-python``
27 (Windows uses local AppData -- caches don't roam; falls back to
28 ``~/AppData/Local`` if the env var is unset).
30 CUDA does not support macOS, so no macOS branch is provided.
32 Callers append their own leaf directory, e.g. ``program-cache`` or
33 ``nvrtc-headers``.
34 """
35 if _IS_WINDOWS: 1a
36 local_app_data = os.environ.get("LOCALAPPDATA") 1a
37 root = Path(local_app_data) if local_app_data else Path.home() / "AppData" / "Local" 1a
38 else:
39 xdg = os.environ.get("XDG_CACHE_HOME") 1a
40 root = Path(xdg) if xdg else Path.home() / ".cache" 1a
41 return root / "cuda-python" 1a