wheel.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import logging
  4. import os
  5. from pip._internal import cmdoptions
  6. from pip._internal.basecommand import RequirementCommand
  7. from pip._internal.cache import WheelCache
  8. from pip._internal.exceptions import CommandError, PreviousBuildDirError
  9. from pip._internal.operations.prepare import RequirementPreparer
  10. from pip._internal.req import RequirementSet
  11. from pip._internal.resolve import Resolver
  12. from pip._internal.utils.temp_dir import TempDirectory
  13. from pip._internal.wheel import WheelBuilder
  14. logger = logging.getLogger(__name__)
  15. class WheelCommand(RequirementCommand):
  16. """
  17. Build Wheel archives for your requirements and dependencies.
  18. Wheel is a built-package format, and offers the advantage of not
  19. recompiling your software during every install. For more details, see the
  20. wheel docs: https://wheel.readthedocs.io/en/latest/
  21. Requirements: setuptools>=0.8, and wheel.
  22. 'pip wheel' uses the bdist_wheel setuptools extension from the wheel
  23. package to build individual wheels.
  24. """
  25. name = 'wheel'
  26. usage = """
  27. %prog [options] <requirement specifier> ...
  28. %prog [options] -r <requirements file> ...
  29. %prog [options] [-e] <vcs project url> ...
  30. %prog [options] [-e] <local project path> ...
  31. %prog [options] <archive url/path> ..."""
  32. summary = 'Build wheels from your requirements.'
  33. def __init__(self, *args, **kw):
  34. super(WheelCommand, self).__init__(*args, **kw)
  35. cmd_opts = self.cmd_opts
  36. cmd_opts.add_option(
  37. '-w', '--wheel-dir',
  38. dest='wheel_dir',
  39. metavar='dir',
  40. default=os.curdir,
  41. help=("Build wheels into <dir>, where the default is the "
  42. "current working directory."),
  43. )
  44. cmd_opts.add_option(cmdoptions.no_binary())
  45. cmd_opts.add_option(cmdoptions.only_binary())
  46. cmd_opts.add_option(
  47. '--build-option',
  48. dest='build_options',
  49. metavar='options',
  50. action='append',
  51. help="Extra arguments to be supplied to 'setup.py bdist_wheel'.",
  52. )
  53. cmd_opts.add_option(cmdoptions.no_build_isolation())
  54. cmd_opts.add_option(cmdoptions.constraints())
  55. cmd_opts.add_option(cmdoptions.editable())
  56. cmd_opts.add_option(cmdoptions.requirements())
  57. cmd_opts.add_option(cmdoptions.src())
  58. cmd_opts.add_option(cmdoptions.ignore_requires_python())
  59. cmd_opts.add_option(cmdoptions.no_deps())
  60. cmd_opts.add_option(cmdoptions.build_dir())
  61. cmd_opts.add_option(cmdoptions.progress_bar())
  62. cmd_opts.add_option(
  63. '--global-option',
  64. dest='global_options',
  65. action='append',
  66. metavar='options',
  67. help="Extra global options to be supplied to the setup.py "
  68. "call before the 'bdist_wheel' command.")
  69. cmd_opts.add_option(
  70. '--pre',
  71. action='store_true',
  72. default=False,
  73. help=("Include pre-release and development versions. By default, "
  74. "pip only finds stable versions."),
  75. )
  76. cmd_opts.add_option(cmdoptions.no_clean())
  77. cmd_opts.add_option(cmdoptions.require_hashes())
  78. index_opts = cmdoptions.make_option_group(
  79. cmdoptions.index_group,
  80. self.parser,
  81. )
  82. self.parser.insert_option_group(0, index_opts)
  83. self.parser.insert_option_group(0, cmd_opts)
  84. def run(self, options, args):
  85. cmdoptions.check_install_build_global(options)
  86. index_urls = [options.index_url] + options.extra_index_urls
  87. if options.no_index:
  88. logger.debug('Ignoring indexes: %s', ','.join(index_urls))
  89. index_urls = []
  90. if options.build_dir:
  91. options.build_dir = os.path.abspath(options.build_dir)
  92. options.src_dir = os.path.abspath(options.src_dir)
  93. with self._build_session(options) as session:
  94. finder = self._build_package_finder(options, session)
  95. build_delete = (not (options.no_clean or options.build_dir))
  96. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  97. with TempDirectory(
  98. options.build_dir, delete=build_delete, kind="wheel"
  99. ) as directory:
  100. requirement_set = RequirementSet(
  101. require_hashes=options.require_hashes,
  102. )
  103. try:
  104. self.populate_requirement_set(
  105. requirement_set, args, options, finder, session,
  106. self.name, wheel_cache
  107. )
  108. preparer = RequirementPreparer(
  109. build_dir=directory.path,
  110. src_dir=options.src_dir,
  111. download_dir=None,
  112. wheel_download_dir=options.wheel_dir,
  113. progress_bar=options.progress_bar,
  114. build_isolation=options.build_isolation,
  115. )
  116. resolver = Resolver(
  117. preparer=preparer,
  118. finder=finder,
  119. session=session,
  120. wheel_cache=wheel_cache,
  121. use_user_site=False,
  122. upgrade_strategy="to-satisfy-only",
  123. force_reinstall=False,
  124. ignore_dependencies=options.ignore_dependencies,
  125. ignore_requires_python=options.ignore_requires_python,
  126. ignore_installed=True,
  127. isolated=options.isolated_mode,
  128. )
  129. resolver.resolve(requirement_set)
  130. # build wheels
  131. wb = WheelBuilder(
  132. finder, preparer, wheel_cache,
  133. build_options=options.build_options or [],
  134. global_options=options.global_options or [],
  135. no_clean=options.no_clean,
  136. )
  137. wheels_built_successfully = wb.build(
  138. requirement_set.requirements.values(), session=session,
  139. )
  140. if not wheels_built_successfully:
  141. raise CommandError(
  142. "Failed to build one or more wheels"
  143. )
  144. except PreviousBuildDirError:
  145. options.no_clean = True
  146. raise
  147. finally:
  148. if not options.no_clean:
  149. requirement_set.cleanup_files()
  150. wheel_cache.cleanup()