Commit f57cdc31 by finswimmer Committed by Randy Döring

feat: turn EnvManager method into staticmethod which detect active python

parent 98568d49
...@@ -20,7 +20,6 @@ from pathlib import Path ...@@ -20,7 +20,6 @@ from pathlib import Path
from subprocess import CalledProcessError from subprocess import CalledProcessError
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Any from typing import Any
from typing import cast
import packaging.tags import packaging.tags
import tomlkit import tomlkit
...@@ -521,7 +520,8 @@ class EnvManager: ...@@ -521,7 +520,8 @@ class EnvManager:
self._poetry = poetry self._poetry = poetry
self._io = io or NullIO() self._io = io or NullIO()
def _full_python_path(self, python: str) -> str: @staticmethod
def _full_python_path(python: str) -> str:
try: try:
executable = decode( executable = decode(
subprocess.check_output( subprocess.check_output(
...@@ -536,23 +536,23 @@ class EnvManager: ...@@ -536,23 +536,23 @@ class EnvManager:
return executable 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 executable = None
try: try:
self._io.write_error_line( io.write_error_line(
( (
"Trying to detect current active python executable as specified in" "Trying to detect current active python executable as specified in"
" the config." " the config."
), ),
verbosity=Verbosity.VERBOSE, verbosity=Verbosity.VERBOSE,
) )
executable = self._full_python_path("python") executable = EnvManager._full_python_path("python")
self._io.write_error_line( io.write_error_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
f"Found: {executable}", verbosity=Verbosity.VERBOSE
)
except EnvCommandError: except EnvCommandError:
self._io.write_error_line( io.write_error_line(
( (
"Unable to detect the current active python executable. Falling" "Unable to detect the current active python executable. Falling"
" back to default." " back to default."
...@@ -561,11 +561,16 @@ class EnvManager: ...@@ -561,11 +561,16 @@ class EnvManager:
) )
return executable return executable
def _get_python_version(self) -> tuple[int, int, int]: @staticmethod
version_info = tuple(sys.version_info[:3]) def get_python_version(
precious: int = 3,
prefer_active_python: bool = False,
io: None | IO = None,
) -> Version:
version = ".".join(str(v) for v in sys.version_info[:precious])
if self._poetry.config.get("virtualenvs.prefer-active-python"): if prefer_active_python:
executable = self._detect_active_python() executable = EnvManager._detect_active_python(io)
if executable: if executable:
python_patch = decode( python_patch = decode(
...@@ -577,9 +582,9 @@ class EnvManager: ...@@ -577,9 +582,9 @@ class EnvManager:
).strip() ).strip()
) )
version_info = tuple(int(v) for v in python_patch.split(".")[:3]) version = ".".join(str(v) for v in python_patch.split(".")[:precious])
return cast("tuple[int, int, int]", version_info) return Version.parse(version)
def activate(self, python: str) -> Env: def activate(self, python: str) -> Env:
venv_path = self._poetry.config.virtualenvs_path venv_path = self._poetry.config.virtualenvs_path
...@@ -692,7 +697,12 @@ class EnvManager: ...@@ -692,7 +697,12 @@ class EnvManager:
if self._env is not None and not reload: if self._env is not None and not reload:
return self._env 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 = self.get_python_version(
precious=2, prefer_active_python=prefer_active_python, io=self._io
).to_string()
venv_path = self._poetry.config.virtualenvs_path venv_path = self._poetry.config.virtualenvs_path
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment