From b00c5254e36953c1900e96d1537e998a68736663 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Mon, 26 Aug 2024 20:52:57 +0200 Subject: [PATCH] enh: delete legacy cache if it exists --- pip_audit/_cache.py | 15 +++++++++++++++ test/test_cache.py | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index 6c20d890..cde0d438 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -6,6 +6,7 @@ import logging import os +import shutil import subprocess import sys from pathlib import Path @@ -29,6 +30,8 @@ _PIP_VERSION = Version(str(pip_api.PIP_VERSION)) +_PIP_AUDIT_LEGACY_INTERNAL_CACHE = Path.home() / ".pip-audit-cache" + def _get_pip_cache() -> Path: # Unless the cache directory is specifically set by the `--cache-dir` option, we try to share @@ -45,6 +48,14 @@ def _get_pip_cache() -> Path: return http_cache_dir +def _delete_legacy_cache_dir(current_cache_dir: Path, legacy_cache_dir: Path) -> None: + """ + Deletes the legacy `pip-audit` if it exists. + """ + if current_cache_dir != legacy_cache_dir: + shutil.rmtree(legacy_cache_dir) + + def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Path: """ Returns a directory path suitable for HTTP caching. @@ -62,6 +73,10 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa # Retrieve pip-audit's default internal cache using `platformdirs`. pip_audit_cache_dir = user_cache_path("pip-audit", appauthor=False, ensure_exists=True) + # If the retrieved cache isn't the legacy one, try to delete it. + if _PIP_AUDIT_LEGACY_INTERNAL_CACHE.exists(): + _delete_legacy_cache_dir(pip_audit_cache_dir, _PIP_AUDIT_LEGACY_INTERNAL_CACHE) + # Respect pip's PIP_NO_CACHE_DIR environment setting. if use_pip and not os.getenv("PIP_NO_CACHE_DIR"): pip_cache_dir = _get_pip_cache() if _PIP_VERSION >= _MINIMUM_PIP_VERSION else None diff --git a/test/test_cache.py b/test/test_cache.py index b2b6702b..0f2834fd 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -9,7 +9,7 @@ from pytest import MonkeyPatch import pip_audit._cache as cache -from pip_audit._cache import _get_cache_dir, _get_pip_cache +from pip_audit._cache import _delete_legacy_cache_dir, _get_cache_dir, _get_pip_cache def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: @@ -138,7 +138,9 @@ def test_get_cache_dir_old_pip(monkeypatch, sys_platform, expected): def test_cache_warns_about_old_pip(monkeypatch, cache_dir): monkeypatch.setattr(cache, "_PIP_VERSION", Version("1.0.0")) - logger = pretend.stub(warning=pretend.call_recorder(lambda s: None)) + logger = pretend.stub( + warning=pretend.call_recorder(lambda s: None), debug=pretend.call_recorder(lambda s: None) + ) monkeypatch.setattr(cache, "logger", logger) # If we supply a cache directory, we're not relying on finding the `pip` cache so no need to log @@ -150,3 +152,13 @@ def test_cache_warns_about_old_pip(monkeypatch, cache_dir): # have an old `pip`, then we should expect a warning to be logged _get_cache_dir(None) assert len(logger.warning.calls) == 1 + + +def test_delete_legacy_cache_dir(tmp_path): + legacy = tmp_path / "pip-audit-cache" + legacy.mkdir() + assert legacy.exists() + + current = _get_cache_dir(None, use_pip=False) + _delete_legacy_cache_dir(current, legacy) + assert not legacy.exists()