download.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import logging
  2. import os
  3. from optparse import Values
  4. from typing import List
  5. from pip._internal.cli import cmdoptions
  6. from pip._internal.cli.cmdoptions import make_target_python
  7. from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  8. from pip._internal.cli.status_codes import SUCCESS
  9. from pip._internal.req.req_tracker import get_requirement_tracker
  10. from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
  11. from pip._internal.utils.temp_dir import TempDirectory
  12. logger = logging.getLogger(__name__)
  13. class DownloadCommand(RequirementCommand):
  14. """
  15. Download packages from:
  16. - PyPI (and other indexes) using requirement specifiers.
  17. - VCS project urls.
  18. - Local project directories.
  19. - Local or remote source archives.
  20. pip also supports downloading from "requirements files", which provide
  21. an easy way to specify a whole environment to be downloaded.
  22. """
  23. usage = """
  24. %prog [options] <requirement specifier> [package-index-options] ...
  25. %prog [options] -r <requirements file> [package-index-options] ...
  26. %prog [options] <vcs project url> ...
  27. %prog [options] <local project path> ...
  28. %prog [options] <archive url/path> ..."""
  29. def add_options(self) -> None:
  30. self.cmd_opts.add_option(cmdoptions.constraints())
  31. self.cmd_opts.add_option(cmdoptions.requirements())
  32. self.cmd_opts.add_option(cmdoptions.no_deps())
  33. self.cmd_opts.add_option(cmdoptions.global_options())
  34. self.cmd_opts.add_option(cmdoptions.no_binary())
  35. self.cmd_opts.add_option(cmdoptions.only_binary())
  36. self.cmd_opts.add_option(cmdoptions.prefer_binary())
  37. self.cmd_opts.add_option(cmdoptions.src())
  38. self.cmd_opts.add_option(cmdoptions.pre())
  39. self.cmd_opts.add_option(cmdoptions.require_hashes())
  40. self.cmd_opts.add_option(cmdoptions.progress_bar())
  41. self.cmd_opts.add_option(cmdoptions.no_build_isolation())
  42. self.cmd_opts.add_option(cmdoptions.use_pep517())
  43. self.cmd_opts.add_option(cmdoptions.no_use_pep517())
  44. self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
  45. self.cmd_opts.add_option(
  46. "-d",
  47. "--dest",
  48. "--destination-dir",
  49. "--destination-directory",
  50. dest="download_dir",
  51. metavar="dir",
  52. default=os.curdir,
  53. help="Download packages into <dir>.",
  54. )
  55. cmdoptions.add_target_python_options(self.cmd_opts)
  56. index_opts = cmdoptions.make_option_group(
  57. cmdoptions.index_group,
  58. self.parser,
  59. )
  60. self.parser.insert_option_group(0, index_opts)
  61. self.parser.insert_option_group(0, self.cmd_opts)
  62. @with_cleanup
  63. def run(self, options: Values, args: List[str]) -> int:
  64. options.ignore_installed = True
  65. # editable doesn't really make sense for `pip download`, but the bowels
  66. # of the RequirementSet code require that property.
  67. options.editables = []
  68. cmdoptions.check_dist_restriction(options)
  69. options.download_dir = normalize_path(options.download_dir)
  70. ensure_dir(options.download_dir)
  71. session = self.get_default_session(options)
  72. target_python = make_target_python(options)
  73. finder = self._build_package_finder(
  74. options=options,
  75. session=session,
  76. target_python=target_python,
  77. ignore_requires_python=options.ignore_requires_python,
  78. )
  79. req_tracker = self.enter_context(get_requirement_tracker())
  80. directory = TempDirectory(
  81. delete=not options.no_clean,
  82. kind="download",
  83. globally_managed=True,
  84. )
  85. reqs = self.get_requirements(args, options, finder, session)
  86. preparer = self.make_requirement_preparer(
  87. temp_build_dir=directory,
  88. options=options,
  89. req_tracker=req_tracker,
  90. session=session,
  91. finder=finder,
  92. download_dir=options.download_dir,
  93. use_user_site=False,
  94. verbosity=self.verbosity,
  95. )
  96. resolver = self.make_resolver(
  97. preparer=preparer,
  98. finder=finder,
  99. options=options,
  100. ignore_requires_python=options.ignore_requires_python,
  101. py_version_info=options.python_version,
  102. )
  103. self.trace_basic_info(finder)
  104. requirement_set = resolver.resolve(reqs, check_supported_wheels=True)
  105. downloaded: List[str] = []
  106. for req in requirement_set.requirements.values():
  107. if req.satisfied_by is None:
  108. assert req.name is not None
  109. preparer.save_linked_requirement(req)
  110. downloaded.append(req.name)
  111. if downloaded:
  112. write_output("Successfully downloaded %s", " ".join(downloaded))
  113. return SUCCESS