_impl.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import json
  2. import os
  3. import sys
  4. import tempfile
  5. from contextlib import contextmanager
  6. from os.path import abspath
  7. from os.path import join as pjoin
  8. from subprocess import STDOUT, check_call, check_output
  9. from ._in_process import _in_proc_script_path
  10. def write_json(obj, path, **kwargs):
  11. with open(path, 'w', encoding='utf-8') as f:
  12. json.dump(obj, f, **kwargs)
  13. def read_json(path):
  14. with open(path, encoding='utf-8') as f:
  15. return json.load(f)
  16. class BackendUnavailable(Exception):
  17. """Will be raised if the backend cannot be imported in the hook process."""
  18. def __init__(self, traceback):
  19. self.traceback = traceback
  20. class BackendInvalid(Exception):
  21. """Will be raised if the backend is invalid."""
  22. def __init__(self, backend_name, backend_path, message):
  23. super().__init__(message)
  24. self.backend_name = backend_name
  25. self.backend_path = backend_path
  26. class HookMissing(Exception):
  27. """Will be raised on missing hooks (if a fallback can't be used)."""
  28. def __init__(self, hook_name):
  29. super().__init__(hook_name)
  30. self.hook_name = hook_name
  31. class UnsupportedOperation(Exception):
  32. """May be raised by build_sdist if the backend indicates that it can't."""
  33. def __init__(self, traceback):
  34. self.traceback = traceback
  35. def default_subprocess_runner(cmd, cwd=None, extra_environ=None):
  36. """The default method of calling the wrapper subprocess.
  37. This uses :func:`subprocess.check_call` under the hood.
  38. """
  39. env = os.environ.copy()
  40. if extra_environ:
  41. env.update(extra_environ)
  42. check_call(cmd, cwd=cwd, env=env)
  43. def quiet_subprocess_runner(cmd, cwd=None, extra_environ=None):
  44. """Call the subprocess while suppressing output.
  45. This uses :func:`subprocess.check_output` under the hood.
  46. """
  47. env = os.environ.copy()
  48. if extra_environ:
  49. env.update(extra_environ)
  50. check_output(cmd, cwd=cwd, env=env, stderr=STDOUT)
  51. def norm_and_check(source_tree, requested):
  52. """Normalise and check a backend path.
  53. Ensure that the requested backend path is specified as a relative path,
  54. and resolves to a location under the given source tree.
  55. Return an absolute version of the requested path.
  56. """
  57. if os.path.isabs(requested):
  58. raise ValueError("paths must be relative")
  59. abs_source = os.path.abspath(source_tree)
  60. abs_requested = os.path.normpath(os.path.join(abs_source, requested))
  61. # We have to use commonprefix for Python 2.7 compatibility. So we
  62. # normalise case to avoid problems because commonprefix is a character
  63. # based comparison :-(
  64. norm_source = os.path.normcase(abs_source)
  65. norm_requested = os.path.normcase(abs_requested)
  66. if os.path.commonprefix([norm_source, norm_requested]) != norm_source:
  67. raise ValueError("paths must be inside source tree")
  68. return abs_requested
  69. class BuildBackendHookCaller:
  70. """A wrapper to call the build backend hooks for a source directory.
  71. """
  72. def __init__(
  73. self,
  74. source_dir,
  75. build_backend,
  76. backend_path=None,
  77. runner=None,
  78. python_executable=None,
  79. ):
  80. """
  81. :param source_dir: The source directory to invoke the build backend for
  82. :param build_backend: The build backend spec
  83. :param backend_path: Additional path entries for the build backend spec
  84. :param runner: The :ref:`subprocess runner <Subprocess Runners>` to use
  85. :param python_executable:
  86. The Python executable used to invoke the build backend
  87. """
  88. if runner is None:
  89. runner = default_subprocess_runner
  90. self.source_dir = abspath(source_dir)
  91. self.build_backend = build_backend
  92. if backend_path:
  93. backend_path = [
  94. norm_and_check(self.source_dir, p) for p in backend_path
  95. ]
  96. self.backend_path = backend_path
  97. self._subprocess_runner = runner
  98. if not python_executable:
  99. python_executable = sys.executable
  100. self.python_executable = python_executable
  101. @contextmanager
  102. def subprocess_runner(self, runner):
  103. """A context manager for temporarily overriding the default
  104. :ref:`subprocess runner <Subprocess Runners>`.
  105. .. code-block:: python
  106. hook_caller = BuildBackendHookCaller(...)
  107. with hook_caller.subprocess_runner(quiet_subprocess_runner):
  108. ...
  109. """
  110. prev = self._subprocess_runner
  111. self._subprocess_runner = runner
  112. try:
  113. yield
  114. finally:
  115. self._subprocess_runner = prev
  116. def _supported_features(self):
  117. """Return the list of optional features supported by the backend."""
  118. return self._call_hook('_supported_features', {})
  119. def get_requires_for_build_wheel(self, config_settings=None):
  120. """Get additional dependencies required for building a wheel.
  121. :returns: A list of :pep:`dependency specifiers <508>`.
  122. :rtype: list[str]
  123. .. admonition:: Fallback
  124. If the build backend does not defined a hook with this name, an
  125. empty list will be returned.
  126. """
  127. return self._call_hook('get_requires_for_build_wheel', {
  128. 'config_settings': config_settings
  129. })
  130. def prepare_metadata_for_build_wheel(
  131. self, metadata_directory, config_settings=None,
  132. _allow_fallback=True):
  133. """Prepare a ``*.dist-info`` folder with metadata for this project.
  134. :returns: Name of the newly created subfolder within
  135. ``metadata_directory``, containing the metadata.
  136. :rtype: str
  137. .. admonition:: Fallback
  138. If the build backend does not define a hook with this name and
  139. ``_allow_fallback`` is truthy, the backend will be asked to build a
  140. wheel via the ``build_wheel`` hook and the dist-info extracted from
  141. that will be returned.
  142. """
  143. return self._call_hook('prepare_metadata_for_build_wheel', {
  144. 'metadata_directory': abspath(metadata_directory),
  145. 'config_settings': config_settings,
  146. '_allow_fallback': _allow_fallback,
  147. })
  148. def build_wheel(
  149. self, wheel_directory, config_settings=None,
  150. metadata_directory=None):
  151. """Build a wheel from this project.
  152. :returns:
  153. The name of the newly created wheel within ``wheel_directory``.
  154. .. admonition:: Interaction with fallback
  155. If the ``build_wheel`` hook was called in the fallback for
  156. :meth:`prepare_metadata_for_build_wheel`, the build backend would
  157. not be invoked. Instead, the previously built wheel will be copied
  158. to ``wheel_directory`` and the name of that file will be returned.
  159. """
  160. if metadata_directory is not None:
  161. metadata_directory = abspath(metadata_directory)
  162. return self._call_hook('build_wheel', {
  163. 'wheel_directory': abspath(wheel_directory),
  164. 'config_settings': config_settings,
  165. 'metadata_directory': metadata_directory,
  166. })
  167. def get_requires_for_build_editable(self, config_settings=None):
  168. """Get additional dependencies required for building an editable wheel.
  169. :returns: A list of :pep:`dependency specifiers <508>`.
  170. :rtype: list[str]
  171. .. admonition:: Fallback
  172. If the build backend does not defined a hook with this name, an
  173. empty list will be returned.
  174. """
  175. return self._call_hook('get_requires_for_build_editable', {
  176. 'config_settings': config_settings
  177. })
  178. def prepare_metadata_for_build_editable(
  179. self, metadata_directory, config_settings=None,
  180. _allow_fallback=True):
  181. """Prepare a ``*.dist-info`` folder with metadata for this project.
  182. :returns: Name of the newly created subfolder within
  183. ``metadata_directory``, containing the metadata.
  184. :rtype: str
  185. .. admonition:: Fallback
  186. If the build backend does not define a hook with this name and
  187. ``_allow_fallback`` is truthy, the backend will be asked to build a
  188. wheel via the ``build_editable`` hook and the dist-info
  189. extracted from that will be returned.
  190. """
  191. return self._call_hook('prepare_metadata_for_build_editable', {
  192. 'metadata_directory': abspath(metadata_directory),
  193. 'config_settings': config_settings,
  194. '_allow_fallback': _allow_fallback,
  195. })
  196. def build_editable(
  197. self, wheel_directory, config_settings=None,
  198. metadata_directory=None):
  199. """Build an editable wheel from this project.
  200. :returns:
  201. The name of the newly created wheel within ``wheel_directory``.
  202. .. admonition:: Interaction with fallback
  203. If the ``build_editable`` hook was called in the fallback for
  204. :meth:`prepare_metadata_for_build_editable`, the build backend
  205. would not be invoked. Instead, the previously built wheel will be
  206. copied to ``wheel_directory`` and the name of that file will be
  207. returned.
  208. """
  209. if metadata_directory is not None:
  210. metadata_directory = abspath(metadata_directory)
  211. return self._call_hook('build_editable', {
  212. 'wheel_directory': abspath(wheel_directory),
  213. 'config_settings': config_settings,
  214. 'metadata_directory': metadata_directory,
  215. })
  216. def get_requires_for_build_sdist(self, config_settings=None):
  217. """Get additional dependencies required for building an sdist.
  218. :returns: A list of :pep:`dependency specifiers <508>`.
  219. :rtype: list[str]
  220. """
  221. return self._call_hook('get_requires_for_build_sdist', {
  222. 'config_settings': config_settings
  223. })
  224. def build_sdist(self, sdist_directory, config_settings=None):
  225. """Build an sdist from this project.
  226. :returns:
  227. The name of the newly created sdist within ``wheel_directory``.
  228. """
  229. return self._call_hook('build_sdist', {
  230. 'sdist_directory': abspath(sdist_directory),
  231. 'config_settings': config_settings,
  232. })
  233. def _call_hook(self, hook_name, kwargs):
  234. extra_environ = {'PEP517_BUILD_BACKEND': self.build_backend}
  235. if self.backend_path:
  236. backend_path = os.pathsep.join(self.backend_path)
  237. extra_environ['PEP517_BACKEND_PATH'] = backend_path
  238. with tempfile.TemporaryDirectory() as td:
  239. hook_input = {'kwargs': kwargs}
  240. write_json(hook_input, pjoin(td, 'input.json'), indent=2)
  241. # Run the hook in a subprocess
  242. with _in_proc_script_path() as script:
  243. python = self.python_executable
  244. self._subprocess_runner(
  245. [python, abspath(str(script)), hook_name, td],
  246. cwd=self.source_dir,
  247. extra_environ=extra_environ
  248. )
  249. data = read_json(pjoin(td, 'output.json'))
  250. if data.get('unsupported'):
  251. raise UnsupportedOperation(data.get('traceback', ''))
  252. if data.get('no_backend'):
  253. raise BackendUnavailable(data.get('traceback', ''))
  254. if data.get('backend_invalid'):
  255. raise BackendInvalid(
  256. backend_name=self.build_backend,
  257. backend_path=self.backend_path,
  258. message=data.get('backend_error', '')
  259. )
  260. if data.get('hook_missing'):
  261. raise HookMissing(data.get('missing_hook_name') or hook_name)
  262. return data['return_val']