python (3.11.7)

(root)/
lib/
python3.11/
site-packages/
pip/
_internal/
utils/
setuptools_build.py
       1  import sys
       2  import textwrap
       3  from typing import List, Optional, Sequence
       4  
       5  # Shim to wrap setup.py invocation with setuptools
       6  # Note that __file__ is handled via two {!r} *and* %r, to ensure that paths on
       7  # Windows are correctly handled (it should be "C:\\Users" not "C:\Users").
       8  _SETUPTOOLS_SHIM = textwrap.dedent(
       9      """
      10      exec(compile('''
      11      # This is <pip-setuptools-caller> -- a caller that pip uses to run setup.py
      12      #
      13      # - It imports setuptools before invoking setup.py, to enable projects that directly
      14      #   import from `distutils.core` to work with newer packaging standards.
      15      # - It provides a clear error message when setuptools is not installed.
      16      # - It sets `sys.argv[0]` to the underlying `setup.py`, when invoking `setup.py` so
      17      #   setuptools doesn't think the script is `-c`. This avoids the following warning:
      18      #     manifest_maker: standard file '-c' not found".
      19      # - It generates a shim setup.py, for handling setup.cfg-only projects.
      20      import os, sys, tokenize
      21  
      22      try:
      23          import setuptools
      24      except ImportError as error:
      25          print(
      26              "ERROR: Can not execute `setup.py` since setuptools is not available in "
      27              "the build environment.",
      28              file=sys.stderr,
      29          )
      30          sys.exit(1)
      31  
      32      __file__ = %r
      33      sys.argv[0] = __file__
      34  
      35      if os.path.exists(__file__):
      36          filename = __file__
      37          with tokenize.open(__file__) as f:
      38              setup_py_code = f.read()
      39      else:
      40          filename = "<auto-generated setuptools caller>"
      41          setup_py_code = "from setuptools import setup; setup()"
      42  
      43      exec(compile(setup_py_code, filename, "exec"))
      44      ''' % ({!r},), "<pip-setuptools-caller>", "exec"))
      45      """
      46  ).rstrip()
      47  
      48  
      49  def make_setuptools_shim_args(
      50      setup_py_path: str,
      51      global_options: Optional[Sequence[str]] = None,
      52      no_user_config: bool = False,
      53      unbuffered_output: bool = False,
      54  ) -> List[str]:
      55      """
      56      Get setuptools command arguments with shim wrapped setup file invocation.
      57  
      58      :param setup_py_path: The path to setup.py to be wrapped.
      59      :param global_options: Additional global options.
      60      :param no_user_config: If True, disables personal user configuration.
      61      :param unbuffered_output: If True, adds the unbuffered switch to the
      62       argument list.
      63      """
      64      args = [sys.executable]
      65      if unbuffered_output:
      66          args += ["-u"]
      67      args += ["-c", _SETUPTOOLS_SHIM.format(setup_py_path)]
      68      if global_options:
      69          args += global_options
      70      if no_user_config:
      71          args += ["--no-user-cfg"]
      72      return args
      73  
      74  
      75  def make_setuptools_bdist_wheel_args(
      76      setup_py_path: str,
      77      global_options: Sequence[str],
      78      build_options: Sequence[str],
      79      destination_dir: str,
      80  ) -> List[str]:
      81      # NOTE: Eventually, we'd want to also -S to the flags here, when we're
      82      # isolating. Currently, it breaks Python in virtualenvs, because it
      83      # relies on site.py to find parts of the standard library outside the
      84      # virtualenv.
      85      args = make_setuptools_shim_args(
      86          setup_py_path, global_options=global_options, unbuffered_output=True
      87      )
      88      args += ["bdist_wheel", "-d", destination_dir]
      89      args += build_options
      90      return args
      91  
      92  
      93  def make_setuptools_clean_args(
      94      setup_py_path: str,
      95      global_options: Sequence[str],
      96  ) -> List[str]:
      97      args = make_setuptools_shim_args(
      98          setup_py_path, global_options=global_options, unbuffered_output=True
      99      )
     100      args += ["clean", "--all"]
     101      return args
     102  
     103  
     104  def make_setuptools_develop_args(
     105      setup_py_path: str,
     106      *,
     107      global_options: Sequence[str],
     108      no_user_config: bool,
     109      prefix: Optional[str],
     110      home: Optional[str],
     111      use_user_site: bool,
     112  ) -> List[str]:
     113      assert not (use_user_site and prefix)
     114  
     115      args = make_setuptools_shim_args(
     116          setup_py_path,
     117          global_options=global_options,
     118          no_user_config=no_user_config,
     119      )
     120  
     121      args += ["develop", "--no-deps"]
     122  
     123      if prefix:
     124          args += ["--prefix", prefix]
     125      if home is not None:
     126          args += ["--install-dir", home]
     127  
     128      if use_user_site:
     129          args += ["--user", "--prefix="]
     130  
     131      return args
     132  
     133  
     134  def make_setuptools_egg_info_args(
     135      setup_py_path: str,
     136      egg_info_dir: Optional[str],
     137      no_user_config: bool,
     138  ) -> List[str]:
     139      args = make_setuptools_shim_args(setup_py_path, no_user_config=no_user_config)
     140  
     141      args += ["egg_info"]
     142  
     143      if egg_info_dir:
     144          args += ["--egg-base", egg_info_dir]
     145  
     146      return args