discover.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Virtualenv-specific Discover base class for plugin-based Python discovery."""
  2. from __future__ import annotations
  3. import os
  4. from abc import ABC, abstractmethod
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from argparse import ArgumentParser
  8. from collections.abc import Mapping
  9. from python_discovery import PythonInfo
  10. from virtualenv.config.cli.parser import VirtualEnvOptions
  11. class Discover(ABC):
  12. @classmethod
  13. def add_parser_arguments(cls, parser: ArgumentParser) -> None:
  14. raise NotImplementedError
  15. def __init__(self, options: VirtualEnvOptions) -> None:
  16. self._has_run = False
  17. self._interpreter: PythonInfo | None = None
  18. self._env: Mapping[str, str] = options.env if options.env is not None else os.environ
  19. @abstractmethod
  20. def run(self) -> PythonInfo | None:
  21. raise NotImplementedError
  22. @property
  23. def interpreter(self) -> PythonInfo | None:
  24. """:returns: the interpreter as returned by :meth:`run`, cached"""
  25. if self._has_run is False:
  26. self._interpreter = self.run()
  27. self._has_run = True
  28. return self._interpreter
  29. __all__ = [
  30. "Discover",
  31. ]