download.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from pip._internal import cmdoptions
  5. from pip._internal.basecommand import RequirementCommand
  6. from pip._internal.exceptions import CommandError
  7. from pip._internal.index import FormatControl
  8. from pip._internal.operations.prepare import RequirementPreparer
  9. from pip._internal.req import RequirementSet
  10. from pip._internal.resolve import Resolver
  11. from pip._internal.utils.filesystem import check_path_owner
  12. from pip._internal.utils.misc import ensure_dir, normalize_path
  13. from pip._internal.utils.temp_dir import TempDirectory
  14. logger = logging.getLogger(__name__)
  15. class DownloadCommand(RequirementCommand):
  16. """
  17. Download packages from:
  18. - PyPI (and other indexes) using requirement specifiers.
  19. - VCS project urls.
  20. - Local project directories.
  21. - Local or remote source archives.
  22. pip also supports downloading from "requirements files", which provide
  23. an easy way to specify a whole environment to be downloaded.
  24. """
  25. name = 'download'
  26. usage = """
  27. %prog [options] <requirement specifier> [package-index-options] ...
  28. %prog [options] -r <requirements file> [package-index-options] ...
  29. %prog [options] <vcs project url> ...
  30. %prog [options] <local project path> ...
  31. %prog [options] <archive url/path> ..."""
  32. summary = 'Download packages.'
  33. def __init__(self, *args, **kw):
  34. super(DownloadCommand, self).__init__(*args, **kw)
  35. cmd_opts = self.cmd_opts
  36. cmd_opts.add_option(cmdoptions.constraints())
  37. cmd_opts.add_option(cmdoptions.requirements())
  38. cmd_opts.add_option(cmdoptions.build_dir())
  39. cmd_opts.add_option(cmdoptions.no_deps())
  40. cmd_opts.add_option(cmdoptions.global_options())
  41. cmd_opts.add_option(cmdoptions.no_binary())
  42. cmd_opts.add_option(cmdoptions.only_binary())
  43. cmd_opts.add_option(cmdoptions.src())
  44. cmd_opts.add_option(cmdoptions.pre())
  45. cmd_opts.add_option(cmdoptions.no_clean())
  46. cmd_opts.add_option(cmdoptions.require_hashes())
  47. cmd_opts.add_option(cmdoptions.progress_bar())
  48. cmd_opts.add_option(cmdoptions.no_build_isolation())
  49. cmd_opts.add_option(
  50. '-d', '--dest', '--destination-dir', '--destination-directory',
  51. dest='download_dir',
  52. metavar='dir',
  53. default=os.curdir,
  54. help=("Download packages into <dir>."),
  55. )
  56. cmd_opts.add_option(
  57. '--platform',
  58. dest='platform',
  59. metavar='platform',
  60. default=None,
  61. help=("Only download wheels compatible with <platform>. "
  62. "Defaults to the platform of the running system."),
  63. )
  64. cmd_opts.add_option(
  65. '--python-version',
  66. dest='python_version',
  67. metavar='python_version',
  68. default=None,
  69. help=("Only download wheels compatible with Python "
  70. "interpreter version <version>. If not specified, then the "
  71. "current system interpreter minor version is used. A major "
  72. "version (e.g. '2') can be specified to match all "
  73. "minor revs of that major version. A minor version "
  74. "(e.g. '34') can also be specified."),
  75. )
  76. cmd_opts.add_option(
  77. '--implementation',
  78. dest='implementation',
  79. metavar='implementation',
  80. default=None,
  81. help=("Only download wheels compatible with Python "
  82. "implementation <implementation>, e.g. 'pp', 'jy', 'cp', "
  83. " or 'ip'. If not specified, then the current "
  84. "interpreter implementation is used. Use 'py' to force "
  85. "implementation-agnostic wheels."),
  86. )
  87. cmd_opts.add_option(
  88. '--abi',
  89. dest='abi',
  90. metavar='abi',
  91. default=None,
  92. help=("Only download wheels compatible with Python "
  93. "abi <abi>, e.g. 'pypy_41'. If not specified, then the "
  94. "current interpreter abi tag is used. Generally "
  95. "you will need to specify --implementation, "
  96. "--platform, and --python-version when using "
  97. "this option."),
  98. )
  99. index_opts = cmdoptions.make_option_group(
  100. cmdoptions.index_group,
  101. self.parser,
  102. )
  103. self.parser.insert_option_group(0, index_opts)
  104. self.parser.insert_option_group(0, cmd_opts)
  105. def run(self, options, args):
  106. options.ignore_installed = True
  107. # editable doesn't really make sense for `pip download`, but the bowels
  108. # of the RequirementSet code require that property.
  109. options.editables = []
  110. if options.python_version:
  111. python_versions = [options.python_version]
  112. else:
  113. python_versions = None
  114. dist_restriction_set = any([
  115. options.python_version,
  116. options.platform,
  117. options.abi,
  118. options.implementation,
  119. ])
  120. binary_only = FormatControl(set(), {':all:'})
  121. no_sdist_dependencies = (
  122. options.format_control != binary_only and
  123. not options.ignore_dependencies
  124. )
  125. if dist_restriction_set and no_sdist_dependencies:
  126. raise CommandError(
  127. "When restricting platform and interpreter constraints using "
  128. "--python-version, --platform, --abi, or --implementation, "
  129. "either --no-deps must be set, or --only-binary=:all: must be "
  130. "set and --no-binary must not be set (or must be set to "
  131. ":none:)."
  132. )
  133. options.src_dir = os.path.abspath(options.src_dir)
  134. options.download_dir = normalize_path(options.download_dir)
  135. ensure_dir(options.download_dir)
  136. with self._build_session(options) as session:
  137. finder = self._build_package_finder(
  138. options=options,
  139. session=session,
  140. platform=options.platform,
  141. python_versions=python_versions,
  142. abi=options.abi,
  143. implementation=options.implementation,
  144. )
  145. build_delete = (not (options.no_clean or options.build_dir))
  146. if options.cache_dir and not check_path_owner(options.cache_dir):
  147. logger.warning(
  148. "The directory '%s' or its parent directory is not owned "
  149. "by the current user and caching wheels has been "
  150. "disabled. check the permissions and owner of that "
  151. "directory. If executing pip with sudo, you may want "
  152. "sudo's -H flag.",
  153. options.cache_dir,
  154. )
  155. options.cache_dir = None
  156. with TempDirectory(
  157. options.build_dir, delete=build_delete, kind="download"
  158. ) as directory:
  159. requirement_set = RequirementSet(
  160. require_hashes=options.require_hashes,
  161. )
  162. self.populate_requirement_set(
  163. requirement_set,
  164. args,
  165. options,
  166. finder,
  167. session,
  168. self.name,
  169. None
  170. )
  171. preparer = RequirementPreparer(
  172. build_dir=directory.path,
  173. src_dir=options.src_dir,
  174. download_dir=options.download_dir,
  175. wheel_download_dir=None,
  176. progress_bar=options.progress_bar,
  177. build_isolation=options.build_isolation,
  178. )
  179. resolver = Resolver(
  180. preparer=preparer,
  181. finder=finder,
  182. session=session,
  183. wheel_cache=None,
  184. use_user_site=False,
  185. upgrade_strategy="to-satisfy-only",
  186. force_reinstall=False,
  187. ignore_dependencies=options.ignore_dependencies,
  188. ignore_requires_python=False,
  189. ignore_installed=True,
  190. isolated=options.isolated_mode,
  191. )
  192. resolver.resolve(requirement_set)
  193. downloaded = ' '.join([
  194. req.name for req in requirement_set.successfully_downloaded
  195. ])
  196. if downloaded:
  197. logger.info('Successfully downloaded %s', downloaded)
  198. # Clean up
  199. if not options.no_clean:
  200. requirement_set.cleanup_files()
  201. return requirement_set