pyenv.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. from __future__ import annotations
  2. import os
  3. import typing as t
  4. from pathlib import Path
  5. from findpython.providers.base import BaseProvider
  6. from findpython.python import PythonVersion
  7. class PyenvProvider(BaseProvider):
  8. """A provider that finds python installed with pyenv"""
  9. def __init__(self, root: Path) -> None:
  10. self.root = root
  11. @classmethod
  12. def create(cls) -> t.Self | None:
  13. pyenv_root = os.path.expanduser(
  14. os.path.expandvars(os.getenv("PYENV_ROOT", "~/.pyenv"))
  15. )
  16. if not os.path.exists(pyenv_root):
  17. return None
  18. return cls(Path(pyenv_root))
  19. def find_pythons(self) -> t.Iterable[PythonVersion]:
  20. versions_path = self.root.joinpath("versions")
  21. if versions_path.exists():
  22. for version in versions_path.iterdir():
  23. if version.is_dir():
  24. bindir = version / "bin"
  25. if not bindir.exists():
  26. bindir = version
  27. yield from self.find_pythons_from_path(bindir, True)