wheel.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import logging
  4. import os
  5. import warnings
  6. from pip.basecommand import RequirementCommand
  7. from pip.exceptions import CommandError, PreviousBuildDirError
  8. from pip.req import RequirementSet
  9. from pip.utils import import_or_raise
  10. from pip.utils.build import BuildDirectory
  11. from pip.utils.deprecation import RemovedInPip10Warning
  12. from pip.wheel import WheelCache, WheelBuilder
  13. from pip import cmdoptions
  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.use_wheel())
  45. cmd_opts.add_option(cmdoptions.no_use_wheel())
  46. cmd_opts.add_option(cmdoptions.no_binary())
  47. cmd_opts.add_option(cmdoptions.only_binary())
  48. cmd_opts.add_option(
  49. '--build-option',
  50. dest='build_options',
  51. metavar='options',
  52. action='append',
  53. help="Extra arguments to be supplied to 'setup.py bdist_wheel'.")
  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(
  62. '--global-option',
  63. dest='global_options',
  64. action='append',
  65. metavar='options',
  66. help="Extra global options to be supplied to the setup.py "
  67. "call before the 'bdist_wheel' command.")
  68. cmd_opts.add_option(
  69. '--pre',
  70. action='store_true',
  71. default=False,
  72. help=("Include pre-release and development versions. By default, "
  73. "pip only finds stable versions."),
  74. )
  75. cmd_opts.add_option(cmdoptions.no_clean())
  76. 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, cmd_opts)
  83. def check_required_packages(self):
  84. import_or_raise(
  85. 'wheel.bdist_wheel',
  86. CommandError,
  87. "'pip wheel' requires the 'wheel' package. To fix this, run: "
  88. "pip install wheel"
  89. )
  90. pkg_resources = import_or_raise(
  91. 'pkg_resources',
  92. CommandError,
  93. "'pip wheel' requires setuptools >= 0.8 for dist-info support."
  94. " To fix this, run: pip install --upgrade setuptools"
  95. )
  96. if not hasattr(pkg_resources, 'DistInfoDistribution'):
  97. raise CommandError(
  98. "'pip wheel' requires setuptools >= 0.8 for dist-info "
  99. "support. To fix this, run: pip install --upgrade "
  100. "setuptools"
  101. )
  102. def run(self, options, args):
  103. self.check_required_packages()
  104. cmdoptions.resolve_wheel_no_use_binary(options)
  105. cmdoptions.check_install_build_global(options)
  106. if options.allow_external:
  107. warnings.warn(
  108. "--allow-external has been deprecated and will be removed in "
  109. "the future. Due to changes in the repository protocol, it no "
  110. "longer has any effect.",
  111. RemovedInPip10Warning,
  112. )
  113. if options.allow_all_external:
  114. warnings.warn(
  115. "--allow-all-external has been deprecated and will be removed "
  116. "in the future. Due to changes in the repository protocol, it "
  117. "no longer has any effect.",
  118. RemovedInPip10Warning,
  119. )
  120. if options.allow_unverified:
  121. warnings.warn(
  122. "--allow-unverified has been deprecated and will be removed "
  123. "in the future. Due to changes in the repository protocol, it "
  124. "no longer has any effect.",
  125. RemovedInPip10Warning,
  126. )
  127. index_urls = [options.index_url] + options.extra_index_urls
  128. if options.no_index:
  129. logger.debug('Ignoring indexes: %s', ','.join(index_urls))
  130. index_urls = []
  131. if options.build_dir:
  132. options.build_dir = os.path.abspath(options.build_dir)
  133. options.src_dir = os.path.abspath(options.src_dir)
  134. with self._build_session(options) as session:
  135. finder = self._build_package_finder(options, session)
  136. build_delete = (not (options.no_clean or options.build_dir))
  137. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  138. with BuildDirectory(options.build_dir,
  139. delete=build_delete) as build_dir:
  140. requirement_set = RequirementSet(
  141. build_dir=build_dir,
  142. src_dir=options.src_dir,
  143. download_dir=None,
  144. ignore_dependencies=options.ignore_dependencies,
  145. ignore_installed=True,
  146. ignore_requires_python=options.ignore_requires_python,
  147. isolated=options.isolated_mode,
  148. session=session,
  149. wheel_cache=wheel_cache,
  150. wheel_download_dir=options.wheel_dir,
  151. require_hashes=options.require_hashes
  152. )
  153. self.populate_requirement_set(
  154. requirement_set, args, options, finder, session, self.name,
  155. wheel_cache
  156. )
  157. if not requirement_set.has_requirements:
  158. return
  159. try:
  160. # build wheels
  161. wb = WheelBuilder(
  162. requirement_set,
  163. finder,
  164. build_options=options.build_options or [],
  165. global_options=options.global_options or [],
  166. )
  167. if not wb.build():
  168. raise CommandError(
  169. "Failed to build one or more wheels"
  170. )
  171. except PreviousBuildDirError:
  172. options.no_clean = True
  173. raise
  174. finally:
  175. if not options.no_clean:
  176. requirement_set.cleanup_files()