|
8 | 8 | import logging.handlers |
9 | 9 | import os |
10 | 10 | import platform |
| 11 | +import shutil |
11 | 12 | import warnings |
12 | 13 | from io import StringIO |
13 | 14 | from pathlib import Path |
|
20 | 21 | console_handler: logging.StreamHandler | None = None |
21 | 22 | file_handler: logging.handlers.RotatingFileHandler | None = None |
22 | 23 |
|
| 24 | +OPENML_CACHE_DIR_ENV_VAR = "OPENML_CACHE_DIR" |
| 25 | + |
23 | 26 |
|
24 | 27 | class _Config(TypedDict): |
25 | 28 | apikey: str |
@@ -101,14 +104,50 @@ def set_file_log_level(file_output_level: int) -> None: |
101 | 104 |
|
102 | 105 | # Default values (see also https://github.com/openml/OpenML/wiki/Client-API-Standards) |
103 | 106 | _user_path = Path("~").expanduser().absolute() |
| 107 | + |
| 108 | + |
| 109 | +def _resolve_default_cache_dir() -> Path: |
| 110 | + user_defined_cache_dir = os.environ.get(OPENML_CACHE_DIR_ENV_VAR) |
| 111 | + if user_defined_cache_dir is not None: |
| 112 | + return Path(user_defined_cache_dir) |
| 113 | + |
| 114 | + if platform.system().lower() != "linux": |
| 115 | + return _user_path / ".openml" |
| 116 | + |
| 117 | + xdg_cache_home = os.environ.get("XDG_CACHE_HOME") |
| 118 | + if xdg_cache_home is None: |
| 119 | + return Path("~", ".cache", "openml") |
| 120 | + |
| 121 | + # This is the proper XDG_CACHE_HOME directory, but |
| 122 | + # we unfortunately had a problem where we used XDG_CACHE_HOME/org, |
| 123 | + # we check heuristically if this old directory still exists and issue |
| 124 | + # a warning if it does. There's too much data to move to do this for the user. |
| 125 | + |
| 126 | + # The new cache directory exists |
| 127 | + cache_dir = Path(xdg_cache_home) / "openml" |
| 128 | + if cache_dir.exists(): |
| 129 | + return cache_dir |
| 130 | + |
| 131 | + # The old cache directory *does not* exist |
| 132 | + heuristic_dir_for_backwards_compat = Path(xdg_cache_home) / "org" / "openml" |
| 133 | + if not heuristic_dir_for_backwards_compat.exists(): |
| 134 | + return cache_dir |
| 135 | + |
| 136 | + root_dir_to_delete = Path(xdg_cache_home) / "org" |
| 137 | + openml_logger.warning( |
| 138 | + "An old cache directory was found at '%s'. This directory is no longer used by " |
| 139 | + "OpenML-Python. To silence this warning you would need to delete the old cache " |
| 140 | + "directory. The cached files will then be located in '%s'.", |
| 141 | + root_dir_to_delete, |
| 142 | + cache_dir, |
| 143 | + ) |
| 144 | + return Path(xdg_cache_home) |
| 145 | + |
| 146 | + |
104 | 147 | _defaults: _Config = { |
105 | 148 | "apikey": "", |
106 | 149 | "server": "https://www.openml.org/api/v1/xml", |
107 | | - "cachedir": ( |
108 | | - Path(os.environ.get("XDG_CACHE_HOME", _user_path / ".cache" / "openml")) |
109 | | - if platform.system() == "Linux" |
110 | | - else _user_path / ".openml" |
111 | | - ), |
| 150 | + "cachedir": _resolve_default_cache_dir(), |
112 | 151 | "avoid_duplicate_runs": True, |
113 | 152 | "retry_policy": "human", |
114 | 153 | "connection_n_retries": 5, |
@@ -218,11 +257,66 @@ def stop_using_configuration_for_example(cls) -> None: |
218 | 257 | cls._start_last_called = False |
219 | 258 |
|
220 | 259 |
|
| 260 | +def _handle_xdg_config_home_backwards_compatibility( |
| 261 | + xdg_home: str, |
| 262 | +) -> Path: |
| 263 | + # NOTE(eddiebergman): A previous bug results in the config |
| 264 | + # file being located at `${XDG_CONFIG_HOME}/config` instead |
| 265 | + # of `${XDG_CONFIG_HOME}/openml/config`. As to maintain backwards |
| 266 | + # compatibility, where users may already may have had a configuration, |
| 267 | + # we copy it over an issue a warning until it's deleted. |
| 268 | + # As a heurisitic to ensure that it's "our" config file, we try parse it first. |
| 269 | + config_dir = Path(xdg_home) / "openml" |
| 270 | + |
| 271 | + backwards_compat_config_file = Path(xdg_home) / "config" |
| 272 | + if not backwards_compat_config_file.exists(): |
| 273 | + return config_dir |
| 274 | + |
| 275 | + # If it errors, that's a good sign it's not ours and we can |
| 276 | + # safely ignore it, jumping out of this block. This is a heurisitc |
| 277 | + try: |
| 278 | + _parse_config(backwards_compat_config_file) |
| 279 | + except Exception: # noqa: BLE001 |
| 280 | + return config_dir |
| 281 | + |
| 282 | + # Looks like it's ours, lets try copy it to the correct place |
| 283 | + correct_config_location = config_dir / "config" |
| 284 | + try: |
| 285 | + # We copy and return the new copied location |
| 286 | + shutil.copy(backwards_compat_config_file, correct_config_location) |
| 287 | + openml_logger.warning( |
| 288 | + "An openml configuration file was found at the old location " |
| 289 | + f"at {backwards_compat_config_file}. We have copied it to the new " |
| 290 | + f"location at {correct_config_location}. " |
| 291 | + "\nTo silence this warning please verify that the configuration file " |
| 292 | + f"at {correct_config_location} is correct and delete the file at " |
| 293 | + f"{backwards_compat_config_file}." |
| 294 | + ) |
| 295 | + return config_dir |
| 296 | + except Exception as e: # noqa: BLE001 |
| 297 | + # We failed to copy and its ours, return the old one. |
| 298 | + openml_logger.warning( |
| 299 | + "While attempting to perform a backwards compatible fix, we " |
| 300 | + f"failed to copy the openml config file at " |
| 301 | + f"{backwards_compat_config_file}' to {correct_config_location}" |
| 302 | + f"\n{type(e)}: {e}", |
| 303 | + "\n\nTo silence this warning, please copy the file " |
| 304 | + "to the new location and delete the old file at " |
| 305 | + f"{backwards_compat_config_file}.", |
| 306 | + ) |
| 307 | + return backwards_compat_config_file |
| 308 | + |
| 309 | + |
221 | 310 | def determine_config_file_path() -> Path: |
222 | | - if platform.system() == "Linux": |
223 | | - config_dir = Path(os.environ.get("XDG_CONFIG_HOME", Path("~") / ".config" / "openml")) |
| 311 | + if platform.system().lower() == "linux": |
| 312 | + xdg_home = os.environ.get("XDG_CONFIG_HOME") |
| 313 | + if xdg_home is not None: |
| 314 | + config_dir = _handle_xdg_config_home_backwards_compatibility(xdg_home) |
| 315 | + else: |
| 316 | + config_dir = Path("~", ".config", "openml") |
224 | 317 | else: |
225 | 318 | config_dir = Path("~") / ".openml" |
| 319 | + |
226 | 320 | # Still use os.path.expanduser to trigger the mock in the unit test |
227 | 321 | config_dir = Path(config_dir).expanduser().resolve() |
228 | 322 | return config_dir / "config" |
@@ -260,11 +354,15 @@ def _setup(config: _Config | None = None) -> None: |
260 | 354 | apikey = config["apikey"] |
261 | 355 | server = config["server"] |
262 | 356 | show_progress = config["show_progress"] |
263 | | - short_cache_dir = Path(config["cachedir"]) |
264 | 357 | n_retries = int(config["connection_n_retries"]) |
265 | 358 |
|
266 | 359 | set_retry_policy(config["retry_policy"], n_retries) |
267 | 360 |
|
| 361 | + user_defined_cache_dir = os.environ.get(OPENML_CACHE_DIR_ENV_VAR) |
| 362 | + if user_defined_cache_dir is not None: |
| 363 | + short_cache_dir = Path(user_defined_cache_dir) |
| 364 | + else: |
| 365 | + short_cache_dir = Path(config["cachedir"]) |
268 | 366 | _root_cache_directory = short_cache_dir.expanduser().resolve() |
269 | 367 |
|
270 | 368 | try: |
|
0 commit comments