wheel.py 6.1 KB

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