Commit 619488cc by finswimmer Committed by Bjorn Neergaard

rename option `virtualenvs.prefer-active-python` and move whole logic to EnvManager.create_venv()

parent 69d790b1
...@@ -48,7 +48,7 @@ virtualenvs.in-project = null ...@@ -48,7 +48,7 @@ virtualenvs.in-project = null
virtualenvs.options.always-copy = true virtualenvs.options.always-copy = true
virtualenvs.options.system-site-packages = false virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs
virtualenvs.prefer-shell-python = false virtualenvs.prefer-active-python = false
``` ```
## Displaying a single configuration setting ## Displaying a single configuration setting
...@@ -189,7 +189,7 @@ Give the virtual environment access to the system site-packages directory. ...@@ -189,7 +189,7 @@ Give the virtual environment access to the system site-packages directory.
Applies on virtualenv creation. Applies on virtualenv creation.
Defaults to `false`. Defaults to `false`.
### `virtualenvs.prefer-shell-python` ### `virtualenvs.prefer-active-python` (experimental)
**Type**: boolean **Type**: boolean
......
...@@ -27,8 +27,9 @@ to find one that is and use it. If it's unable to do so then you will be prompte ...@@ -27,8 +27,9 @@ to find one that is and use it. If it's unable to do so then you will be prompte
to activate one explicitly, see [Switching environments](#switching-between-environments). to activate one explicitly, see [Switching environments](#switching-between-environments).
{{% note %}} {{% note %}}
To easily switch between Python versions, it is recommended to If you use a tool like [pyenv](https://github.com/pyenv/pyenv) to manage different Python versions,
use [pyenv](https://github.com/pyenv/pyenv) or similar tools. you can set the experimental `virtualenvs.prefer-active-python` option to `true`. Poetry
than will try to find the current `python` of your shell.
For instance, if your project requires a newer Python than is available with For instance, if your project requires a newer Python than is available with
your system, a standard workflow would be: your system, a standard workflow would be:
...@@ -38,7 +39,6 @@ pyenv install 3.9.8 ...@@ -38,7 +39,6 @@ pyenv install 3.9.8
pyenv local 3.9.8 # Activate Python 3.9 for the current project pyenv local 3.9.8 # Activate Python 3.9 for the current project
poetry install poetry install
``` ```
This requires setting the `virtualenvs.prefer-shell-python` option to `true`.
{{% /note %}} {{% /note %}}
## Switching between environments ## Switching between environments
......
...@@ -38,7 +38,7 @@ class Config: ...@@ -38,7 +38,7 @@ class Config:
"in-project": None, "in-project": None,
"path": os.path.join("{cache-dir}", "virtualenvs"), "path": os.path.join("{cache-dir}", "virtualenvs"),
"options": {"always-copy": False, "system-site-packages": False}, "options": {"always-copy": False, "system-site-packages": False},
"prefer-shell-python": False, "prefer-active-python": False,
}, },
"experimental": {"new-installer": True}, "experimental": {"new-installer": True},
"installer": {"parallel": True, "max-workers": None}, "installer": {"parallel": True, "max-workers": None},
...@@ -139,7 +139,7 @@ class Config: ...@@ -139,7 +139,7 @@ class Config:
"virtualenvs.in-project", "virtualenvs.in-project",
"virtualenvs.options.always-copy", "virtualenvs.options.always-copy",
"virtualenvs.options.system-site-packages", "virtualenvs.options.system-site-packages",
"virtualenvs.options.prefer-shell-python", "virtualenvs.options.prefer-active-python",
"experimental.new-installer", "experimental.new-installer",
"installer.parallel", "installer.parallel",
}: }:
......
import logging import logging
import re import re
import subprocess
from contextlib import suppress from contextlib import suppress
from importlib import import_module from importlib import import_module
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 Callable from typing import Callable
...@@ -18,14 +16,11 @@ from cleo.events.event_dispatcher import EventDispatcher ...@@ -18,14 +16,11 @@ from cleo.events.event_dispatcher import EventDispatcher
from cleo.exceptions import CleoException from cleo.exceptions import CleoException
from cleo.formatters.style import Style from cleo.formatters.style import Style
from cleo.io.inputs.argv_input import ArgvInput from cleo.io.inputs.argv_input import ArgvInput
from cleo.io.outputs.output import Verbosity
from poetry.core.utils._compat import PY37 from poetry.core.utils._compat import PY37
from poetry.__version__ import __version__ from poetry.__version__ import __version__
from poetry.console.command_loader import CommandLoader from poetry.console.command_loader import CommandLoader
from poetry.console.commands.command import Command from poetry.console.commands.command import Command
from poetry.utils._compat import decode
from poetry.utils._compat import list_to_shell_command
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -229,30 +224,6 @@ class Application(BaseApplication): ...@@ -229,30 +224,6 @@ class Application(BaseApplication):
return super()._configure_io(io) return super()._configure_io(io)
def _detect_active_python(self, io: "IO") -> str:
executable = None
try:
io.write_line(
"Trying to detect current active python executable as specified in the config.",
verbosity=Verbosity.VERBOSE,
)
executable = decode(
subprocess.check_output(
list_to_shell_command(
["python", "-c", '"import sys; print(sys.executable)"']
),
shell=True,
).strip()
)
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
except CalledProcessError:
io.write_line(
"Unable to detect the current active python executable. Falling back to default.",
verbosity=Verbosity.VERBOSE,
)
return executable
def register_command_loggers( def register_command_loggers(
self, event: "ConsoleCommandEvent", event_name: str, _: Any self, event: "ConsoleCommandEvent", event_name: str, _: Any
) -> None: ) -> None:
...@@ -311,18 +282,8 @@ class Application(BaseApplication): ...@@ -311,18 +282,8 @@ class Application(BaseApplication):
io = event.io io = event.io
poetry = command.poetry poetry = command.poetry
executable = (
self._detect_active_python(io)
if poetry.config.get("virtualenvs.prefer-shell-python")
else None
)
env_manager = EnvManager(poetry) env_manager = EnvManager(poetry)
env = env_manager.create_venv( env = env_manager.create_venv(io)
io,
executable=executable,
find_compatible=True if executable else None,
)
if env.is_venv() and io.is_verbose(): if env.is_venv() and io.is_verbose():
io.write_line(f"Using virtualenv: <comment>{env.path}</>") io.write_line(f"Using virtualenv: <comment>{env.path}</>")
......
...@@ -78,7 +78,7 @@ To remove a repository (repo is a short alias for repositories): ...@@ -78,7 +78,7 @@ To remove a repository (repo is a short alias for repositories):
lambda val: str(Path(val)), lambda val: str(Path(val)),
str(Path(CACHE_DIR) / "virtualenvs"), str(Path(CACHE_DIR) / "virtualenvs"),
), ),
"virtualenvs.prefer-shell-python": ( "virtualenvs.prefer-active-python": (
boolean_validator, boolean_validator,
boolean_normalizer, boolean_normalizer,
False, False,
......
...@@ -30,6 +30,7 @@ import packaging.tags ...@@ -30,6 +30,7 @@ import packaging.tags
import tomlkit import tomlkit
import virtualenv import virtualenv
from cleo.io.outputs.output import Verbosity
from packaging.tags import Tag from packaging.tags import Tag
from packaging.tags import interpreter_name from packaging.tags import interpreter_name
from packaging.tags import interpreter_version from packaging.tags import interpreter_version
...@@ -464,6 +465,30 @@ class EnvManager: ...@@ -464,6 +465,30 @@ class EnvManager:
def __init__(self, poetry: "Poetry") -> None: def __init__(self, poetry: "Poetry") -> None:
self._poetry = poetry self._poetry = poetry
def _detect_active_python(self, io: "IO") -> str:
executable = None
try:
io.write_line(
"Trying to detect current active python executable as specified in the config.",
verbosity=Verbosity.VERBOSE,
)
executable = decode(
subprocess.check_output(
list_to_shell_command(
["python", "-c", '"import sys; print(sys.executable)"']
),
shell=True,
).strip()
)
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
except CalledProcessError:
io.write_line(
"Unable to detect the current active python executable. Falling back to default.",
verbosity=Verbosity.VERBOSE,
)
return executable
def activate(self, python: str, io: "IO") -> "Env": def activate(self, python: str, io: "IO") -> "Env":
venv_path = self._poetry.config.get("virtualenvs.path") venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None: if venv_path is None:
...@@ -763,7 +788,6 @@ class EnvManager: ...@@ -763,7 +788,6 @@ class EnvManager:
name: Optional[str] = None, name: Optional[str] = None,
executable: Optional[str] = None, executable: Optional[str] = None,
force: bool = False, force: bool = False,
find_compatible: Optional[bool] = None,
) -> Union["SystemEnv", "VirtualEnv"]: ) -> Union["SystemEnv", "VirtualEnv"]:
if self._env is not None and not force: if self._env is not None and not force:
return self._env return self._env
...@@ -781,6 +805,12 @@ class EnvManager: ...@@ -781,6 +805,12 @@ class EnvManager:
create_venv = self._poetry.config.get("virtualenvs.create") create_venv = self._poetry.config.get("virtualenvs.create")
root_venv = self._poetry.config.get("virtualenvs.in-project") root_venv = self._poetry.config.get("virtualenvs.in-project")
venv_path = self._poetry.config.get("virtualenvs.path") venv_path = self._poetry.config.get("virtualenvs.path")
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
if not executable and prefer_active_python:
executable = self._detect_active_python(io)
if root_venv: if root_venv:
venv_path = cwd / ".venv" venv_path = cwd / ".venv"
...@@ -813,7 +843,7 @@ class EnvManager: ...@@ -813,7 +843,7 @@ class EnvManager:
# If an executable has been specified, we stop there # If an executable has been specified, we stop there
# and notify the user of the incompatibility. # and notify the user of the incompatibility.
# Otherwise, we try to find a compatible Python version. # Otherwise, we try to find a compatible Python version.
if executable and not find_compatible: if executable and not prefer_active_python:
raise NoCompatiblePythonVersionFound( raise NoCompatiblePythonVersionFound(
self._poetry.package.python_versions, python_patch self._poetry.package.python_versions, python_patch
) )
......
...@@ -56,7 +56,7 @@ virtualenvs.in-project = null ...@@ -56,7 +56,7 @@ virtualenvs.in-project = null
virtualenvs.options.always-copy = false virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-shell-python = false virtualenvs.prefer-active-python = false
""" """
assert expected == tester.io.fetch_output() assert expected == tester.io.fetch_output()
...@@ -80,7 +80,7 @@ virtualenvs.in-project = null ...@@ -80,7 +80,7 @@ virtualenvs.in-project = null
virtualenvs.options.always-copy = false virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-shell-python = false virtualenvs.prefer-active-python = false
""" """
assert config.set_config_source.call_count == 0 assert config.set_config_source.call_count == 0
...@@ -128,7 +128,7 @@ virtualenvs.in-project = null ...@@ -128,7 +128,7 @@ virtualenvs.in-project = null
virtualenvs.options.always-copy = false virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false virtualenvs.options.system-site-packages = false
virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'}
virtualenvs.prefer-shell-python = false virtualenvs.prefer-active-python = false
""" """
assert config.set_config_source.call_count == 1 assert config.set_config_source.call_count == 1
......
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