Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More respectful convention for caching #814

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

aanghelidi
Copy link

A PR that aims to close #342.

All the logic is inside _get_internal_cache_path in _cache.py.

This commit implements cache's location as described in pypa#342.
It default to the XDG Base Directory Specification excepts on
macOS where it is located at `$HOME/Library/Caches/pip-audit`.

Closes pypa#342
On Windows, although not "officially" a convention, it appears
that programs like `pip` use %LocalAppData%\<program_name>\Cache.
This commit implements that default.
See https://pip.pypa.io/en/latest/topics/caching/#default-paths.

Closes pypa#342.
@aanghelidi aanghelidi marked this pull request as ready for review August 18, 2024 10:09
@aanghelidi aanghelidi changed the title [DRAFT] More respectful convention for caching More respectful convention for caching Aug 18, 2024
Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aanghelidi, this looks pretty reasonable to me. pip-audit's choice of cache location isn't a stability commitment we make, so we should be fine to change this without making a major release.

A couple of thoughts:

  • Maybe we could use platformdirs to abstract the cache location discovery for us? OTOH the current logic in _get_internal_cache_path is pretty simple; curious what you think.
  • Maybe we should blow away the old cache if a new path is discovered? Otherwise, users will end up silently keeping _PIP_AUDIT_INTERNAL_CACHE around in their $HOME taking up space, which isn't very polite of us.

For the second point, something like this is what I have in mind:

# if the selected internal cache isn't the old one, delete the old one.
if internal_cache_path != _PIP_AUDIT_LEGACY_INTERNAL_CACHE:
    shutil.rmtree(_PIP_AUDIT_LEGACY_INTERNAL_CACHE)

CC @di for thoughts as well 🙂

@aanghelidi
Copy link
Author

Thanks @woodruffw for the review 🙂 TIL about platformdirs ! This is indeed a really handy package. Regarding the aforementioned points:

  • There is several pros about using platformdirs:

    • simplicity. Using platformdirs, the implementation of pip-audit's cache location is a one-liner.
    • we do not reinvent the wheel. This means less maintenance for the project.
    • consistency. Upon research, it seems that platformdirs is already used in several pypa projects:
  • Regarding the potential con(s):

    • dependency cost. We add one more dependency to the project. Although platformdirs itself has no dependencies, it's still a decision to make. Maybe this is not worth adding given the simplicity of _get_internal_cache_path implementation (which I have the feeling could be simplified).
  • Regarding PIP_AUDIT_INTERNAL_CACHE, it is indeed not nice to keep the old cache in $HOME. Removing it seems to be a good idea. Maybe, as an alternative, using shutil.move to move the old cache to the new location could be an idea?

I added a commit using platformdirs for the cache location, let me know what you think 🙂

@woodruffw
Copy link
Member

  • Regarding PIP_AUDIT_INTERNAL_CACHE, it is indeed not nice to keep the old cache in $HOME. Removing it seems to be a good idea. Maybe, as an alternative, using shutil.move to move the old cache to the new location could be an idea?

I think we should just blow it away for now -- #794 covers some larger architectural changes we need to make to handle newer versions of pip's cache anyways, so these old paths may not be super useful when moved to the new location.

I added a commit using platformdirs for the cache location, let me know what you think 🙂

LGTM, thank you!

@aanghelidi
Copy link
Author

I think we should just blow it away for now -- #794 covers some larger architectural changes we need to make to handle newer versions of pip's cache anyways, so these old paths may not be super useful when moved to the new location

Thank you for the context @woodruffw ! I added a extra commit to delete the old cache that goes into that direction. Let me know what you think 🙂

@aanghelidi aanghelidi force-pushed the enh/cache-conventional-location branch from b00c525 to f67e9cc Compare August 26, 2024 19:03
Comment on lines 51 to 56
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a helper here; let's just inline this into _get_cache_dir IMO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commit now inline the deletion into _get_cache_dir ✔️

Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aanghelidi, LGTM!

Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last request: could you add a CHANGELOG entry for this change? See the other entries in the log for our preferred style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caching: Use a more respectful default location?
2 participants