__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from __future__ import absolute_import
  2. import collections
  3. import logging
  4. from pip._internal.utils.logging import indent_log
  5. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  6. from .req_file import parse_requirements
  7. from .req_install import InstallRequirement
  8. from .req_set import RequirementSet
  9. if MYPY_CHECK_RUNNING:
  10. from typing import Iterator, List, Optional, Sequence, Tuple
  11. __all__ = [
  12. "RequirementSet", "InstallRequirement",
  13. "parse_requirements", "install_given_reqs",
  14. ]
  15. logger = logging.getLogger(__name__)
  16. class InstallationResult(object):
  17. def __init__(self, name):
  18. # type: (str) -> None
  19. self.name = name
  20. def __repr__(self):
  21. # type: () -> str
  22. return "InstallationResult(name={!r})".format(self.name)
  23. def _validate_requirements(
  24. requirements, # type: List[InstallRequirement]
  25. ):
  26. # type: (...) -> Iterator[Tuple[str, InstallRequirement]]
  27. for req in requirements:
  28. assert req.name, "invalid to-be-installed requirement: {}".format(req)
  29. yield req.name, req
  30. def install_given_reqs(
  31. requirements, # type: List[InstallRequirement]
  32. install_options, # type: List[str]
  33. global_options, # type: Sequence[str]
  34. root, # type: Optional[str]
  35. home, # type: Optional[str]
  36. prefix, # type: Optional[str]
  37. warn_script_location, # type: bool
  38. use_user_site, # type: bool
  39. pycompile, # type: bool
  40. ):
  41. # type: (...) -> List[InstallationResult]
  42. """
  43. Install everything in the given list.
  44. (to be called after having downloaded and unpacked the packages)
  45. """
  46. to_install = collections.OrderedDict(_validate_requirements(requirements))
  47. if to_install:
  48. logger.info(
  49. 'Installing collected packages: %s',
  50. ', '.join(to_install.keys()),
  51. )
  52. installed = []
  53. with indent_log():
  54. for req_name, requirement in to_install.items():
  55. if requirement.should_reinstall:
  56. logger.info('Attempting uninstall: %s', req_name)
  57. with indent_log():
  58. uninstalled_pathset = requirement.uninstall(
  59. auto_confirm=True
  60. )
  61. else:
  62. uninstalled_pathset = None
  63. try:
  64. requirement.install(
  65. install_options,
  66. global_options,
  67. root=root,
  68. home=home,
  69. prefix=prefix,
  70. warn_script_location=warn_script_location,
  71. use_user_site=use_user_site,
  72. pycompile=pycompile,
  73. )
  74. except Exception:
  75. # if install did not succeed, rollback previous uninstall
  76. if uninstalled_pathset and not requirement.install_succeeded:
  77. uninstalled_pathset.rollback()
  78. raise
  79. else:
  80. if uninstalled_pathset and requirement.install_succeeded:
  81. uninstalled_pathset.commit()
  82. installed.append(InstallationResult(req_name))
  83. return installed