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