wrappers.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import threading
  2. from contextlib import contextmanager
  3. import os
  4. from os.path import abspath, join as pjoin
  5. import shutil
  6. from subprocess import check_call, check_output, STDOUT
  7. import sys
  8. from tempfile import mkdtemp
  9. from . import compat
  10. from .in_process import _in_proc_script_path
  11. __all__ = [
  12. 'BackendUnavailable',
  13. 'BackendInvalid',
  14. 'HookMissing',
  15. 'UnsupportedOperation',
  16. 'default_subprocess_runner',
  17. 'quiet_subprocess_runner',
  18. 'Pep517HookCaller',
  19. ]
  20. @contextmanager
  21. def tempdir():
  22. td = mkdtemp()
  23. try:
  24. yield td
  25. finally:
  26. shutil.rmtree(td)
  27. class BackendUnavailable(Exception):
  28. """Will be raised if the backend cannot be imported in the hook process."""
  29. def __init__(self, traceback):
  30. self.traceback = traceback
  31. class BackendInvalid(Exception):
  32. """Will be raised if the backend is invalid."""
  33. def __init__(self, backend_name, backend_path, message):
  34. self.backend_name = backend_name
  35. self.backend_path = backend_path
  36. self.message = message
  37. class HookMissing(Exception):
  38. """Will be raised on missing hooks."""
  39. def __init__(self, hook_name):
  40. super(HookMissing, self).__init__(hook_name)
  41. self.hook_name = hook_name
  42. class UnsupportedOperation(Exception):
  43. """May be raised by build_sdist if the backend indicates that it can't."""
  44. def __init__(self, traceback):
  45. self.traceback = traceback
  46. def default_subprocess_runner(cmd, cwd=None, extra_environ=None):
  47. """The default method of calling the wrapper subprocess."""
  48. env = os.environ.copy()
  49. if extra_environ:
  50. env.update(extra_environ)
  51. check_call(cmd, cwd=cwd, env=env)
  52. def quiet_subprocess_runner(cmd, cwd=None, extra_environ=None):
  53. """A method of calling the wrapper subprocess while suppressing output."""
  54. env = os.environ.copy()
  55. if extra_environ:
  56. env.update(extra_environ)
  57. check_output(cmd, cwd=cwd, env=env, stderr=STDOUT)
  58. def norm_and_check(source_tree, requested):
  59. """Normalise and check a backend path.
  60. Ensure that the requested backend path is specified as a relative path,
  61. and resolves to a location under the given source tree.
  62. Return an absolute version of the requested path.
  63. """
  64. if os.path.isabs(requested):
  65. raise ValueError("paths must be relative")
  66. abs_source = os.path.abspath(source_tree)
  67. abs_requested = os.path.normpath(os.path.join(abs_source, requested))
  68. # We have to use commonprefix for Python 2.7 compatibility. So we
  69. # normalise case to avoid problems because commonprefix is a character
  70. # based comparison :-(
  71. norm_source = os.path.normcase(abs_source)
  72. norm_requested = os.path.normcase(abs_requested)
  73. if os.path.commonprefix([norm_source, norm_requested]) != norm_source:
  74. raise ValueError("paths must be inside source tree")
  75. return abs_requested
  76. class Pep517HookCaller(object):
  77. """A wrapper around a source directory to be built with a PEP 517 backend.
  78. :param source_dir: The path to the source directory, containing
  79. pyproject.toml.
  80. :param build_backend: The build backend spec, as per PEP 517, from
  81. pyproject.toml.
  82. :param backend_path: The backend path, as per PEP 517, from pyproject.toml.
  83. :param runner: A callable that invokes the wrapper subprocess.
  84. :param python_executable: The Python executable used to invoke the backend
  85. The 'runner', if provided, must expect the following:
  86. - cmd: a list of strings representing the command and arguments to
  87. execute, as would be passed to e.g. 'subprocess.check_call'.
  88. - cwd: a string representing the working directory that must be
  89. used for the subprocess. Corresponds to the provided source_dir.
  90. - extra_environ: a dict mapping environment variable names to values
  91. which must be set for the subprocess execution.
  92. """
  93. def __init__(
  94. self,
  95. source_dir,
  96. build_backend,
  97. backend_path=None,
  98. runner=None,
  99. python_executable=None,
  100. ):
  101. if runner is None:
  102. runner = default_subprocess_runner
  103. self.source_dir = abspath(source_dir)
  104. self.build_backend = build_backend
  105. if backend_path:
  106. backend_path = [
  107. norm_and_check(self.source_dir, p) for p in backend_path
  108. ]
  109. self.backend_path = backend_path
  110. self._subprocess_runner = runner
  111. if not python_executable:
  112. python_executable = sys.executable
  113. self.python_executable = python_executable
  114. @contextmanager
  115. def subprocess_runner(self, runner):
  116. """A context manager for temporarily overriding the default subprocess
  117. runner.
  118. """
  119. prev = self._subprocess_runner
  120. self._subprocess_runner = runner
  121. try:
  122. yield
  123. finally:
  124. self._subprocess_runner = prev
  125. def get_requires_for_build_wheel(self, config_settings=None):
  126. """Identify packages required for building a wheel
  127. Returns a list of dependency specifications, e.g.::
  128. ["wheel >= 0.25", "setuptools"]
  129. This does not include requirements specified in pyproject.toml.
  130. It returns the result of calling the equivalently named hook in a
  131. subprocess.
  132. """
  133. return self._call_hook('get_requires_for_build_wheel', {
  134. 'config_settings': config_settings
  135. })
  136. def prepare_metadata_for_build_wheel(
  137. self, metadata_directory, config_settings=None,
  138. _allow_fallback=True):
  139. """Prepare a ``*.dist-info`` folder with metadata for this project.
  140. Returns the name of the newly created folder.
  141. If the build backend defines a hook with this name, it will be called
  142. in a subprocess. If not, the backend will be asked to build a wheel,
  143. and the dist-info extracted from that (unless _allow_fallback is
  144. False).
  145. """
  146. return self._call_hook('prepare_metadata_for_build_wheel', {
  147. 'metadata_directory': abspath(metadata_directory),
  148. 'config_settings': config_settings,
  149. '_allow_fallback': _allow_fallback,
  150. })
  151. def build_wheel(
  152. self, wheel_directory, config_settings=None,
  153. metadata_directory=None):
  154. """Build a wheel from this project.
  155. Returns the name of the newly created file.
  156. In general, this will call the 'build_wheel' hook in the backend.
  157. However, if that was previously called by
  158. 'prepare_metadata_for_build_wheel', and the same metadata_directory is
  159. used, the previously built wheel will be copied to wheel_directory.
  160. """
  161. if metadata_directory is not None:
  162. metadata_directory = abspath(metadata_directory)
  163. return self._call_hook('build_wheel', {
  164. 'wheel_directory': abspath(wheel_directory),
  165. 'config_settings': config_settings,
  166. 'metadata_directory': metadata_directory,
  167. })
  168. def get_requires_for_build_editable(self, config_settings=None):
  169. """Identify packages required for building an editable wheel
  170. Returns a list of dependency specifications, e.g.::
  171. ["wheel >= 0.25", "setuptools"]
  172. This does not include requirements specified in pyproject.toml.
  173. It returns the result of calling the equivalently named hook in a
  174. subprocess.
  175. """
  176. return self._call_hook('get_requires_for_build_editable', {
  177. 'config_settings': config_settings
  178. })
  179. def prepare_metadata_for_build_editable(
  180. self, metadata_directory, config_settings=None,
  181. _allow_fallback=True):
  182. """Prepare a ``*.dist-info`` folder with metadata for this project.
  183. Returns the name of the newly created folder.
  184. If the build backend defines a hook with this name, it will be called
  185. in a subprocess. If not, the backend will be asked to build an editable
  186. wheel, and the dist-info extracted from that (unless _allow_fallback is
  187. False).
  188. """
  189. return self._call_hook('prepare_metadata_for_build_editable', {
  190. 'metadata_directory': abspath(metadata_directory),
  191. 'config_settings': config_settings,
  192. '_allow_fallback': _allow_fallback,
  193. })
  194. def build_editable(
  195. self, wheel_directory, config_settings=None,
  196. metadata_directory=None):
  197. """Build an editable wheel from this project.
  198. Returns the name of the newly created file.
  199. In general, this will call the 'build_editable' hook in the backend.
  200. However, if that was previously called by
  201. 'prepare_metadata_for_build_editable', and the same metadata_directory
  202. is used, the previously built wheel will be copied to wheel_directory.
  203. """
  204. if metadata_directory is not None:
  205. metadata_directory = abspath(metadata_directory)
  206. return self._call_hook('build_editable', {
  207. 'wheel_directory': abspath(wheel_directory),
  208. 'config_settings': config_settings,
  209. 'metadata_directory': metadata_directory,
  210. })
  211. def get_requires_for_build_sdist(self, config_settings=None):
  212. """Identify packages required for building a wheel
  213. Returns a list of dependency specifications, e.g.::
  214. ["setuptools >= 26"]
  215. This does not include requirements specified in pyproject.toml.
  216. It returns the result of calling the equivalently named hook in a
  217. subprocess.
  218. """
  219. return self._call_hook('get_requires_for_build_sdist', {
  220. 'config_settings': config_settings
  221. })
  222. def build_sdist(self, sdist_directory, config_settings=None):
  223. """Build an sdist from this project.
  224. Returns the name of the newly created file.
  225. This calls the 'build_sdist' backend hook in a subprocess.
  226. """
  227. return self._call_hook('build_sdist', {
  228. 'sdist_directory': abspath(sdist_directory),
  229. 'config_settings': config_settings,
  230. })
  231. def _call_hook(self, hook_name, kwargs):
  232. # On Python 2, pytoml returns Unicode values (which is correct) but the
  233. # environment passed to check_call needs to contain string values. We
  234. # convert here by encoding using ASCII (the backend can only contain
  235. # letters, digits and _, . and : characters, and will be used as a
  236. # Python identifier, so non-ASCII content is wrong on Python 2 in
  237. # any case).
  238. # For backend_path, we use sys.getfilesystemencoding.
  239. if sys.version_info[0] == 2:
  240. build_backend = self.build_backend.encode('ASCII')
  241. else:
  242. build_backend = self.build_backend
  243. extra_environ = {'PEP517_BUILD_BACKEND': build_backend}
  244. if self.backend_path:
  245. backend_path = os.pathsep.join(self.backend_path)
  246. if sys.version_info[0] == 2:
  247. backend_path = backend_path.encode(sys.getfilesystemencoding())
  248. extra_environ['PEP517_BACKEND_PATH'] = backend_path
  249. with tempdir() as td:
  250. hook_input = {'kwargs': kwargs}
  251. compat.write_json(hook_input, pjoin(td, 'input.json'),
  252. indent=2)
  253. # Run the hook in a subprocess
  254. with _in_proc_script_path() as script:
  255. python = self.python_executable
  256. self._subprocess_runner(
  257. [python, abspath(str(script)), hook_name, td],
  258. cwd=self.source_dir,
  259. extra_environ=extra_environ
  260. )
  261. data = compat.read_json(pjoin(td, 'output.json'))
  262. if data.get('unsupported'):
  263. raise UnsupportedOperation(data.get('traceback', ''))
  264. if data.get('no_backend'):
  265. raise BackendUnavailable(data.get('traceback', ''))
  266. if data.get('backend_invalid'):
  267. raise BackendInvalid(
  268. backend_name=self.build_backend,
  269. backend_path=self.backend_path,
  270. message=data.get('backend_error', '')
  271. )
  272. if data.get('hook_missing'):
  273. raise HookMissing(data.get('missing_hook_name') or hook_name)
  274. return data['return_val']
  275. class LoggerWrapper(threading.Thread):
  276. """
  277. Read messages from a pipe and redirect them
  278. to a logger (see python's logging module).
  279. """
  280. def __init__(self, logger, level):
  281. threading.Thread.__init__(self)
  282. self.daemon = True
  283. self.logger = logger
  284. self.level = level
  285. # create the pipe and reader
  286. self.fd_read, self.fd_write = os.pipe()
  287. self.reader = os.fdopen(self.fd_read)
  288. self.start()
  289. def fileno(self):
  290. return self.fd_write
  291. @staticmethod
  292. def remove_newline(msg):
  293. return msg[:-1] if msg.endswith(os.linesep) else msg
  294. def run(self):
  295. for line in self.reader:
  296. self._write(self.remove_newline(line))
  297. def _write(self, message):
  298. self.logger.log(self.level, message)