Commit f5158013 by Sébastien Eustace

Ensure we don't pick up Poetry's virtualenv as the system env

parent 9591e884
......@@ -72,7 +72,7 @@ You can specify a package in the following forms:
plugins = self.argument("plugins")
# Plugins should be installed in the system env to be globally available
system_env = EnvManager.get_system_env()
system_env = EnvManager.get_system_env(naive=True)
env_dir = Path(
os.getenv("POETRY_HOME") if os.getenv("POETRY_HOME") else system_env.path
......
......@@ -43,7 +43,7 @@ class PluginRemoveCommand(Command):
plugins = self.argument("plugins")
system_env = EnvManager.get_system_env()
system_env = EnvManager.get_system_env(naive=True)
env_dir = Path(
os.getenv("POETRY_HOME") if os.getenv("POETRY_HOME") else system_env.path
)
......
......@@ -38,7 +38,7 @@ class PluginShowCommand(Command):
+ PluginManager("plugin").get_plugin_entry_points()
)
system_env = EnvManager.get_system_env()
system_env = EnvManager.get_system_env(naive=True)
installed_repository = InstalledRepository.load(
system_env, with_dependencies=True
)
......
import os
from pathlib import Path
from .utils.appdirs import user_cache_dir
......@@ -10,3 +12,10 @@ DATA_DIR = user_data_dir("pypoetry")
CONFIG_DIR = user_config_dir("pypoetry")
REPOSITORY_CACHE_DIR = Path(CACHE_DIR) / "cache" / "repositories"
def data_dir() -> Path:
if os.getenv("POETRY_HOME"):
return Path(os.getenv("POETRY_HOME")).expanduser()
return Path(user_data_dir("pypoetry", roaming=True))
......@@ -996,8 +996,31 @@ class EnvManager:
shutil.rmtree(str(file_path))
@classmethod
def get_system_env(cls) -> "SystemEnv":
return SystemEnv(Path(sys.prefix), cls.get_base_prefix())
def get_system_env(cls, naive: bool = False) -> "SystemEnv":
"""
Retrieve the current Python environment.
This can be the base Python environment or an activated virtual environment.
This method also workaround the issue that the virtual environment
used by Poetry internally (when installed via the custom installer)
is incorrectly detected as the system environment. Note that this workaround
happens only when `naive` is False since there are times where we actually
want to retrieve Poetry's custom virtual environment
(e.g. plugin installation or self update).
"""
prefix, base_prefix = Path(sys.prefix), cls.get_base_prefix()
if naive is False:
from poetry.locations import data_dir
try:
prefix.relative_to(data_dir())
except ValueError:
pass
else:
prefix = base_prefix
return SystemEnv(prefix, base_prefix)
@classmethod
def get_base_prefix(cls) -> 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