|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import platform |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +openml_logger = logging.getLogger("openml") |
| 9 | + |
| 10 | +# Default values (see also https://github.com/openml/OpenML/wiki/Client-API-Standards) |
| 11 | +_user_path = Path("~").expanduser().absolute() |
| 12 | + |
| 13 | + |
| 14 | +def _resolve_default_cache_dir() -> Path: |
| 15 | + user_defined_cache_dir = os.environ.get("OPENML_CACHE_DIR") |
| 16 | + if user_defined_cache_dir is not None: |
| 17 | + return Path(user_defined_cache_dir) |
| 18 | + |
| 19 | + if platform.system().lower() != "linux": |
| 20 | + return _user_path / ".openml" |
| 21 | + |
| 22 | + xdg_cache_home = os.environ.get("XDG_CACHE_HOME") |
| 23 | + if xdg_cache_home is None: |
| 24 | + return Path("~", ".cache", "openml") |
| 25 | + |
| 26 | + # This is the proper XDG_CACHE_HOME directory, but |
| 27 | + # we unfortunately had a problem where we used XDG_CACHE_HOME/org, |
| 28 | + # we check heuristically if this old directory still exists and issue |
| 29 | + # a warning if it does. There's too much data to move to do this for the user. |
| 30 | + |
| 31 | + # The new cache directory exists |
| 32 | + cache_dir = Path(xdg_cache_home) / "openml" |
| 33 | + if cache_dir.exists(): |
| 34 | + return cache_dir |
| 35 | + |
| 36 | + # The old cache directory *does not* exist |
| 37 | + heuristic_dir_for_backwards_compat = Path(xdg_cache_home) / "org" / "openml" |
| 38 | + if not heuristic_dir_for_backwards_compat.exists(): |
| 39 | + return cache_dir |
| 40 | + |
| 41 | + root_dir_to_delete = Path(xdg_cache_home) / "org" |
| 42 | + openml_logger.warning( |
| 43 | + "An old cache directory was found at '%s'. This directory is no longer used by " |
| 44 | + "OpenML-Python. To silence this warning you would need to delete the old cache " |
| 45 | + "directory. The cached files will then be located in '%s'.", |
| 46 | + root_dir_to_delete, |
| 47 | + cache_dir, |
| 48 | + ) |
| 49 | + return Path(xdg_cache_home) |
0 commit comments