__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # The following comment should be removed at some point in the future.
  2. # mypy: strict-optional=False
  3. from __future__ import absolute_import
  4. import logging
  5. from pip._internal.utils.logging import indent_log
  6. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  7. from .req_file import parse_requirements
  8. from .req_install import InstallRequirement
  9. from .req_set import RequirementSet
  10. if MYPY_CHECK_RUNNING:
  11. from typing import Any, List, Sequence
  12. __all__ = [
  13. "RequirementSet", "InstallRequirement",
  14. "parse_requirements", "install_given_reqs",
  15. ]
  16. logger = logging.getLogger(__name__)
  17. def install_given_reqs(
  18. to_install, # type: List[InstallRequirement]
  19. install_options, # type: List[str]
  20. global_options=(), # type: Sequence[str]
  21. *args, # type: Any
  22. **kwargs # type: Any
  23. ):
  24. # type: (...) -> List[InstallRequirement]
  25. """
  26. Install everything in the given list.
  27. (to be called after having downloaded and unpacked the packages)
  28. """
  29. if to_install:
  30. logger.info(
  31. 'Installing collected packages: %s',
  32. ', '.join([req.name for req in to_install]),
  33. )
  34. with indent_log():
  35. for requirement in to_install:
  36. if requirement.conflicts_with:
  37. logger.info(
  38. 'Found existing installation: %s',
  39. requirement.conflicts_with,
  40. )
  41. with indent_log():
  42. uninstalled_pathset = requirement.uninstall(
  43. auto_confirm=True
  44. )
  45. try:
  46. requirement.install(
  47. install_options,
  48. global_options,
  49. *args,
  50. **kwargs
  51. )
  52. except Exception:
  53. should_rollback = (
  54. requirement.conflicts_with and
  55. not requirement.install_succeeded
  56. )
  57. # if install did not succeed, rollback previous uninstall
  58. if should_rollback:
  59. uninstalled_pathset.rollback()
  60. raise
  61. else:
  62. should_commit = (
  63. requirement.conflicts_with and
  64. requirement.install_succeeded
  65. )
  66. if should_commit:
  67. uninstalled_pathset.commit()
  68. requirement.remove_temporary_source()
  69. return to_install