command_context.py 796 B

1234567891011121314151617181920212223242526272829
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from contextlib import contextmanager
  4. from pip._vendor.contextlib2 import ExitStack
  5. class CommandContextMixIn(object):
  6. def __init__(self):
  7. super(CommandContextMixIn, self).__init__()
  8. self._in_main_context = False
  9. self._main_context = ExitStack()
  10. @contextmanager
  11. def main_context(self):
  12. assert not self._in_main_context
  13. self._in_main_context = True
  14. try:
  15. with self._main_context:
  16. yield
  17. finally:
  18. self._in_main_context = False
  19. def enter_context(self, context_provider):
  20. assert self._in_main_context
  21. return self._main_context.enter_context(context_provider)