Skip to content

Commit

Permalink
pass path to executable for getting a specific venv
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Jun 14, 2021
1 parent 9a6e18d commit efeb695
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from contextlib import contextmanager
from copy import deepcopy
from functools import lru_cache
from pathlib import Path
from subprocess import CalledProcessError
from typing import Any
Expand Down Expand Up @@ -427,6 +428,26 @@ class EnvManager:
def __init__(self, poetry: Poetry) -> None:
self._poetry = poetry

@lru_cache()
def _python_version(self, executable: str) -> str:
try:
python_version = decode(
subprocess.check_output(
list_to_shell_command(
[
executable,
"-c",
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
]
),
shell=True,
)
)
except CalledProcessError as e:
raise EnvCommandError(e)

return python_version

def activate(self, python: str, io: IO) -> "Env":
venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
Expand Down Expand Up @@ -548,11 +569,18 @@ def deactivate(self, io: IO) -> None:

envs_file.write(envs)

def get(self, reload: bool = False) -> Union["VirtualEnv", "SystemEnv"]:
def get(
self, reload: bool = False, executable: Optional[str] = None
) -> Union["VirtualEnv", "SystemEnv"]:
if self._env is not None and not reload:
return self._env

python_minor = ".".join([str(v) for v in sys.version_info[:2]])
if executable:
python_version = self._python_version(executable)
python_version = Version.parse(python_version.strip())
python_minor = f"{python_version.major}.{python_version.minor}"
else:
python_minor = ".".join([str(v) for v in sys.version_info[:2]])

venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
Expand Down Expand Up @@ -743,7 +771,7 @@ def create_venv(
return self._env

cwd = self._poetry.file.parent
env = self.get(reload=True)
env = self.get(reload=True, executable=executable)

if not env.is_sane():
force = True
Expand All @@ -769,18 +797,7 @@ def create_venv(
python_patch = ".".join([str(v) for v in sys.version_info[:3]])
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
if executable:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[
executable,
"-c",
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
]
),
shell=True,
).strip()
)
python_patch = self._python_version(executable)
python_minor = ".".join(python_patch.split(".")[:2])

supported_python = self._poetry.package.python_constraint
Expand Down

0 comments on commit efeb695

Please sign in to comment.