download.py 4.6 KB

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