appdirs.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. This code wraps the vendored appdirs module to so the return values are
  3. compatible for the current pip code base.
  4. The intention is to rewrite current usages gradually, keeping the tests pass,
  5. and eventually drop this after all usages are changed.
  6. """
  7. import os
  8. import sys
  9. from typing import List
  10. from pip._vendor import platformdirs as _appdirs
  11. def user_cache_dir(appname: str) -> str:
  12. return _appdirs.user_cache_dir(appname, appauthor=False)
  13. def _macos_user_config_dir(appname: str, roaming: bool = True) -> str:
  14. # Use ~/Application Support/pip, if the directory exists.
  15. path = _appdirs.user_data_dir(appname, appauthor=False, roaming=roaming)
  16. if os.path.isdir(path):
  17. return path
  18. # Use a Linux-like ~/.config/pip, by default.
  19. linux_like_path = "~/.config/"
  20. if appname:
  21. linux_like_path = os.path.join(linux_like_path, appname)
  22. return os.path.expanduser(linux_like_path)
  23. def user_config_dir(appname: str, roaming: bool = True) -> str:
  24. if sys.platform == "darwin":
  25. return _macos_user_config_dir(appname, roaming)
  26. return _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
  27. # for the discussion regarding site_config_dir locations
  28. # see <https://github.com/pypa/pip/issues/1733>
  29. def site_config_dirs(appname: str) -> List[str]:
  30. if sys.platform == "darwin":
  31. return [_appdirs.site_data_dir(appname, appauthor=False, multipath=True)]
  32. dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
  33. if sys.platform == "win32":
  34. return [dirval]
  35. # Unix-y system. Look in /etc as well.
  36. return dirval.split(os.pathsep) + ["/etc"]