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.

download.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from pip._internal.cli import cmdoptions
  5. from pip._internal.cli.base_command import RequirementCommand
  6. from pip._internal.operations.prepare import RequirementPreparer
  7. from pip._internal.req import RequirementSet
  8. from pip._internal.req.req_tracker import RequirementTracker
  9. from pip._internal.resolve import Resolver
  10. from pip._internal.utils.filesystem import check_path_owner
  11. from pip._internal.utils.misc import ensure_dir, normalize_path
  12. from pip._internal.utils.temp_dir import TempDirectory
  13. logger = logging.getLogger(__name__)
  14. class DownloadCommand(RequirementCommand):
  15. """
  16. Download packages from:
  17. - PyPI (and other indexes) using requirement specifiers.
  18. - VCS project urls.
  19. - Local project directories.
  20. - Local or remote source archives.
  21. pip also supports downloading from "requirements files", which provide
  22. an easy way to specify a whole environment to be downloaded.
  23. """
  24. name = 'download'
  25. usage = """
  26. %prog [options] <requirement specifier> [package-index-options] ...
  27. %prog [options] -r <requirements file> [package-index-options] ...
  28. %prog [options] <vcs project url> ...
  29. %prog [options] <local project path> ...
  30. %prog [options] <archive url/path> ..."""
  31. summary = 'Download packages.'
  32. def __init__(self, *args, **kw):
  33. super(DownloadCommand, self).__init__(*args, **kw)
  34. cmd_opts = self.cmd_opts
  35. cmd_opts.add_option(cmdoptions.constraints())
  36. cmd_opts.add_option(cmdoptions.requirements())
  37. cmd_opts.add_option(cmdoptions.build_dir())
  38. cmd_opts.add_option(cmdoptions.no_deps())
  39. cmd_opts.add_option(cmdoptions.global_options())
  40. cmd_opts.add_option(cmdoptions.no_binary())
  41. cmd_opts.add_option(cmdoptions.only_binary())
  42. cmd_opts.add_option(cmdoptions.prefer_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(cmdoptions.use_pep517())
  50. cmd_opts.add_option(cmdoptions.no_use_pep517())
  51. cmd_opts.add_option(
  52. '-d', '--dest', '--destination-dir', '--destination-directory',
  53. dest='download_dir',
  54. metavar='dir',
  55. default=os.curdir,
  56. help=("Download packages into <dir>."),
  57. )
  58. cmd_opts.add_option(cmdoptions.platform())
  59. cmd_opts.add_option(cmdoptions.python_version())
  60. cmd_opts.add_option(cmdoptions.implementation())
  61. cmd_opts.add_option(cmdoptions.abi())
  62. index_opts = cmdoptions.make_option_group(
  63. cmdoptions.index_group,
  64. self.parser,
  65. )
  66. self.parser.insert_option_group(0, index_opts)
  67. self.parser.insert_option_group(0, cmd_opts)
  68. def run(self, options, args):
  69. options.ignore_installed = True
  70. # editable doesn't really make sense for `pip download`, but the bowels
  71. # of the RequirementSet code require that property.
  72. options.editables = []
  73. if options.python_version:
  74. python_versions = [options.python_version]
  75. else:
  76. python_versions = None
  77. cmdoptions.check_dist_restriction(options)
  78. options.src_dir = os.path.abspath(options.src_dir)
  79. options.download_dir = normalize_path(options.download_dir)
  80. ensure_dir(options.download_dir)
  81. with self._build_session(options) as session:
  82. finder = self._build_package_finder(
  83. options=options,
  84. session=session,
  85. platform=options.platform,
  86. python_versions=python_versions,
  87. abi=options.abi,
  88. implementation=options.implementation,
  89. )
  90. build_delete = (not (options.no_clean or options.build_dir))
  91. if options.cache_dir and not check_path_owner(options.cache_dir):
  92. logger.warning(
  93. "The directory '%s' or its parent directory is not owned "
  94. "by the current user and caching wheels has been "
  95. "disabled. check the permissions and owner of that "
  96. "directory. If executing pip with sudo, you may want "
  97. "sudo's -H flag.",
  98. options.cache_dir,
  99. )
  100. options.cache_dir = None
  101. with RequirementTracker() as req_tracker, TempDirectory(
  102. options.build_dir, delete=build_delete, kind="download"
  103. ) as directory:
  104. requirement_set = RequirementSet(
  105. require_hashes=options.require_hashes,
  106. )
  107. self.populate_requirement_set(
  108. requirement_set,
  109. args,
  110. options,
  111. finder,
  112. session,
  113. self.name,
  114. None
  115. )
  116. preparer = RequirementPreparer(
  117. build_dir=directory.path,
  118. src_dir=options.src_dir,
  119. download_dir=options.download_dir,
  120. wheel_download_dir=None,
  121. progress_bar=options.progress_bar,
  122. build_isolation=options.build_isolation,
  123. req_tracker=req_tracker,
  124. )
  125. resolver = Resolver(
  126. preparer=preparer,
  127. finder=finder,
  128. session=session,
  129. wheel_cache=None,
  130. use_user_site=False,
  131. upgrade_strategy="to-satisfy-only",
  132. force_reinstall=False,
  133. ignore_dependencies=options.ignore_dependencies,
  134. ignore_requires_python=False,
  135. ignore_installed=True,
  136. isolated=options.isolated_mode,
  137. )
  138. resolver.resolve(requirement_set)
  139. downloaded = ' '.join([
  140. req.name for req in requirement_set.successfully_downloaded
  141. ])
  142. if downloaded:
  143. logger.info('Successfully downloaded %s', downloaded)
  144. # Clean up
  145. if not options.no_clean:
  146. requirement_set.cleanup_files()
  147. return requirement_set