download.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from pip.exceptions import CommandError
  5. from pip.index import FormatControl
  6. from pip.req import RequirementSet
  7. from pip.basecommand import RequirementCommand
  8. from pip import cmdoptions
  9. from pip.utils import ensure_dir, normalize_path
  10. from pip.utils.build import BuildDirectory
  11. from pip.utils.filesystem import check_path_owner
  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. name = 'download'
  24. usage = """
  25. %prog [options] <requirement specifier> [package-index-options] ...
  26. %prog [options] -r <requirements file> [package-index-options] ...
  27. %prog [options] [-e] <vcs project url> ...
  28. %prog [options] [-e] <local project path> ...
  29. %prog [options] <archive url/path> ..."""
  30. summary = 'Download packages.'
  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.editable())
  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.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(
  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. cmd_opts.add_option(
  54. '--platform',
  55. dest='platform',
  56. metavar='platform',
  57. default=None,
  58. help=("Only download wheels compatible with <platform>. "
  59. "Defaults to the platform of the running system."),
  60. )
  61. cmd_opts.add_option(
  62. '--python-version',
  63. dest='python_version',
  64. metavar='python_version',
  65. default=None,
  66. help=("Only download wheels compatible with Python "
  67. "interpreter version <version>. If not specified, then the "
  68. "current system interpreter minor version is used. A major "
  69. "version (e.g. '2') can be specified to match all "
  70. "minor revs of that major version. A minor version "
  71. "(e.g. '34') can also be specified."),
  72. )
  73. cmd_opts.add_option(
  74. '--implementation',
  75. dest='implementation',
  76. metavar='implementation',
  77. default=None,
  78. help=("Only download wheels compatible with Python "
  79. "implementation <implementation>, e.g. 'pp', 'jy', 'cp', "
  80. " or 'ip'. If not specified, then the current "
  81. "interpreter implementation is used. Use 'py' to force "
  82. "implementation-agnostic wheels."),
  83. )
  84. cmd_opts.add_option(
  85. '--abi',
  86. dest='abi',
  87. metavar='abi',
  88. default=None,
  89. help=("Only download wheels compatible with Python "
  90. "abi <abi>, e.g. 'pypy_41'. If not specified, then the "
  91. "current interpreter abi tag is used. Generally "
  92. "you will need to specify --implementation, "
  93. "--platform, and --python-version when using "
  94. "this option."),
  95. )
  96. index_opts = cmdoptions.make_option_group(
  97. cmdoptions.non_deprecated_index_group,
  98. self.parser,
  99. )
  100. self.parser.insert_option_group(0, index_opts)
  101. self.parser.insert_option_group(0, cmd_opts)
  102. def run(self, options, args):
  103. options.ignore_installed = True
  104. if options.python_version:
  105. python_versions = [options.python_version]
  106. else:
  107. python_versions = None
  108. dist_restriction_set = any([
  109. options.python_version,
  110. options.platform,
  111. options.abi,
  112. options.implementation,
  113. ])
  114. binary_only = FormatControl(set(), set([':all:']))
  115. if dist_restriction_set and options.format_control != binary_only:
  116. raise CommandError(
  117. "--only-binary=:all: must be set and --no-binary must not "
  118. "be set (or must be set to :none:) when restricting platform "
  119. "and interpreter constraints using --python-version, "
  120. "--platform, --abi, or --implementation."
  121. )
  122. options.src_dir = os.path.abspath(options.src_dir)
  123. options.download_dir = normalize_path(options.download_dir)
  124. ensure_dir(options.download_dir)
  125. with self._build_session(options) as session:
  126. finder = self._build_package_finder(
  127. options=options,
  128. session=session,
  129. platform=options.platform,
  130. python_versions=python_versions,
  131. abi=options.abi,
  132. implementation=options.implementation,
  133. )
  134. build_delete = (not (options.no_clean or options.build_dir))
  135. if options.cache_dir and not check_path_owner(options.cache_dir):
  136. logger.warning(
  137. "The directory '%s' or its parent directory is not owned "
  138. "by the current user and caching wheels has been "
  139. "disabled. check the permissions and owner of that "
  140. "directory. If executing pip with sudo, you may want "
  141. "sudo's -H flag.",
  142. options.cache_dir,
  143. )
  144. options.cache_dir = None
  145. with BuildDirectory(options.build_dir,
  146. delete=build_delete) as build_dir:
  147. requirement_set = RequirementSet(
  148. build_dir=build_dir,
  149. src_dir=options.src_dir,
  150. download_dir=options.download_dir,
  151. ignore_installed=True,
  152. ignore_dependencies=options.ignore_dependencies,
  153. session=session,
  154. isolated=options.isolated_mode,
  155. require_hashes=options.require_hashes
  156. )
  157. self.populate_requirement_set(
  158. requirement_set,
  159. args,
  160. options,
  161. finder,
  162. session,
  163. self.name,
  164. None
  165. )
  166. if not requirement_set.has_requirements:
  167. return
  168. requirement_set.prepare_files(finder)
  169. downloaded = ' '.join([
  170. req.name for req in requirement_set.successfully_downloaded
  171. ])
  172. if downloaded:
  173. logger.info(
  174. 'Successfully downloaded %s', downloaded
  175. )
  176. # Clean up
  177. if not options.no_clean:
  178. requirement_set.cleanup_files()
  179. return requirement_set