base.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import functools
  2. import os
  3. import site
  4. import sys
  5. import sysconfig
  6. import typing
  7. from pip._internal.utils import appdirs
  8. from pip._internal.utils.virtualenv import running_under_virtualenv
  9. # Application Directories
  10. USER_CACHE_DIR = appdirs.user_cache_dir("pip")
  11. # FIXME doesn't account for venv linked to global site-packages
  12. site_packages: typing.Optional[str] = sysconfig.get_path("purelib")
  13. def get_major_minor_version() -> str:
  14. """
  15. Return the major-minor version of the current Python as a string, e.g.
  16. "3.7" or "3.10".
  17. """
  18. return "{}.{}".format(*sys.version_info)
  19. def get_src_prefix() -> str:
  20. if running_under_virtualenv():
  21. src_prefix = os.path.join(sys.prefix, "src")
  22. else:
  23. # FIXME: keep src in cwd for now (it is not a temporary folder)
  24. try:
  25. src_prefix = os.path.join(os.getcwd(), "src")
  26. except OSError:
  27. # In case the current working directory has been renamed or deleted
  28. sys.exit("The folder you are executing pip from can no longer be found.")
  29. # under macOS + virtualenv sys.prefix is not properly resolved
  30. # it is something like /path/to/python/bin/..
  31. return os.path.abspath(src_prefix)
  32. try:
  33. # Use getusersitepackages if this is present, as it ensures that the
  34. # value is initialised properly.
  35. user_site: typing.Optional[str] = site.getusersitepackages()
  36. except AttributeError:
  37. user_site = site.USER_SITE
  38. @functools.lru_cache(maxsize=None)
  39. def is_osx_framework() -> bool:
  40. return bool(sysconfig.get_config_var("PYTHONFRAMEWORK"))