wheel.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import logging
  4. import os
  5. import shutil
  6. from pip._internal.cache import WheelCache
  7. from pip._internal.cli import cmdoptions
  8. from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  9. from pip._internal.cli.status_codes import SUCCESS
  10. from pip._internal.exceptions import CommandError
  11. from pip._internal.req.req_tracker import get_requirement_tracker
  12. from pip._internal.utils.misc import ensure_dir, normalize_path
  13. from pip._internal.utils.temp_dir import TempDirectory
  14. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  15. from pip._internal.wheel_builder import build, should_build_for_wheel_command
  16. if MYPY_CHECK_RUNNING:
  17. from optparse import Values
  18. from typing import List
  19. logger = logging.getLogger(__name__)
  20. class WheelCommand(RequirementCommand):
  21. """
  22. Build Wheel archives for your requirements and dependencies.
  23. Wheel is a built-package format, and offers the advantage of not
  24. recompiling your software during every install. For more details, see the
  25. wheel docs: https://wheel.readthedocs.io/en/latest/
  26. Requirements: setuptools>=0.8, and wheel.
  27. 'pip wheel' uses the bdist_wheel setuptools extension from the wheel
  28. package to build individual wheels.
  29. """
  30. usage = """
  31. %prog [options] <requirement specifier> ...
  32. %prog [options] -r <requirements file> ...
  33. %prog [options] [-e] <vcs project url> ...
  34. %prog [options] [-e] <local project path> ...
  35. %prog [options] <archive url/path> ..."""
  36. def add_options(self):
  37. # type: () -> None
  38. self.cmd_opts.add_option(
  39. '-w', '--wheel-dir',
  40. dest='wheel_dir',
  41. metavar='dir',
  42. default=os.curdir,
  43. help=("Build wheels into <dir>, where the default is the "
  44. "current working directory."),
  45. )
  46. self.cmd_opts.add_option(cmdoptions.no_binary())
  47. self.cmd_opts.add_option(cmdoptions.only_binary())
  48. self.cmd_opts.add_option(cmdoptions.prefer_binary())
  49. self.cmd_opts.add_option(
  50. '--build-option',
  51. dest='build_options',
  52. metavar='options',
  53. action='append',
  54. help="Extra arguments to be supplied to 'setup.py bdist_wheel'.",
  55. )
  56. self.cmd_opts.add_option(cmdoptions.no_build_isolation())
  57. self.cmd_opts.add_option(cmdoptions.use_pep517())
  58. self.cmd_opts.add_option(cmdoptions.no_use_pep517())
  59. self.cmd_opts.add_option(cmdoptions.constraints())
  60. self.cmd_opts.add_option(cmdoptions.editable())
  61. self.cmd_opts.add_option(cmdoptions.requirements())
  62. self.cmd_opts.add_option(cmdoptions.src())
  63. self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
  64. self.cmd_opts.add_option(cmdoptions.no_deps())
  65. self.cmd_opts.add_option(cmdoptions.build_dir())
  66. self.cmd_opts.add_option(cmdoptions.progress_bar())
  67. self.cmd_opts.add_option(
  68. '--global-option',
  69. dest='global_options',
  70. action='append',
  71. metavar='options',
  72. help="Extra global options to be supplied to the setup.py "
  73. "call before the 'bdist_wheel' command.")
  74. self.cmd_opts.add_option(
  75. '--pre',
  76. action='store_true',
  77. default=False,
  78. help=("Include pre-release and development versions. By default, "
  79. "pip only finds stable versions."),
  80. )
  81. self.cmd_opts.add_option(cmdoptions.require_hashes())
  82. index_opts = cmdoptions.make_option_group(
  83. cmdoptions.index_group,
  84. self.parser,
  85. )
  86. self.parser.insert_option_group(0, index_opts)
  87. self.parser.insert_option_group(0, self.cmd_opts)
  88. @with_cleanup
  89. def run(self, options, args):
  90. # type: (Values, List[str]) -> int
  91. cmdoptions.check_install_build_global(options)
  92. session = self.get_default_session(options)
  93. finder = self._build_package_finder(options, session)
  94. build_delete = (not (options.no_clean or options.build_dir))
  95. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  96. options.wheel_dir = normalize_path(options.wheel_dir)
  97. ensure_dir(options.wheel_dir)
  98. req_tracker = self.enter_context(get_requirement_tracker())
  99. directory = TempDirectory(
  100. options.build_dir,
  101. delete=build_delete,
  102. kind="wheel",
  103. globally_managed=True,
  104. )
  105. reqs = self.get_requirements(args, options, finder, session)
  106. preparer = self.make_requirement_preparer(
  107. temp_build_dir=directory,
  108. options=options,
  109. req_tracker=req_tracker,
  110. session=session,
  111. finder=finder,
  112. wheel_download_dir=options.wheel_dir,
  113. use_user_site=False,
  114. )
  115. resolver = self.make_resolver(
  116. preparer=preparer,
  117. finder=finder,
  118. options=options,
  119. wheel_cache=wheel_cache,
  120. ignore_requires_python=options.ignore_requires_python,
  121. use_pep517=options.use_pep517,
  122. )
  123. self.trace_basic_info(finder)
  124. requirement_set = resolver.resolve(
  125. reqs, check_supported_wheels=True
  126. )
  127. reqs_to_build = [
  128. r for r in requirement_set.requirements.values()
  129. if should_build_for_wheel_command(r)
  130. ]
  131. # build wheels
  132. build_successes, build_failures = build(
  133. reqs_to_build,
  134. wheel_cache=wheel_cache,
  135. build_options=options.build_options or [],
  136. global_options=options.global_options or [],
  137. )
  138. for req in build_successes:
  139. assert req.link and req.link.is_wheel
  140. assert req.local_file_path
  141. # copy from cache to target directory
  142. try:
  143. shutil.copy(req.local_file_path, options.wheel_dir)
  144. except OSError as e:
  145. logger.warning(
  146. "Building wheel for %s failed: %s",
  147. req.name, e,
  148. )
  149. build_failures.append(req)
  150. if len(build_failures) != 0:
  151. raise CommandError(
  152. "Failed to build one or more wheels"
  153. )
  154. return SUCCESS