__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. class InstallationResult(object):
  18. def __init__(self, name):
  19. # type: (str) -> None
  20. self.name = name
  21. def __repr__(self):
  22. # type: () -> str
  23. return "InstallationResult(name={!r})".format(self.name)
  24. def install_given_reqs(
  25. to_install, # type: List[InstallRequirement]
  26. install_options, # type: List[str]
  27. global_options=(), # type: Sequence[str]
  28. *args, # type: Any
  29. **kwargs # type: Any
  30. ):
  31. # type: (...) -> List[InstallationResult]
  32. """
  33. Install everything in the given list.
  34. (to be called after having downloaded and unpacked the packages)
  35. """
  36. if to_install:
  37. logger.info(
  38. 'Installing collected packages: %s',
  39. ', '.join([req.name for req in to_install]),
  40. )
  41. installed = []
  42. with indent_log():
  43. for requirement in to_install:
  44. if requirement.should_reinstall:
  45. logger.info('Attempting uninstall: %s', requirement.name)
  46. with indent_log():
  47. uninstalled_pathset = requirement.uninstall(
  48. auto_confirm=True
  49. )
  50. try:
  51. requirement.install(
  52. install_options,
  53. global_options,
  54. *args,
  55. **kwargs
  56. )
  57. except Exception:
  58. should_rollback = (
  59. requirement.should_reinstall and
  60. not requirement.install_succeeded
  61. )
  62. # if install did not succeed, rollback previous uninstall
  63. if should_rollback:
  64. uninstalled_pathset.rollback()
  65. raise
  66. else:
  67. should_commit = (
  68. requirement.should_reinstall and
  69. requirement.install_succeeded
  70. )
  71. if should_commit:
  72. uninstalled_pathset.commit()
  73. installed.append(InstallationResult(requirement.name))
  74. return installed