wheel.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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', '--wheel-dir',
  36. dest='wheel_dir',
  37. metavar='dir',
  38. default=os.curdir,
  39. help=("Build wheels into <dir>, where the default is the "
  40. "current working directory."),
  41. )
  42. self.cmd_opts.add_option(cmdoptions.no_binary())
  43. self.cmd_opts.add_option(cmdoptions.only_binary())
  44. self.cmd_opts.add_option(cmdoptions.prefer_binary())
  45. self.cmd_opts.add_option(cmdoptions.no_build_isolation())
  46. self.cmd_opts.add_option(cmdoptions.use_pep517())
  47. self.cmd_opts.add_option(cmdoptions.no_use_pep517())
  48. self.cmd_opts.add_option(cmdoptions.constraints())
  49. self.cmd_opts.add_option(cmdoptions.editable())
  50. self.cmd_opts.add_option(cmdoptions.requirements())
  51. self.cmd_opts.add_option(cmdoptions.src())
  52. self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
  53. self.cmd_opts.add_option(cmdoptions.no_deps())
  54. self.cmd_opts.add_option(cmdoptions.build_dir())
  55. self.cmd_opts.add_option(cmdoptions.progress_bar())
  56. self.cmd_opts.add_option(
  57. '--no-verify',
  58. dest='no_verify',
  59. action='store_true',
  60. default=False,
  61. help="Don't verify if built wheel is valid.",
  62. )
  63. self.cmd_opts.add_option(cmdoptions.build_options())
  64. self.cmd_opts.add_option(cmdoptions.global_options())
  65. self.cmd_opts.add_option(
  66. '--pre',
  67. action='store_true',
  68. default=False,
  69. help=("Include pre-release and development versions. By default, "
  70. "pip only finds stable versions."),
  71. )
  72. self.cmd_opts.add_option(cmdoptions.require_hashes())
  73. index_opts = cmdoptions.make_option_group(
  74. cmdoptions.index_group,
  75. self.parser,
  76. )
  77. self.parser.insert_option_group(0, index_opts)
  78. self.parser.insert_option_group(0, self.cmd_opts)
  79. @with_cleanup
  80. def run(self, options: Values, args: List[str]) -> int:
  81. cmdoptions.check_install_build_global(options)
  82. session = self.get_default_session(options)
  83. finder = self._build_package_finder(options, session)
  84. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  85. options.wheel_dir = normalize_path(options.wheel_dir)
  86. ensure_dir(options.wheel_dir)
  87. req_tracker = self.enter_context(get_requirement_tracker())
  88. directory = TempDirectory(
  89. delete=not options.no_clean,
  90. kind="wheel",
  91. globally_managed=True,
  92. )
  93. reqs = self.get_requirements(args, options, finder, session)
  94. preparer = self.make_requirement_preparer(
  95. temp_build_dir=directory,
  96. options=options,
  97. req_tracker=req_tracker,
  98. session=session,
  99. finder=finder,
  100. download_dir=options.wheel_dir,
  101. use_user_site=False,
  102. )
  103. resolver = self.make_resolver(
  104. preparer=preparer,
  105. finder=finder,
  106. options=options,
  107. wheel_cache=wheel_cache,
  108. ignore_requires_python=options.ignore_requires_python,
  109. use_pep517=options.use_pep517,
  110. )
  111. self.trace_basic_info(finder)
  112. requirement_set = resolver.resolve(
  113. reqs, check_supported_wheels=True
  114. )
  115. reqs_to_build: List[InstallRequirement] = []
  116. for req in requirement_set.requirements.values():
  117. if req.is_wheel:
  118. preparer.save_linked_requirement(req)
  119. elif should_build_for_wheel_command(req):
  120. reqs_to_build.append(req)
  121. # build wheels
  122. build_successes, build_failures = build(
  123. reqs_to_build,
  124. wheel_cache=wheel_cache,
  125. verify=(not options.no_verify),
  126. build_options=options.build_options or [],
  127. global_options=options.global_options or [],
  128. )
  129. for req in build_successes:
  130. assert req.link and req.link.is_wheel
  131. assert req.local_file_path
  132. # copy from cache to target directory
  133. try:
  134. shutil.copy(req.local_file_path, options.wheel_dir)
  135. except OSError as e:
  136. logger.warning(
  137. "Building wheel for %s failed: %s",
  138. req.name, e,
  139. )
  140. build_failures.append(req)
  141. if len(build_failures) != 0:
  142. raise CommandError(
  143. "Failed to build one or more wheels"
  144. )
  145. return SUCCESS