install.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. from __future__ import absolute_import
  2. import logging
  3. import operator
  4. import os
  5. import tempfile
  6. import shutil
  7. import warnings
  8. from pip.req import InstallRequirement, RequirementSet, parse_requirements
  9. from pip.locations import build_prefix, virtualenv_no_global, distutils_scheme
  10. from pip.basecommand import Command
  11. from pip.index import PackageFinder
  12. from pip.exceptions import (
  13. InstallationError, CommandError, PreviousBuildDirError,
  14. )
  15. from pip import cmdoptions
  16. from pip.utils.build import BuildDirectory
  17. from pip.utils.deprecation import RemovedInPip7Warning, RemovedInPip8Warning
  18. logger = logging.getLogger(__name__)
  19. class InstallCommand(Command):
  20. """
  21. Install packages from:
  22. - PyPI (and other indexes) using requirement specifiers.
  23. - VCS project urls.
  24. - Local project directories.
  25. - Local or remote source archives.
  26. pip also supports installing from "requirements files", which provide
  27. an easy way to specify a whole environment to be installed.
  28. """
  29. name = 'install'
  30. usage = """
  31. %prog [options] <requirement specifier> [package-index-options] ...
  32. %prog [options] -r <requirements file> [package-index-options] ...
  33. %prog [options] [-e] <vcs project url> ...
  34. %prog [options] [-e] <local project path> ...
  35. %prog [options] <archive url/path> ..."""
  36. summary = 'Install packages.'
  37. def __init__(self, *args, **kw):
  38. super(InstallCommand, self).__init__(*args, **kw)
  39. cmd_opts = self.cmd_opts
  40. cmd_opts.add_option(cmdoptions.editable.make())
  41. cmd_opts.add_option(cmdoptions.requirements.make())
  42. cmd_opts.add_option(cmdoptions.build_dir.make())
  43. cmd_opts.add_option(
  44. '-t', '--target',
  45. dest='target_dir',
  46. metavar='dir',
  47. default=None,
  48. help='Install packages into <dir>. '
  49. 'By default this will not replace existing files/folders in '
  50. '<dir>. Use --upgrade to replace existing packages in <dir> '
  51. 'with new versions.'
  52. )
  53. cmd_opts.add_option(
  54. '-d', '--download', '--download-dir', '--download-directory',
  55. dest='download_dir',
  56. metavar='dir',
  57. default=None,
  58. help=("Download packages into <dir> instead of installing them, "
  59. "regardless of what's already installed."),
  60. )
  61. cmd_opts.add_option(cmdoptions.download_cache.make())
  62. cmd_opts.add_option(cmdoptions.src.make())
  63. cmd_opts.add_option(
  64. '-U', '--upgrade',
  65. dest='upgrade',
  66. action='store_true',
  67. help='Upgrade all specified packages to the newest available '
  68. 'version. This process is recursive regardless of whether '
  69. 'a dependency is already satisfied.'
  70. )
  71. cmd_opts.add_option(
  72. '--force-reinstall',
  73. dest='force_reinstall',
  74. action='store_true',
  75. help='When upgrading, reinstall all packages even if they are '
  76. 'already up-to-date.')
  77. cmd_opts.add_option(
  78. '-I', '--ignore-installed',
  79. dest='ignore_installed',
  80. action='store_true',
  81. help='Ignore the installed packages (reinstalling instead).')
  82. cmd_opts.add_option(cmdoptions.no_deps.make())
  83. cmd_opts.add_option(
  84. '--no-install',
  85. dest='no_install',
  86. action='store_true',
  87. help="DEPRECATED. Download and unpack all packages, but don't "
  88. "actually install them."
  89. )
  90. cmd_opts.add_option(
  91. '--no-download',
  92. dest='no_download',
  93. action="store_true",
  94. help="DEPRECATED. Don't download any packages, just install the "
  95. "ones already downloaded (completes an install run with "
  96. "--no-install).")
  97. cmd_opts.add_option(cmdoptions.install_options.make())
  98. cmd_opts.add_option(cmdoptions.global_options.make())
  99. cmd_opts.add_option(
  100. '--user',
  101. dest='use_user_site',
  102. action='store_true',
  103. help="Install to the Python user install directory for your "
  104. "platform. Typically ~/.local/, or %APPDATA%\Python on "
  105. "Windows. (See the Python documentation for site.USER_BASE "
  106. "for full details.)")
  107. cmd_opts.add_option(
  108. '--egg',
  109. dest='as_egg',
  110. action='store_true',
  111. help="Install packages as eggs, not 'flat', like pip normally "
  112. "does. This option is not about installing *from* eggs. "
  113. "(WARNING: Because this option overrides pip's normal install"
  114. " logic, requirements files may not behave as expected.)")
  115. cmd_opts.add_option(
  116. '--root',
  117. dest='root_path',
  118. metavar='dir',
  119. default=None,
  120. help="Install everything relative to this alternate root "
  121. "directory.")
  122. cmd_opts.add_option(
  123. "--compile",
  124. action="store_true",
  125. dest="compile",
  126. default=True,
  127. help="Compile py files to pyc",
  128. )
  129. cmd_opts.add_option(
  130. "--no-compile",
  131. action="store_false",
  132. dest="compile",
  133. help="Do not compile py files to pyc",
  134. )
  135. cmd_opts.add_option(cmdoptions.use_wheel.make())
  136. cmd_opts.add_option(cmdoptions.no_use_wheel.make())
  137. cmd_opts.add_option(
  138. '--pre',
  139. action='store_true',
  140. default=False,
  141. help="Include pre-release and development versions. By default, "
  142. "pip only finds stable versions.")
  143. cmd_opts.add_option(cmdoptions.no_clean.make())
  144. index_opts = cmdoptions.make_option_group(
  145. cmdoptions.index_group,
  146. self.parser,
  147. )
  148. self.parser.insert_option_group(0, index_opts)
  149. self.parser.insert_option_group(0, cmd_opts)
  150. def _build_package_finder(self, options, index_urls, session):
  151. """
  152. Create a package finder appropriate to this install command.
  153. This method is meant to be overridden by subclasses, not
  154. called directly.
  155. """
  156. return PackageFinder(
  157. find_links=options.find_links,
  158. index_urls=index_urls,
  159. use_wheel=options.use_wheel,
  160. allow_external=options.allow_external,
  161. allow_unverified=options.allow_unverified,
  162. allow_all_external=options.allow_all_external,
  163. trusted_hosts=options.trusted_hosts,
  164. allow_all_prereleases=options.pre,
  165. process_dependency_links=options.process_dependency_links,
  166. session=session,
  167. )
  168. def run(self, options, args):
  169. if (
  170. options.no_install or
  171. options.no_download
  172. ):
  173. warnings.warn(
  174. "--no-install and --no-download are deprecated. "
  175. "See https://github.com/pypa/pip/issues/906.",
  176. RemovedInPip7Warning,
  177. )
  178. # If we have --no-install or --no-download and no --build we use the
  179. # legacy static build dir
  180. if (options.build_dir is None and
  181. (options.no_install or options.no_download)):
  182. options.build_dir = build_prefix
  183. if options.download_dir:
  184. options.no_install = True
  185. options.ignore_installed = True
  186. if options.build_dir:
  187. options.build_dir = os.path.abspath(options.build_dir)
  188. options.src_dir = os.path.abspath(options.src_dir)
  189. install_options = options.install_options or []
  190. if options.use_user_site:
  191. if virtualenv_no_global():
  192. raise InstallationError(
  193. "Can not perform a '--user' install. User site-packages "
  194. "are not visible in this virtualenv."
  195. )
  196. install_options.append('--user')
  197. temp_target_dir = None
  198. if options.target_dir:
  199. options.ignore_installed = True
  200. temp_target_dir = tempfile.mkdtemp()
  201. options.target_dir = os.path.abspath(options.target_dir)
  202. if (os.path.exists(options.target_dir) and not
  203. os.path.isdir(options.target_dir)):
  204. raise CommandError(
  205. "Target path exists but is not a directory, will not "
  206. "continue."
  207. )
  208. install_options.append('--home=' + temp_target_dir)
  209. global_options = options.global_options or []
  210. index_urls = [options.index_url] + options.extra_index_urls
  211. if options.no_index:
  212. logger.info('Ignoring indexes: %s', ','.join(index_urls))
  213. index_urls = []
  214. if options.use_mirrors:
  215. warnings.warn(
  216. "--use-mirrors has been deprecated and will be removed in the "
  217. "future. Explicit uses of --index-url and/or --extra-index-url"
  218. " is suggested.",
  219. RemovedInPip7Warning,
  220. )
  221. if options.mirrors:
  222. warnings.warn(
  223. "--mirrors has been deprecated and will be removed in the "
  224. "future. Explicit uses of --index-url and/or --extra-index-url"
  225. " is suggested.",
  226. RemovedInPip7Warning,
  227. )
  228. index_urls += options.mirrors
  229. if options.download_cache:
  230. warnings.warn(
  231. "--download-cache has been deprecated and will be removed in "
  232. "the future. Pip now automatically uses and configures its "
  233. "cache.",
  234. RemovedInPip8Warning,
  235. )
  236. with self._build_session(options) as session:
  237. finder = self._build_package_finder(options, index_urls, session)
  238. build_delete = (not (options.no_clean or options.build_dir))
  239. with BuildDirectory(options.build_dir,
  240. delete=build_delete) as build_dir:
  241. requirement_set = RequirementSet(
  242. build_dir=build_dir,
  243. src_dir=options.src_dir,
  244. download_dir=options.download_dir,
  245. upgrade=options.upgrade,
  246. as_egg=options.as_egg,
  247. ignore_installed=options.ignore_installed,
  248. ignore_dependencies=options.ignore_dependencies,
  249. force_reinstall=options.force_reinstall,
  250. use_user_site=options.use_user_site,
  251. target_dir=temp_target_dir,
  252. session=session,
  253. pycompile=options.compile,
  254. isolated=options.isolated_mode,
  255. )
  256. for name in args:
  257. requirement_set.add_requirement(
  258. InstallRequirement.from_line(
  259. name, None, isolated=options.isolated_mode,
  260. )
  261. )
  262. for name in options.editables:
  263. requirement_set.add_requirement(
  264. InstallRequirement.from_editable(
  265. name,
  266. default_vcs=options.default_vcs,
  267. isolated=options.isolated_mode,
  268. )
  269. )
  270. for filename in options.requirements:
  271. for req in parse_requirements(
  272. filename,
  273. finder=finder, options=options, session=session):
  274. requirement_set.add_requirement(req)
  275. if not requirement_set.has_requirements:
  276. opts = {'name': self.name}
  277. if options.find_links:
  278. msg = ('You must give at least one requirement to '
  279. '%(name)s (maybe you meant "pip %(name)s '
  280. '%(links)s"?)' %
  281. dict(opts, links=' '.join(options.find_links)))
  282. else:
  283. msg = ('You must give at least one requirement '
  284. 'to %(name)s (see "pip help %(name)s")' % opts)
  285. logger.warning(msg)
  286. return
  287. try:
  288. if not options.no_download:
  289. requirement_set.prepare_files(finder)
  290. else:
  291. # This is the only call site of locate_files. Nuke with
  292. # fire.
  293. requirement_set.locate_files()
  294. if not options.no_install:
  295. requirement_set.install(
  296. install_options,
  297. global_options,
  298. root=options.root_path,
  299. )
  300. reqs = sorted(
  301. requirement_set.successfully_installed,
  302. key=operator.attrgetter('name'))
  303. items = []
  304. for req in reqs:
  305. item = req.name
  306. try:
  307. if hasattr(req, 'installed_version'):
  308. if req.installed_version:
  309. item += '-' + req.installed_version
  310. except Exception:
  311. pass
  312. items.append(item)
  313. installed = ' '.join(items)
  314. if installed:
  315. logger.info('Successfully installed %s', installed)
  316. else:
  317. downloaded = ' '.join([
  318. req.name
  319. for req in requirement_set.successfully_downloaded
  320. ])
  321. if downloaded:
  322. logger.info(
  323. 'Successfully downloaded %s', downloaded
  324. )
  325. except PreviousBuildDirError:
  326. options.no_clean = True
  327. raise
  328. finally:
  329. # Clean up
  330. if ((not options.no_clean) and
  331. ((not options.no_install) or
  332. options.download_dir)):
  333. requirement_set.cleanup_files()
  334. if options.target_dir:
  335. if not os.path.exists(options.target_dir):
  336. os.makedirs(options.target_dir)
  337. lib_dir = distutils_scheme('', home=temp_target_dir)['purelib']
  338. for item in os.listdir(lib_dir):
  339. target_item_dir = os.path.join(options.target_dir, item)
  340. if os.path.exists(target_item_dir):
  341. if not options.upgrade:
  342. logger.warning(
  343. 'Target directory %s already exists. Specify '
  344. '--upgrade to force replacement.',
  345. target_item_dir
  346. )
  347. continue
  348. if os.path.islink(target_item_dir):
  349. logger.warning(
  350. 'Target directory %s already exists and is '
  351. 'a link. Pip will not automatically replace '
  352. 'links, please remove if replacement is '
  353. 'desired.',
  354. target_item_dir
  355. )
  356. continue
  357. if os.path.isdir(target_item_dir):
  358. shutil.rmtree(target_item_dir)
  359. else:
  360. os.remove(target_item_dir)
  361. shutil.move(
  362. os.path.join(lib_dir, item),
  363. target_item_dir
  364. )
  365. shutil.rmtree(temp_target_dir)
  366. return requirement_set