Skip to content

Commit 57a5aa9

Browse files
committed
use new cache dir
1 parent 8b596b0 commit 57a5aa9

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/firebolt/utils/cache.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import getpass
12
import logging
23
import os
4+
import sys
35
import time
46
from dataclasses import asdict, dataclass, field
57
from json import JSONDecodeError
@@ -17,8 +19,6 @@
1719
TypeVar,
1820
)
1921

20-
from appdirs import user_data_dir
21-
2222
from firebolt.utils.file_operations import (
2323
FernetEncrypter,
2424
generate_encrypted_file_name,
@@ -193,6 +193,37 @@ def __hash__(self) -> int:
193193
return hash(self.key)
194194

195195

196+
def get_cache_data_dir(appname: str = APPNAME) -> str:
197+
"""
198+
Return the directory for storing cache files based on the OS.
199+
Mac: use $TMPDIR
200+
Windows: use the system property 'java.io.tmpdir'
201+
Linux: use $XDG_RUNTIME_DIR, fallback to /tmp/<user_home>
202+
"""
203+
204+
if sys.platform == "darwin":
205+
tmpdir = os.environ.get("TMPDIR")
206+
if tmpdir:
207+
return os.path.join(tmpdir, appname)
208+
# fallback
209+
return os.path.join("/tmp", appname)
210+
elif sys.platform.startswith("win"):
211+
# Python doesn't expose java.io.tmpdir, but os.environ['TEMP'] is standard
212+
tmpdir = os.environ.get("TEMP")
213+
if tmpdir:
214+
return os.path.join(tmpdir, appname)
215+
# fallback
216+
return os.path.join("C:\\Temp", appname)
217+
else:
218+
# Assume Linux/Unix
219+
xdg_dir = os.environ.get("XDG_RUNTIME_DIR")
220+
if xdg_dir:
221+
return os.path.join(xdg_dir, appname)
222+
# fallback: /tmp/<username>
223+
username = getpass.getuser()
224+
return os.path.join("/tmp", username, appname)
225+
226+
196227
class FileBasedCache:
197228
"""
198229
File-based cache that persists to disk with encryption.
@@ -202,7 +233,7 @@ class FileBasedCache:
202233

203234
def __init__(self, memory_cache: UtilCache[ConnectionInfo], cache_name: str = ""):
204235
self.memory_cache = memory_cache
205-
self._data_dir = user_data_dir(appname=APPNAME) # TODO: change to new dir
236+
self._data_dir = get_cache_data_dir(APPNAME)
206237
makedirs(self._data_dir, exist_ok=True)
207238
# FileBasedCache has its own disabled state, independent of memory cache
208239
cache_env_var = f"FIREBOLT_SDK_DISABLE_CACHE_${cache_name}"

tests/unit/utils/test_cache.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import getpass
12
import json
23
import os
34
import re
5+
import sys
46
import time
57
from typing import Generator
68
from unittest.mock import mock_open, patch
@@ -10,12 +12,14 @@
1012
from firebolt.client.auth.client_credentials import ClientCredentials
1113
from firebolt.db import connect
1214
from firebolt.utils.cache import (
15+
APPNAME,
1316
CACHE_EXPIRY_SECONDS,
1417
ConnectionInfo,
1518
FileBasedCache,
1619
SecureCacheKey,
1720
UtilCache,
1821
_firebolt_cache,
22+
get_cache_data_dir,
1923
)
2024
from firebolt.utils.file_operations import FernetEncrypter, generate_salt
2125
from tests.unit.response import Response
@@ -1319,3 +1323,36 @@ def test_disk_file_operations_with_pyfakefs(
13191323
)
13201324
cache_data = _firebolt_cache.get(cache_key)
13211325
assert cache_data is not None, "Cache data should be accessible"
1326+
1327+
1328+
def test_get_cache_data_dir_os(monkeypatch):
1329+
"""Test get_cache_data_dir returns correct path for each OS and env."""
1330+
1331+
# Mac
1332+
monkeypatch.setattr(sys, "platform", "darwin")
1333+
monkeypatch.setenv("TMPDIR", "/mac/tmpdir/")
1334+
assert get_cache_data_dir(APPNAME) == os.path.join("/mac/tmpdir/", APPNAME)
1335+
monkeypatch.delenv("TMPDIR", raising=False)
1336+
assert get_cache_data_dir(APPNAME) == os.path.join("/tmp", APPNAME)
1337+
1338+
# Windows
1339+
monkeypatch.setattr(sys, "platform", "win32")
1340+
monkeypatch.setenv("TEMP", "C:/win/TEMP/")
1341+
assert get_cache_data_dir(APPNAME) == os.path.join("C:/win/TEMP/", APPNAME)
1342+
monkeypatch.delenv("TEMP", raising=False)
1343+
# Fallback to C:\\Temp/firebolt, but os.path.join on non-Windows will use /, so check both
1344+
expected = os.path.join("C:\\Temp", APPNAME)
1345+
actual = get_cache_data_dir(APPNAME)
1346+
assert actual == expected or actual.replace("\\", "/") == expected.replace(
1347+
"\\", "/"
1348+
)
1349+
1350+
# Linux
1351+
monkeypatch.setattr(sys, "platform", "linux")
1352+
monkeypatch.setenv("XDG_RUNTIME_DIR", "/linux/xdg/")
1353+
assert get_cache_data_dir(APPNAME) == os.path.join("/linux/xdg/", APPNAME)
1354+
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
1355+
monkeypatch.setenv("user.home", "/home/testuser")
1356+
monkeypatch.setattr(getpass, "getuser", lambda: "testuser")
1357+
assert get_cache_data_dir(APPNAME) == os.path.join("/tmp", "testuser", APPNAME)
1358+
monkeypatch.delenv("user.home", raising=False)

0 commit comments

Comments
 (0)