wrappers.py 11 KB

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