1
0

envbuild.py 5.9 KB

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