wheel.py 6.6 KB

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