You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wheel.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. from pip._internal.cache import WheelCache
  8. from pip._internal.cli import cmdoptions
  9. from pip._internal.cli.req_command import RequirementCommand
  10. from pip._internal.exceptions import CommandError, PreviousBuildDirError
  11. from pip._internal.req import RequirementSet
  12. from pip._internal.req.req_tracker import RequirementTracker
  13. from pip._internal.utils.temp_dir import TempDirectory
  14. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  15. from pip._internal.wheel import WheelBuilder
  16. if MYPY_CHECK_RUNNING:
  17. from optparse import Values
  18. from typing import Any, 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 __init__(self, *args, **kw):
  37. super(WheelCommand, self).__init__(*args, **kw)
  38. cmd_opts = self.cmd_opts
  39. 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. cmd_opts.add_option(cmdoptions.no_binary())
  48. cmd_opts.add_option(cmdoptions.only_binary())
  49. cmd_opts.add_option(cmdoptions.prefer_binary())
  50. 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. cmd_opts.add_option(cmdoptions.no_build_isolation())
  58. cmd_opts.add_option(cmdoptions.use_pep517())
  59. cmd_opts.add_option(cmdoptions.no_use_pep517())
  60. cmd_opts.add_option(cmdoptions.constraints())
  61. cmd_opts.add_option(cmdoptions.editable())
  62. cmd_opts.add_option(cmdoptions.requirements())
  63. cmd_opts.add_option(cmdoptions.src())
  64. cmd_opts.add_option(cmdoptions.ignore_requires_python())
  65. cmd_opts.add_option(cmdoptions.no_deps())
  66. cmd_opts.add_option(cmdoptions.build_dir())
  67. cmd_opts.add_option(cmdoptions.progress_bar())
  68. cmd_opts.add_option(
  69. '--global-option',
  70. dest='global_options',
  71. action='append',
  72. metavar='options',
  73. help="Extra global options to be supplied to the setup.py "
  74. "call before the 'bdist_wheel' command.")
  75. cmd_opts.add_option(
  76. '--pre',
  77. action='store_true',
  78. default=False,
  79. help=("Include pre-release and development versions. By default, "
  80. "pip only finds stable versions."),
  81. )
  82. cmd_opts.add_option(cmdoptions.no_clean())
  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. def run(self, options, args):
  91. # type: (Values, List[Any]) -> None
  92. cmdoptions.check_install_build_global(options)
  93. if options.build_dir:
  94. options.build_dir = os.path.abspath(options.build_dir)
  95. options.src_dir = os.path.abspath(options.src_dir)
  96. session = self.get_default_session(options)
  97. finder = self._build_package_finder(options, session)
  98. build_delete = (not (options.no_clean or options.build_dir))
  99. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  100. with RequirementTracker() as req_tracker, TempDirectory(
  101. options.build_dir, delete=build_delete, kind="wheel"
  102. ) as directory:
  103. requirement_set = RequirementSet(
  104. require_hashes=options.require_hashes,
  105. )
  106. try:
  107. self.populate_requirement_set(
  108. requirement_set, args, options, finder, session,
  109. wheel_cache
  110. )
  111. preparer = self.make_requirement_preparer(
  112. temp_build_dir=directory,
  113. options=options,
  114. req_tracker=req_tracker,
  115. wheel_download_dir=options.wheel_dir,
  116. )
  117. resolver = self.make_resolver(
  118. preparer=preparer,
  119. finder=finder,
  120. session=session,
  121. options=options,
  122. wheel_cache=wheel_cache,
  123. ignore_requires_python=options.ignore_requires_python,
  124. use_pep517=options.use_pep517,
  125. )
  126. resolver.resolve(requirement_set)
  127. # build wheels
  128. wb = WheelBuilder(
  129. preparer, wheel_cache,
  130. build_options=options.build_options or [],
  131. global_options=options.global_options or [],
  132. no_clean=options.no_clean,
  133. )
  134. build_failures = wb.build(
  135. requirement_set.requirements.values(),
  136. )
  137. if len(build_failures) != 0:
  138. raise CommandError(
  139. "Failed to build one or more wheels"
  140. )
  141. except PreviousBuildDirError:
  142. options.no_clean = True
  143. raise
  144. finally:
  145. if not options.no_clean:
  146. requirement_set.cleanup_files()
  147. wheel_cache.cleanup()