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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import logging
  5. import os
  6. from pip._internal.cli import cmdoptions
  7. from pip._internal.cli.cmdoptions import make_target_python
  8. from pip._internal.cli.req_command import RequirementCommand
  9. from pip._internal.req import RequirementSet
  10. from pip._internal.req.req_tracker import RequirementTracker
  11. from pip._internal.utils.filesystem import check_path_owner
  12. from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
  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. 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. def __init__(self, *args, **kw):
  32. super(DownloadCommand, self).__init__(*args, **kw)
  33. cmd_opts = self.cmd_opts
  34. cmd_opts.add_option(cmdoptions.constraints())
  35. cmd_opts.add_option(cmdoptions.requirements())
  36. cmd_opts.add_option(cmdoptions.build_dir())
  37. cmd_opts.add_option(cmdoptions.no_deps())
  38. cmd_opts.add_option(cmdoptions.global_options())
  39. cmd_opts.add_option(cmdoptions.no_binary())
  40. cmd_opts.add_option(cmdoptions.only_binary())
  41. cmd_opts.add_option(cmdoptions.prefer_binary())
  42. cmd_opts.add_option(cmdoptions.src())
  43. cmd_opts.add_option(cmdoptions.pre())
  44. cmd_opts.add_option(cmdoptions.no_clean())
  45. cmd_opts.add_option(cmdoptions.require_hashes())
  46. cmd_opts.add_option(cmdoptions.progress_bar())
  47. cmd_opts.add_option(cmdoptions.no_build_isolation())
  48. cmd_opts.add_option(cmdoptions.use_pep517())
  49. cmd_opts.add_option(cmdoptions.no_use_pep517())
  50. cmd_opts.add_option(
  51. '-d', '--dest', '--destination-dir', '--destination-directory',
  52. dest='download_dir',
  53. metavar='dir',
  54. default=os.curdir,
  55. help=("Download packages into <dir>."),
  56. )
  57. cmdoptions.add_target_python_options(cmd_opts)
  58. index_opts = cmdoptions.make_option_group(
  59. cmdoptions.index_group,
  60. self.parser,
  61. )
  62. self.parser.insert_option_group(0, index_opts)
  63. self.parser.insert_option_group(0, cmd_opts)
  64. def run(self, options, args):
  65. options.ignore_installed = True
  66. # editable doesn't really make sense for `pip download`, but the bowels
  67. # of the RequirementSet code require that property.
  68. options.editables = []
  69. cmdoptions.check_dist_restriction(options)
  70. options.src_dir = os.path.abspath(options.src_dir)
  71. options.download_dir = normalize_path(options.download_dir)
  72. ensure_dir(options.download_dir)
  73. session = self.get_default_session(options)
  74. target_python = make_target_python(options)
  75. finder = self._build_package_finder(
  76. options=options,
  77. session=session,
  78. target_python=target_python,
  79. )
  80. build_delete = (not (options.no_clean or options.build_dir))
  81. if options.cache_dir and not check_path_owner(options.cache_dir):
  82. logger.warning(
  83. "The directory '%s' or its parent directory is not owned "
  84. "by the current user and caching wheels has been "
  85. "disabled. check the permissions and owner of that "
  86. "directory. If executing pip with sudo, you may want "
  87. "sudo's -H flag.",
  88. options.cache_dir,
  89. )
  90. options.cache_dir = None
  91. with RequirementTracker() as req_tracker, TempDirectory(
  92. options.build_dir, delete=build_delete, kind="download"
  93. ) as directory:
  94. requirement_set = RequirementSet(
  95. require_hashes=options.require_hashes,
  96. )
  97. self.populate_requirement_set(
  98. requirement_set,
  99. args,
  100. options,
  101. finder,
  102. session,
  103. None
  104. )
  105. preparer = self.make_requirement_preparer(
  106. temp_build_dir=directory,
  107. options=options,
  108. req_tracker=req_tracker,
  109. download_dir=options.download_dir,
  110. )
  111. resolver = self.make_resolver(
  112. preparer=preparer,
  113. finder=finder,
  114. session=session,
  115. options=options,
  116. py_version_info=options.python_version,
  117. )
  118. resolver.resolve(requirement_set)
  119. downloaded = ' '.join([
  120. req.name for req in requirement_set.successfully_downloaded
  121. ])
  122. if downloaded:
  123. write_output('Successfully downloaded %s', downloaded)
  124. # Clean up
  125. if not options.no_clean:
  126. requirement_set.cleanup_files()
  127. return requirement_set