discovery.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import annotations
  2. from .base import PluginLoader
  3. class Discovery(PluginLoader):
  4. """Discovery plugins."""
  5. def get_discover(parser, args):
  6. discover_types = Discovery.entry_points_for("virtualenv.discovery")
  7. discovery_parser = parser.add_argument_group(
  8. title="discovery",
  9. description="discover and provide a target interpreter",
  10. )
  11. choices = _get_default_discovery(discover_types)
  12. # prefer the builtin if present, otherwise fallback to first defined type
  13. choices = sorted(choices, key=lambda a: 0 if a == "builtin" else 1)
  14. try:
  15. default_discovery = next(iter(choices))
  16. except StopIteration as e:
  17. msg = "No discovery plugin found. Try reinstalling virtualenv to fix this issue."
  18. raise RuntimeError(msg) from e
  19. discovery_parser.add_argument(
  20. "--discovery",
  21. choices=choices,
  22. default=default_discovery,
  23. required=False,
  24. help="interpreter discovery method",
  25. )
  26. options, _ = parser.parse_known_args(args)
  27. discover_class = discover_types[options.discovery]
  28. discover_class.add_parser_arguments(discovery_parser)
  29. options, _ = parser.parse_known_args(args, namespace=options)
  30. return discover_class(options)
  31. def _get_default_discovery(discover_types):
  32. return list(discover_types.keys())
  33. __all__ = [
  34. "Discovery",
  35. "get_discover",
  36. ]