From aaa9955ade1090b7f9836d3a33d2f3f32b344f84 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Tue, 19 Mar 2024 13:11:24 -0500 Subject: [PATCH 1/2] PersistentDict: honor XDG_CACHE_HOME on MacOS --- pytools/persistent_dict.py | 9 ++++++++- pytools/test/test_persistent_dict.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pytools/persistent_dict.py b/pytools/persistent_dict.py index ed1e8031..4e6d64cb 100644 --- a/pytools/persistent_dict.py +++ b/pytools/persistent_dict.py @@ -556,8 +556,15 @@ def __init__(self, identifier, key_builder=None, container_dir=None): except ImportError: import appdirs + if sys.platform == "darwin" and os.getenv("XDG_CACHE_HOME") is not None: + # appdirs and platformdirs do not handle XDG_CACHE_HOME on macOS + # https://github.com/platformdirs/platformdirs/issues/269 + cache_dir = join(os.getenv("XDG_CACHE_HOME"), "pytools") + else: + cache_dir = appdirs.user_cache_dir("pytools", "pytools") + container_dir = join( - appdirs.user_cache_dir("pytools", "pytools"), + cache_dir, "pdict-v4-{}-py{}".format( identifier, ".".join(str(i) for i in sys.version_info))) diff --git a/pytools/test/test_persistent_dict.py b/pytools/test/test_persistent_dict.py index d0b32c06..21092820 100644 --- a/pytools/test/test_persistent_dict.py +++ b/pytools/test/test_persistent_dict.py @@ -582,6 +582,28 @@ class MyAttrs2: assert keyb(MyAttrs2("hi", 1)) != keyb(MyAttrs("hi", 1)) +def test_xdg_cache_home(): + import os + xdg_dir = "tmpdir_pytools_xdg_test" + + assert not os.path.exists(xdg_dir) + + try: + old_xdg_cache_home = os.getenv("XDG_CACHE_HOME") + os.environ["XDG_CACHE_HOME"] = xdg_dir + + PersistentDict("pytools-test") + + assert os.path.exists(xdg_dir) + finally: + if old_xdg_cache_home is not None: + os.environ["XDG_CACHE_HOME"] = old_xdg_cache_home + else: + del os.environ["XDG_CACHE_HOME"] + + shutil.rmtree(xdg_dir) + + if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) From ef75ec710fd29a933ea34e4b71d5aa0b8f0b55fc Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Tue, 19 Mar 2024 13:20:09 -0500 Subject: [PATCH 2/2] run pytest on macos --- .github/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ccbc92db..ce3f5663 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,11 +68,12 @@ jobs: . ./prepare-and-run-mypy.sh python3 mypy pytest: - name: Pytest on Py${{ matrix.python-version }} - runs-on: ubuntu-latest + name: Pytest on Py${{ matrix.python-version }} ${{ matrix.os }} + runs-on: ${{ matrix.os }} strategy: matrix: python-version: ["3.8", "3.9", "3.x", "pypy3.8"] + os: [ubuntu-latest, macos-latest] steps: - uses: actions/checkout@v4 -