wheel.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # -*- coding: utf-8 -*-
  2. # The following comment should be removed at some point in the future.
  3. # mypy: disallow-untyped-defs=False
  4. from __future__ import absolute_import
  5. import logging
  6. import os
  7. import shutil
  8. from pip._internal.cache import WheelCache
  9. from pip._internal.cli import cmdoptions
  10. from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  11. from pip._internal.exceptions import CommandError
  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.utils.typing import MYPY_CHECK_RUNNING
  16. from pip._internal.wheel_builder import build, should_build_for_wheel_command
  17. if MYPY_CHECK_RUNNING:
  18. from optparse import Values
  19. from typing import Any, List
  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 __init__(self, *args, **kw):
  38. super(WheelCommand, self).__init__(*args, **kw)
  39. cmd_opts = self.cmd_opts
  40. cmd_opts.add_option(
  41. '-w', '--wheel-dir',
  42. dest='wheel_dir',
  43. metavar='dir',
  44. default=os.curdir,
  45. help=("Build wheels into <dir>, where the default is the "
  46. "current working directory."),
  47. )
  48. cmd_opts.add_option(cmdoptions.no_binary())
  49. cmd_opts.add_option(cmdoptions.only_binary())
  50. cmd_opts.add_option(cmdoptions.prefer_binary())
  51. cmd_opts.add_option(
  52. '--build-option',
  53. dest='build_options',
  54. metavar='options',
  55. action='append',
  56. help="Extra arguments to be supplied to 'setup.py bdist_wheel'.",
  57. )
  58. cmd_opts.add_option(cmdoptions.no_build_isolation())
  59. cmd_opts.add_option(cmdoptions.use_pep517())
  60. cmd_opts.add_option(cmdoptions.no_use_pep517())
  61. cmd_opts.add_option(cmdoptions.constraints())
  62. cmd_opts.add_option(cmdoptions.editable())
  63. cmd_opts.add_option(cmdoptions.requirements())
  64. cmd_opts.add_option(cmdoptions.src())
  65. cmd_opts.add_option(cmdoptions.ignore_requires_python())
  66. cmd_opts.add_option(cmdoptions.no_deps())
  67. cmd_opts.add_option(cmdoptions.build_dir())
  68. cmd_opts.add_option(cmdoptions.progress_bar())
  69. cmd_opts.add_option(
  70. '--global-option',
  71. dest='global_options',
  72. action='append',
  73. metavar='options',
  74. help="Extra global options to be supplied to the setup.py "
  75. "call before the 'bdist_wheel' command.")
  76. cmd_opts.add_option(
  77. '--pre',
  78. action='store_true',
  79. default=False,
  80. help=("Include pre-release and development versions. By default, "
  81. "pip only finds stable versions."),
  82. )
  83. cmd_opts.add_option(cmdoptions.require_hashes())
  84. index_opts = cmdoptions.make_option_group(
  85. cmdoptions.index_group,
  86. self.parser,
  87. )
  88. self.parser.insert_option_group(0, index_opts)
  89. self.parser.insert_option_group(0, cmd_opts)
  90. @with_cleanup
  91. def run(self, options, args):
  92. # type: (Values, List[Any]) -> None
  93. cmdoptions.check_install_build_global(options)
  94. session = self.get_default_session(options)
  95. finder = self._build_package_finder(options, session)
  96. build_delete = (not (options.no_clean or options.build_dir))
  97. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  98. options.wheel_dir = normalize_path(options.wheel_dir)
  99. ensure_dir(options.wheel_dir)
  100. req_tracker = self.enter_context(get_requirement_tracker())
  101. directory = TempDirectory(
  102. options.build_dir,
  103. delete=build_delete,
  104. kind="wheel",
  105. globally_managed=True,
  106. )
  107. reqs = self.get_requirements(args, options, finder, session)
  108. preparer = self.make_requirement_preparer(
  109. temp_build_dir=directory,
  110. options=options,
  111. req_tracker=req_tracker,
  112. session=session,
  113. finder=finder,
  114. wheel_download_dir=options.wheel_dir,
  115. use_user_site=False,
  116. )
  117. resolver = self.make_resolver(
  118. preparer=preparer,
  119. finder=finder,
  120. options=options,
  121. wheel_cache=wheel_cache,
  122. ignore_requires_python=options.ignore_requires_python,
  123. use_pep517=options.use_pep517,
  124. )
  125. self.trace_basic_info(finder)
  126. requirement_set = resolver.resolve(
  127. reqs, check_supported_wheels=True
  128. )
  129. reqs_to_build = [
  130. r for r in requirement_set.requirements.values()
  131. if should_build_for_wheel_command(r)
  132. ]
  133. # build wheels
  134. build_successes, build_failures = build(
  135. reqs_to_build,
  136. wheel_cache=wheel_cache,
  137. build_options=options.build_options or [],
  138. global_options=options.global_options or [],
  139. )
  140. for req in build_successes:
  141. assert req.link and req.link.is_wheel
  142. assert req.local_file_path
  143. # copy from cache to target directory
  144. try:
  145. shutil.copy(req.local_file_path, options.wheel_dir)
  146. except OSError as e:
  147. logger.warning(
  148. "Building wheel for %s failed: %s",
  149. req.name, e,
  150. )
  151. build_failures.append(req)
  152. if len(build_failures) != 0:
  153. raise CommandError(
  154. "Failed to build one or more wheels"
  155. )