setuptools_build.py 4.3 KB

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