python (3.11.7)

(root)/
lib/
python3.11/
site-packages/
pip/
_internal/
cli/
main.py
       1  """Primary application entrypoint.
       2  """
       3  import locale
       4  import logging
       5  import os
       6  import sys
       7  import warnings
       8  from typing import List, Optional
       9  
      10  from pip._internal.cli.autocompletion import autocomplete
      11  from pip._internal.cli.main_parser import parse_command
      12  from pip._internal.commands import create_command
      13  from pip._internal.exceptions import PipError
      14  from pip._internal.utils import deprecation
      15  
      16  logger = logging.getLogger(__name__)
      17  
      18  
      19  # Do not import and use main() directly! Using it directly is actively
      20  # discouraged by pip's maintainers. The name, location and behavior of
      21  # this function is subject to change, so calling it directly is not
      22  # portable across different pip versions.
      23  
      24  # In addition, running pip in-process is unsupported and unsafe. This is
      25  # elaborated in detail at
      26  # https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.
      27  # That document also provides suggestions that should work for nearly
      28  # all users that are considering importing and using main() directly.
      29  
      30  # However, we know that certain users will still want to invoke pip
      31  # in-process. If you understand and accept the implications of using pip
      32  # in an unsupported manner, the best approach is to use runpy to avoid
      33  # depending on the exact location of this entry point.
      34  
      35  # The following example shows how to use runpy to invoke pip in that
      36  # case:
      37  #
      38  #     sys.argv = ["pip", your, args, here]
      39  #     runpy.run_module("pip", run_name="__main__")
      40  #
      41  # Note that this will exit the process after running, unlike a direct
      42  # call to main. As it is not safe to do any processing after calling
      43  # main, this should not be an issue in practice.
      44  
      45  
      46  def main(args: Optional[List[str]] = None) -> int:
      47      if args is None:
      48          args = sys.argv[1:]
      49  
      50      # Suppress the pkg_resources deprecation warning
      51      # Note - we use a module of .*pkg_resources to cover
      52      # the normal case (pip._vendor.pkg_resources) and the
      53      # devendored case (a bare pkg_resources)
      54      warnings.filterwarnings(
      55          action="ignore", category=DeprecationWarning, module=".*pkg_resources"
      56      )
      57  
      58      # Configure our deprecation warnings to be sent through loggers
      59      deprecation.install_warning_logger()
      60  
      61      autocomplete()
      62  
      63      try:
      64          cmd_name, cmd_args = parse_command(args)
      65      except PipError as exc:
      66          sys.stderr.write(f"ERROR: {exc}")
      67          sys.stderr.write(os.linesep)
      68          sys.exit(1)
      69  
      70      # Needed for locale.getpreferredencoding(False) to work
      71      # in pip._internal.utils.encoding.auto_decode
      72      try:
      73          locale.setlocale(locale.LC_ALL, "")
      74      except locale.Error as e:
      75          # setlocale can apparently crash if locale are uninitialized
      76          logger.debug("Ignoring error %s when setting locale", e)
      77      command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
      78  
      79      return command.main(cmd_args)