图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

  1. import functools
  2. import os
  3. import site
  4. import sys
  5. import sysconfig
  6. import typing
  7. from pip._internal.exceptions import InstallationError
  8. from pip._internal.utils import appdirs
  9. from pip._internal.utils.virtualenv import running_under_virtualenv
  10. # Application Directories
  11. USER_CACHE_DIR = appdirs.user_cache_dir("pip")
  12. # FIXME doesn't account for venv linked to global site-packages
  13. site_packages: str = sysconfig.get_path("purelib")
  14. def get_major_minor_version() -> str:
  15. """
  16. Return the major-minor version of the current Python as a string, e.g.
  17. "3.7" or "3.10".
  18. """
  19. return "{}.{}".format(*sys.version_info)
  20. def change_root(new_root: str, pathname: str) -> str:
  21. """Return 'pathname' with 'new_root' prepended.
  22. If 'pathname' is relative, this is equivalent to os.path.join(new_root, pathname).
  23. Otherwise, it requires making 'pathname' relative and then joining the
  24. two, which is tricky on DOS/Windows and Mac OS.
  25. This is borrowed from Python's standard library's distutils module.
  26. """
  27. if os.name == "posix":
  28. if not os.path.isabs(pathname):
  29. return os.path.join(new_root, pathname)
  30. else:
  31. return os.path.join(new_root, pathname[1:])
  32. elif os.name == "nt":
  33. (drive, path) = os.path.splitdrive(pathname)
  34. if path[0] == "\\":
  35. path = path[1:]
  36. return os.path.join(new_root, path)
  37. else:
  38. raise InstallationError(
  39. f"Unknown platform: {os.name}\n"
  40. "Can not change root path prefix on unknown platform."
  41. )
  42. def get_src_prefix() -> str:
  43. if running_under_virtualenv():
  44. src_prefix = os.path.join(sys.prefix, "src")
  45. else:
  46. # FIXME: keep src in cwd for now (it is not a temporary folder)
  47. try:
  48. src_prefix = os.path.join(os.getcwd(), "src")
  49. except OSError:
  50. # In case the current working directory has been renamed or deleted
  51. sys.exit("The folder you are executing pip from can no longer be found.")
  52. # under macOS + virtualenv sys.prefix is not properly resolved
  53. # it is something like /path/to/python/bin/..
  54. return os.path.abspath(src_prefix)
  55. try:
  56. # Use getusersitepackages if this is present, as it ensures that the
  57. # value is initialised properly.
  58. user_site: typing.Optional[str] = site.getusersitepackages()
  59. except AttributeError:
  60. user_site = site.USER_SITE
  61. @functools.lru_cache(maxsize=None)
  62. def is_osx_framework() -> bool:
  63. return bool(sysconfig.get_config_var("PYTHONFRAMEWORK"))