Skip to content

Commit

Permalink
feat: turn EnvManager method into staticmethod which detect active py…
Browse files Browse the repository at this point in the history
…thon
  • Loading branch information
finswimmer committed Nov 26, 2022
1 parent dcd48c8 commit b712f3c
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ def __init__(self, poetry: Poetry, io: None | IO = None) -> None:
self._poetry = poetry
self._io = io or NullIO()

def _full_python_path(self, python: str) -> str:
@staticmethod
def _full_python_path(python: str) -> str:
try:
executable = decode(
subprocess.check_output(
Expand All @@ -536,32 +537,35 @@ def _full_python_path(self, python: str) -> str:

return executable

def _detect_active_python(self) -> str | None:
@staticmethod
def _detect_active_python(io: None | IO = None) -> str | None:
io = io or NullIO()
executable = None

try:
self._io.write_error_line(
io.write_error_line(
"Trying to detect current active python executable as specified in the"
" config.",
verbosity=Verbosity.VERBOSE,
)
executable = self._full_python_path("python")
self._io.write_error_line(
f"Found: {executable}", verbosity=Verbosity.VERBOSE
)
executable = EnvManager._full_python_path("python")
io.write_error_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
except CalledProcessError:
self._io.write_error_line(
io.write_error_line(
"Unable to detect the current active python executable. Falling back to"
" default.",
verbosity=Verbosity.VERBOSE,
)
return executable

def _get_python_version(self) -> tuple[int, int, int]:
@staticmethod
def get_python_version(
prefer_active_python: bool = False, io: None | IO = None
) -> tuple[int, int, int]:
version_info = tuple(sys.version_info[:3])

if self._poetry.config.get("virtualenvs.prefer-active-python"):
executable = self._detect_active_python()
if prefer_active_python:
executable = EnvManager._detect_active_python(io)

if executable:
python_patch = decode(
Expand Down Expand Up @@ -688,7 +692,17 @@ def get(self, reload: bool = False) -> Env:
if self._env is not None and not reload:
return self._env

python_minor = ".".join([str(v) for v in self._get_python_version()[:2]])
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
python_minor = ".".join(
[
str(v)
for v in self.get_python_version(
prefer_active_python=prefer_active_python, io=self._io
)[:2]
]
)

venv_path = self._poetry.config.virtualenvs_path

Expand Down

0 comments on commit b712f3c

Please sign in to comment.