download.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.build_dir())
  33. self.cmd_opts.add_option(cmdoptions.no_deps())
  34. self.cmd_opts.add_option(cmdoptions.global_options())
  35. self.cmd_opts.add_option(cmdoptions.no_binary())
  36. self.cmd_opts.add_option(cmdoptions.only_binary())
  37. self.cmd_opts.add_option(cmdoptions.prefer_binary())
  38. self.cmd_opts.add_option(cmdoptions.src())
  39. self.cmd_opts.add_option(cmdoptions.pre())
  40. self.cmd_opts.add_option(cmdoptions.require_hashes())
  41. self.cmd_opts.add_option(cmdoptions.progress_bar())
  42. self.cmd_opts.add_option(cmdoptions.no_build_isolation())
  43. self.cmd_opts.add_option(cmdoptions.use_pep517())
  44. self.cmd_opts.add_option(cmdoptions.no_use_pep517())
  45. self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
  46. self.cmd_opts.add_option(
  47. '-d', '--dest', '--destination-dir', '--destination-directory',
  48. dest='download_dir',
  49. metavar='dir',
  50. default=os.curdir,
  51. help=("Download packages into <dir>."),
  52. )
  53. cmdoptions.add_target_python_options(self.cmd_opts)
  54. index_opts = cmdoptions.make_option_group(
  55. cmdoptions.index_group,
  56. self.parser,
  57. )
  58. self.parser.insert_option_group(0, index_opts)
  59. self.parser.insert_option_group(0, self.cmd_opts)
  60. @with_cleanup
  61. def run(self, options: Values, args: List[str]) -> int:
  62. options.ignore_installed = True
  63. # editable doesn't really make sense for `pip download`, but the bowels
  64. # of the RequirementSet code require that property.
  65. options.editables = []
  66. cmdoptions.check_dist_restriction(options)
  67. options.download_dir = normalize_path(options.download_dir)
  68. ensure_dir(options.download_dir)
  69. session = self.get_default_session(options)
  70. target_python = make_target_python(options)
  71. finder = self._build_package_finder(
  72. options=options,
  73. session=session,
  74. target_python=target_python,
  75. ignore_requires_python=options.ignore_requires_python,
  76. )
  77. req_tracker = self.enter_context(get_requirement_tracker())
  78. directory = TempDirectory(
  79. delete=not options.no_clean,
  80. kind="download",
  81. globally_managed=True,
  82. )
  83. reqs = self.get_requirements(args, options, finder, session)
  84. preparer = self.make_requirement_preparer(
  85. temp_build_dir=directory,
  86. options=options,
  87. req_tracker=req_tracker,
  88. session=session,
  89. finder=finder,
  90. download_dir=options.download_dir,
  91. use_user_site=False,
  92. )
  93. resolver = self.make_resolver(
  94. preparer=preparer,
  95. finder=finder,
  96. options=options,
  97. ignore_requires_python=options.ignore_requires_python,
  98. py_version_info=options.python_version,
  99. )
  100. self.trace_basic_info(finder)
  101. requirement_set = resolver.resolve(
  102. reqs, check_supported_wheels=True
  103. )
  104. downloaded: List[str] = []
  105. for req in requirement_set.requirements.values():
  106. if req.satisfied_by is None:
  107. assert req.name is not None
  108. preparer.save_linked_requirement(req)
  109. downloaded.append(req.name)
  110. if downloaded:
  111. write_output('Successfully downloaded %s', ' '.join(downloaded))
  112. return SUCCESS