python (3.11.7)
       1  import itertools
       2  import os
       3  import shutil
       4  import sys
       5  from typing import List, Optional
       6  
       7  from pip._internal.cli.main import main
       8  from pip._internal.utils.compat import WINDOWS
       9  
      10  _EXECUTABLE_NAMES = [
      11      "pip",
      12      f"pip{sys.version_info.major}",
      13      f"pip{sys.version_info.major}.{sys.version_info.minor}",
      14  ]
      15  if WINDOWS:
      16      _allowed_extensions = {"", ".exe"}
      17      _EXECUTABLE_NAMES = [
      18          "".join(parts)
      19          for parts in itertools.product(_EXECUTABLE_NAMES, _allowed_extensions)
      20      ]
      21  
      22  
      23  def _wrapper(args: Optional[List[str]] = None) -> int:
      24      """Central wrapper for all old entrypoints.
      25  
      26      Historically pip has had several entrypoints defined. Because of issues
      27      arising from PATH, sys.path, multiple Pythons, their interactions, and most
      28      of them having a pip installed, users suffer every time an entrypoint gets
      29      moved.
      30  
      31      To alleviate this pain, and provide a mechanism for warning users and
      32      directing them to an appropriate place for help, we now define all of
      33      our old entrypoints as wrappers for the current one.
      34      """
      35      sys.stderr.write(
      36          "WARNING: pip is being invoked by an old script wrapper. This will "
      37          "fail in a future version of pip.\n"
      38          "Please see https://github.com/pypa/pip/issues/5599 for advice on "
      39          "fixing the underlying issue.\n"
      40          "To avoid this problem you can invoke Python with '-m pip' instead of "
      41          "running pip directly.\n"
      42      )
      43      return main(args)
      44  
      45  
      46  def get_best_invocation_for_this_pip() -> str:
      47      """Try to figure out the best way to invoke pip in the current environment."""
      48      binary_directory = "Scripts" if WINDOWS else "bin"
      49      binary_prefix = os.path.join(sys.prefix, binary_directory)
      50  
      51      # Try to use pip[X[.Y]] names, if those executables for this environment are
      52      # the first on PATH with that name.
      53      path_parts = os.path.normcase(os.environ.get("PATH", "")).split(os.pathsep)
      54      exe_are_in_PATH = os.path.normcase(binary_prefix) in path_parts
      55      if exe_are_in_PATH:
      56          for exe_name in _EXECUTABLE_NAMES:
      57              found_executable = shutil.which(exe_name)
      58              binary_executable = os.path.join(binary_prefix, exe_name)
      59              if (
      60                  found_executable
      61                  and os.path.exists(binary_executable)
      62                  and os.path.samefile(
      63                      found_executable,
      64                      binary_executable,
      65                  )
      66              ):
      67                  return exe_name
      68  
      69      # Use the `-m` invocation, if there's no "nice" invocation.
      70      return f"{get_best_invocation_for_this_python()} -m pip"
      71  
      72  
      73  def get_best_invocation_for_this_python() -> str:
      74      """Try to figure out the best way to invoke the current Python."""
      75      exe = sys.executable
      76      exe_name = os.path.basename(exe)
      77  
      78      # Try to use the basename, if it's the first executable.
      79      found_executable = shutil.which(exe_name)
      80      if found_executable and os.path.samefile(found_executable, exe):
      81          return exe_name
      82  
      83      # Use the full executable name, because we couldn't find something simpler.
      84      return exe