| 49 | """ |
| 50 | |
| 51 | def __init__(self): |
| 52 | # Check if TMPDIR is set and handle the case where it doesn't exist |
| 53 | tmpdir = os.environ.get("TMPDIR") or os.environ.get("TEMP") or os.environ.get("TMP") |
| 54 | # Normalize the path to handle any path resolution issues |
| 55 | if tmpdir: |
| 56 | tmpdir = os.path.normpath(tmpdir) |
| 57 | if not os.path.exists(tmpdir): |
| 58 | # Auto-create the directory if it doesn't exist |
| 59 | # This prevents tempfile from silently falling back to /tmp |
| 60 | try: |
| 61 | os.makedirs(tmpdir, exist_ok=True) |
| 62 | logger.info(f"Created TMPDIR directory: {tmpdir}") |
| 63 | except OSError as e: |
| 64 | raise OSError( |
| 65 | f"TMPDIR is set to '{tmpdir}' but the directory does not exist and could not be created: {e}. " |
| 66 | "Please create it manually or unset TMPDIR to fall back to the default temporary directory." |
| 67 | ) from e |
| 68 | # If tmpdir exists, verify it's actually a directory and writable |
| 69 | elif not os.path.isdir(tmpdir): |
| 70 | raise OSError( |
| 71 | f"TMPDIR is set to '{tmpdir}' but it is not a directory. " |
| 72 | "Please point TMPDIR to a writable directory or unset it to fall back to the default temporary directory." |
| 73 | ) |
| 74 | |
| 75 | # Explicitly pass the directory to mkdtemp to ensure TMPDIR is respected |
| 76 | # This works even if tempfile.gettempdir() was already called and cached |
| 77 | # Pass dir=None if tmpdir is None to use default temp directory |
| 78 | self.name = tempfile.mkdtemp(prefix=config.TEMP_CACHE_DIR_PREFIX, dir=tmpdir) |
| 79 | self._finalizer = weakref.finalize(self, self._cleanup) |
| 80 | |
| 81 | def _cleanup(self): |
| 82 | for dset in get_datasets_with_cache_file_in_temp_dir(): |