You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

378 lines
14 KiB

  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. try:
  9. import wheel
  10. except ImportError:
  11. wheel = None
  12. from pip.req import RequirementSet
  13. from pip.basecommand import RequirementCommand
  14. from pip.locations import virtualenv_no_global, distutils_scheme
  15. from pip.index import PackageFinder
  16. from pip.exceptions import (
  17. InstallationError, CommandError, PreviousBuildDirError,
  18. )
  19. from pip import cmdoptions
  20. from pip.utils import ensure_dir
  21. from pip.utils.build import BuildDirectory
  22. from pip.utils.deprecation import RemovedInPip8Warning
  23. from pip.wheel import WheelCache, WheelBuilder
  24. logger = logging.getLogger(__name__)
  25. class InstallCommand(RequirementCommand):
  26. """
  27. Install packages from:
  28. - PyPI (and other indexes) using requirement specifiers.
  29. - VCS project urls.
  30. - Local project directories.
  31. - Local or remote source archives.
  32. pip also supports installing from "requirements files", which provide
  33. an easy way to specify a whole environment to be installed.
  34. """
  35. name = 'install'
  36. usage = """
  37. %prog [options] <requirement specifier> [package-index-options] ...
  38. %prog [options] -r <requirements file> [package-index-options] ...
  39. %prog [options] [-e] <vcs project url> ...
  40. %prog [options] [-e] <local project path> ...
  41. %prog [options] <archive url/path> ..."""
  42. summary = 'Install packages.'
  43. def __init__(self, *args, **kw):
  44. super(InstallCommand, self).__init__(*args, **kw)
  45. cmd_opts = self.cmd_opts
  46. cmd_opts.add_option(cmdoptions.constraints())
  47. cmd_opts.add_option(cmdoptions.editable())
  48. cmd_opts.add_option(cmdoptions.requirements())
  49. cmd_opts.add_option(cmdoptions.build_dir())
  50. cmd_opts.add_option(
  51. '-t', '--target',
  52. dest='target_dir',
  53. metavar='dir',
  54. default=None,
  55. help='Install packages into <dir>. '
  56. 'By default this will not replace existing files/folders in '
  57. '<dir>. Use --upgrade to replace existing packages in <dir> '
  58. 'with new versions.'
  59. )
  60. cmd_opts.add_option(
  61. '-d', '--download', '--download-dir', '--download-directory',
  62. dest='download_dir',
  63. metavar='dir',
  64. default=None,
  65. help=("Download packages into <dir> instead of installing them, "
  66. "regardless of what's already installed."),
  67. )
  68. cmd_opts.add_option(cmdoptions.download_cache())
  69. cmd_opts.add_option(cmdoptions.src())
  70. cmd_opts.add_option(
  71. '-U', '--upgrade',
  72. dest='upgrade',
  73. action='store_true',
  74. help='Upgrade all specified packages to the newest available '
  75. 'version. This process is recursive regardless of whether '
  76. 'a dependency is already satisfied.'
  77. )
  78. cmd_opts.add_option(
  79. '--force-reinstall',
  80. dest='force_reinstall',
  81. action='store_true',
  82. help='When upgrading, reinstall all packages even if they are '
  83. 'already up-to-date.')
  84. cmd_opts.add_option(
  85. '-I', '--ignore-installed',
  86. dest='ignore_installed',
  87. action='store_true',
  88. help='Ignore the installed packages (reinstalling instead).')
  89. cmd_opts.add_option(cmdoptions.no_deps())
  90. cmd_opts.add_option(cmdoptions.install_options())
  91. cmd_opts.add_option(cmdoptions.global_options())
  92. cmd_opts.add_option(
  93. '--user',
  94. dest='use_user_site',
  95. action='store_true',
  96. help="Install to the Python user install directory for your "
  97. "platform. Typically ~/.local/, or %APPDATA%\Python on "
  98. "Windows. (See the Python documentation for site.USER_BASE "
  99. "for full details.)")
  100. cmd_opts.add_option(
  101. '--egg',
  102. dest='as_egg',
  103. action='store_true',
  104. help="Install packages as eggs, not 'flat', like pip normally "
  105. "does. This option is not about installing *from* eggs. "
  106. "(WARNING: Because this option overrides pip's normal install"
  107. " logic, requirements files may not behave as expected.)")
  108. cmd_opts.add_option(
  109. '--root',
  110. dest='root_path',
  111. metavar='dir',
  112. default=None,
  113. help="Install everything relative to this alternate root "
  114. "directory.")
  115. cmd_opts.add_option(
  116. '--strip-file-prefix',
  117. dest='strip_file_prefix',
  118. metavar='prefix',
  119. default=None,
  120. help="Strip given prefix from script paths in wheel RECORD."
  121. )
  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())
  136. cmd_opts.add_option(cmdoptions.no_use_wheel())
  137. cmd_opts.add_option(cmdoptions.no_binary())
  138. cmd_opts.add_option(cmdoptions.only_binary())
  139. cmd_opts.add_option(
  140. '--pre',
  141. action='store_true',
  142. default=False,
  143. help="Include pre-release and development versions. By default, "
  144. "pip only finds stable versions.")
  145. cmd_opts.add_option(cmdoptions.no_clean())
  146. index_opts = cmdoptions.make_option_group(
  147. cmdoptions.index_group,
  148. self.parser,
  149. )
  150. self.parser.insert_option_group(0, index_opts)
  151. self.parser.insert_option_group(0, cmd_opts)
  152. def _build_package_finder(self, options, index_urls, session):
  153. """
  154. Create a package finder appropriate to this install command.
  155. This method is meant to be overridden by subclasses, not
  156. called directly.
  157. """
  158. return PackageFinder(
  159. find_links=options.find_links,
  160. format_control=options.format_control,
  161. index_urls=index_urls,
  162. allow_external=options.allow_external,
  163. allow_unverified=options.allow_unverified,
  164. allow_all_external=options.allow_all_external,
  165. trusted_hosts=options.trusted_hosts,
  166. allow_all_prereleases=options.pre,
  167. process_dependency_links=options.process_dependency_links,
  168. session=session,
  169. )
  170. def run(self, options, args):
  171. cmdoptions.resolve_wheel_no_use_binary(options)
  172. cmdoptions.check_install_build_global(options)
  173. if options.download_dir:
  174. options.ignore_installed = True
  175. if options.build_dir:
  176. options.build_dir = os.path.abspath(options.build_dir)
  177. options.src_dir = os.path.abspath(options.src_dir)
  178. install_options = options.install_options or []
  179. if options.use_user_site:
  180. if virtualenv_no_global():
  181. raise InstallationError(
  182. "Can not perform a '--user' install. User site-packages "
  183. "are not visible in this virtualenv."
  184. )
  185. install_options.append('--user')
  186. install_options.append('--prefix=')
  187. temp_target_dir = None
  188. if options.target_dir:
  189. options.ignore_installed = True
  190. temp_target_dir = tempfile.mkdtemp()
  191. options.target_dir = os.path.abspath(options.target_dir)
  192. if (os.path.exists(options.target_dir) and not
  193. os.path.isdir(options.target_dir)):
  194. raise CommandError(
  195. "Target path exists but is not a directory, will not "
  196. "continue."
  197. )
  198. install_options.append('--home=' + temp_target_dir)
  199. global_options = options.global_options or []
  200. index_urls = [options.index_url] + options.extra_index_urls
  201. if options.no_index:
  202. logger.info('Ignoring indexes: %s', ','.join(index_urls))
  203. index_urls = []
  204. if options.download_cache:
  205. warnings.warn(
  206. "--download-cache has been deprecated and will be removed in "
  207. "the future. Pip now automatically uses and configures its "
  208. "cache.",
  209. RemovedInPip8Warning,
  210. )
  211. with self._build_session(options) as session:
  212. finder = self._build_package_finder(options, index_urls, session)
  213. build_delete = (not (options.no_clean or options.build_dir))
  214. wheel_cache = WheelCache(options.cache_dir, options.format_control)
  215. with BuildDirectory(options.build_dir,
  216. delete=build_delete) as build_dir:
  217. requirement_set = RequirementSet(
  218. build_dir=build_dir,
  219. src_dir=options.src_dir,
  220. download_dir=options.download_dir,
  221. upgrade=options.upgrade,
  222. as_egg=options.as_egg,
  223. ignore_installed=options.ignore_installed,
  224. ignore_dependencies=options.ignore_dependencies,
  225. force_reinstall=options.force_reinstall,
  226. use_user_site=options.use_user_site,
  227. target_dir=temp_target_dir,
  228. session=session,
  229. pycompile=options.compile,
  230. isolated=options.isolated_mode,
  231. wheel_cache=wheel_cache,
  232. )
  233. self.populate_requirement_set(
  234. requirement_set, args, options, finder, session, self.name,
  235. wheel_cache
  236. )
  237. if not requirement_set.has_requirements:
  238. return
  239. try:
  240. if (options.download_dir or not wheel or not
  241. options.cache_dir):
  242. # on -d don't do complex things like building
  243. # wheels, and don't try to build wheels when wheel is
  244. # not installed.
  245. requirement_set.prepare_files(finder)
  246. else:
  247. # build wheels before install.
  248. wb = WheelBuilder(
  249. requirement_set,
  250. finder,
  251. build_options=[],
  252. global_options=[],
  253. )
  254. # Ignore the result: a failed wheel will be
  255. # installed from the sdist/vcs whatever.
  256. wb.build(autobuilding=True)
  257. if not options.download_dir:
  258. requirement_set.install(
  259. install_options,
  260. global_options,
  261. root=options.root_path,
  262. strip_file_prefix=options.strip_file_prefix,
  263. )
  264. reqs = sorted(
  265. requirement_set.successfully_installed,
  266. key=operator.attrgetter('name'))
  267. items = []
  268. for req in reqs:
  269. item = req.name
  270. try:
  271. if hasattr(req, 'installed_version'):
  272. if req.installed_version:
  273. item += '-' + req.installed_version
  274. except Exception:
  275. pass
  276. items.append(item)
  277. installed = ' '.join(items)
  278. if installed:
  279. logger.info('Successfully installed %s', installed)
  280. else:
  281. downloaded = ' '.join([
  282. req.name
  283. for req in requirement_set.successfully_downloaded
  284. ])
  285. if downloaded:
  286. logger.info(
  287. 'Successfully downloaded %s', downloaded
  288. )
  289. except PreviousBuildDirError:
  290. options.no_clean = True
  291. raise
  292. finally:
  293. # Clean up
  294. if not options.no_clean:
  295. requirement_set.cleanup_files()
  296. if options.target_dir:
  297. ensure_dir(options.target_dir)
  298. lib_dir = distutils_scheme('', home=temp_target_dir)['purelib']
  299. for item in os.listdir(lib_dir):
  300. target_item_dir = os.path.join(options.target_dir, item)
  301. if os.path.exists(target_item_dir):
  302. if not options.upgrade:
  303. logger.warning(
  304. 'Target directory %s already exists. Specify '
  305. '--upgrade to force replacement.',
  306. target_item_dir
  307. )
  308. continue
  309. if os.path.islink(target_item_dir):
  310. logger.warning(
  311. 'Target directory %s already exists and is '
  312. 'a link. Pip will not automatically replace '
  313. 'links, please remove if replacement is '
  314. 'desired.',
  315. target_item_dir
  316. )
  317. continue
  318. if os.path.isdir(target_item_dir):
  319. shutil.rmtree(target_item_dir)
  320. else:
  321. os.remove(target_item_dir)
  322. shutil.move(
  323. os.path.join(lib_dir, item),
  324. target_item_dir
  325. )
  326. shutil.rmtree(temp_target_dir)
  327. return requirement_set