envbuild.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """Build wheels/sdists by installing build deps to a temporary environment.
  2. """
  3. import io
  4. import os
  5. import logging
  6. import shutil
  7. from subprocess import check_call
  8. import sys
  9. from sysconfig import get_paths
  10. from tempfile import mkdtemp
  11. from .compat import toml_load
  12. from .wrappers import Pep517HookCaller, LoggerWrapper
  13. log = logging.getLogger(__name__)
  14. def _load_pyproject(source_dir):
  15. with io.open(
  16. os.path.join(source_dir, 'pyproject.toml'),
  17. 'rb',
  18. ) as f:
  19. pyproject_data = toml_load(f)
  20. buildsys = pyproject_data['build-system']
  21. return (
  22. buildsys['requires'],
  23. buildsys['build-backend'],
  24. buildsys.get('backend-path'),
  25. )
  26. class BuildEnvironment(object):
  27. """Context manager to install build deps in a simple temporary environment
  28. Based on code I wrote for pip, which is MIT licensed.
  29. """
  30. # Copyright (c) 2008-2016 The pip developers (see AUTHORS.txt file)
  31. #
  32. # Permission is hereby granted, free of charge, to any person obtaining
  33. # a copy of this software and associated documentation files (the
  34. # "Software"), to deal in the Software without restriction, including
  35. # without limitation the rights to use, copy, modify, merge, publish,
  36. # distribute, sublicense, and/or sell copies of the Software, and to
  37. # permit persons to whom the Software is furnished to do so, subject to
  38. # the following conditions:
  39. #
  40. # The above copyright notice and this permission notice shall be
  41. # included in all copies or substantial portions of the Software.
  42. #
  43. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  44. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  45. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  46. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  47. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  48. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  49. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  50. path = None
  51. def __init__(self, cleanup=True):
  52. self._cleanup = cleanup
  53. def __enter__(self):
  54. self.path = mkdtemp(prefix='pep517-build-env-')
  55. log.info('Temporary build environment: %s', self.path)
  56. self.save_path = os.environ.get('PATH', None)
  57. self.save_pythonpath = os.environ.get('PYTHONPATH', None)
  58. install_scheme = 'nt' if (os.name == 'nt') else 'posix_prefix'
  59. install_dirs = get_paths(install_scheme, vars={
  60. 'base': self.path,
  61. 'platbase': self.path,
  62. })
  63. scripts = install_dirs['scripts']
  64. if self.save_path:
  65. os.environ['PATH'] = scripts + os.pathsep + self.save_path
  66. else:
  67. os.environ['PATH'] = scripts + os.pathsep + os.defpath
  68. if install_dirs['purelib'] == install_dirs['platlib']:
  69. lib_dirs = install_dirs['purelib']
  70. else:
  71. lib_dirs = install_dirs['purelib'] + os.pathsep + \
  72. install_dirs['platlib']
  73. if self.save_pythonpath:
  74. os.environ['PYTHONPATH'] = lib_dirs + os.pathsep + \
  75. self.save_pythonpath
  76. else:
  77. os.environ['PYTHONPATH'] = lib_dirs
  78. return self
  79. def pip_install(self, reqs):
  80. """Install dependencies into this env by calling pip in a subprocess"""
  81. if not reqs:
  82. return
  83. log.info('Calling pip to install %s', reqs)
  84. cmd = [
  85. sys.executable, '-m', 'pip', 'install', '--ignore-installed',
  86. '--prefix', self.path] + list(reqs)
  87. check_call(
  88. cmd,
  89. stdout=LoggerWrapper(log, logging.INFO),
  90. stderr=LoggerWrapper(log, logging.ERROR),
  91. )
  92. def __exit__(self, exc_type, exc_val, exc_tb):
  93. needs_cleanup = (
  94. self._cleanup and
  95. self.path is not None and
  96. os.path.isdir(self.path)
  97. )
  98. if needs_cleanup:
  99. shutil.rmtree(self.path)
  100. if self.save_path is None:
  101. os.environ.pop('PATH', None)
  102. else:
  103. os.environ['PATH'] = self.save_path
  104. if self.save_pythonpath is None:
  105. os.environ.pop('PYTHONPATH', None)
  106. else:
  107. os.environ['PYTHONPATH'] = self.save_pythonpath
  108. def build_wheel(source_dir, wheel_dir, config_settings=None):
  109. """Build a wheel from a source directory using PEP 517 hooks.
  110. :param str source_dir: Source directory containing pyproject.toml
  111. :param str wheel_dir: Target directory to create wheel in
  112. :param dict config_settings: Options to pass to build backend
  113. This is a blocking function which will run pip in a subprocess to install
  114. build requirements.
  115. """
  116. if config_settings is None:
  117. config_settings = {}
  118. requires, backend, backend_path = _load_pyproject(source_dir)
  119. hooks = Pep517HookCaller(source_dir, backend, backend_path)
  120. with BuildEnvironment() as env:
  121. env.pip_install(requires)
  122. reqs = hooks.get_requires_for_build_wheel(config_settings)
  123. env.pip_install(reqs)
  124. return hooks.build_wheel(wheel_dir, config_settings)
  125. def build_sdist(source_dir, sdist_dir, config_settings=None):
  126. """Build an sdist from a source directory using PEP 517 hooks.
  127. :param str source_dir: Source directory containing pyproject.toml
  128. :param str sdist_dir: Target directory to place sdist in
  129. :param dict config_settings: Options to pass to build backend
  130. This is a blocking function which will run pip in a subprocess to install
  131. build requirements.
  132. """
  133. if config_settings is None:
  134. config_settings = {}
  135. requires, backend, backend_path = _load_pyproject(source_dir)
  136. hooks = Pep517HookCaller(source_dir, backend, backend_path)
  137. with BuildEnvironment() as env:
  138. env.pip_install(requires)
  139. reqs = hooks.get_requires_for_build_sdist(config_settings)
  140. env.pip_install(reqs)
  141. return hooks.build_sdist(sdist_dir, config_settings)