diff --git a/src/poetry/console/commands/plugin/add.py b/src/poetry/console/commands/plugin/add.py index c8a09772cd7..52792781084 100644 --- a/src/poetry/console/commands/plugin/add.py +++ b/src/poetry/console/commands/plugin/add.py @@ -1,5 +1,3 @@ -import os - from cleo.helpers import argument from cleo.helpers import option @@ -46,13 +44,12 @@ class PluginAddCommand(InitCommand, PluginCommandMixin): """ def handle(self) -> int: - from pathlib import Path - import tomlkit from poetry.core.semver.helpers import parse_constraint from poetry.factory import Factory + from poetry.locations import home_dir from poetry.utils.env import EnvManager from poetry.utils.helpers import canonicalize_name @@ -61,9 +58,7 @@ def handle(self) -> int: # Plugins should be installed in the system env to be globally available system_env = EnvManager.get_system_env(naive=True) - env_dir = Path( - os.getenv("POETRY_HOME") if os.getenv("POETRY_HOME") else system_env.path - ) + env_dir = home_dir() existing_plugins = {} if env_dir.joinpath("plugins.toml").exists(): diff --git a/src/poetry/console/commands/plugin/remove.py b/src/poetry/console/commands/plugin/remove.py index a58237225f2..2c17d77bc8d 100644 --- a/src/poetry/console/commands/plugin/remove.py +++ b/src/poetry/console/commands/plugin/remove.py @@ -1,10 +1,9 @@ -import os - from cleo.helpers import argument from cleo.helpers import option from poetry.console.commands.command import Command from poetry.console.commands.plugin.plugin_command_mixin import PluginCommandMixin +from poetry.locations import home_dir class PluginRemoveCommand(Command, PluginCommandMixin): @@ -26,8 +25,6 @@ class PluginRemoveCommand(Command, PluginCommandMixin): ] def handle(self) -> int: - from pathlib import Path - import tomlkit from poetry.utils.env import EnvManager @@ -36,9 +33,7 @@ def handle(self) -> int: plugins = self.argument("plugins") system_env = EnvManager.get_system_env(naive=True) - env_dir = Path( - os.getenv("POETRY_HOME") if os.getenv("POETRY_HOME") else system_env.path - ) + env_dir = home_dir() existing_plugins = {} if env_dir.joinpath("plugins.toml").exists(): diff --git a/src/poetry/locations.py b/src/poetry/locations.py index 035a9a66e94..dbe8a2652e6 100644 --- a/src/poetry/locations.py +++ b/src/poetry/locations.py @@ -20,3 +20,13 @@ def data_dir() -> Path: return Path(poetry_home).expanduser() return Path(user_data_dir("pypoetry", roaming=True)) + + +def home_dir() -> Path: + poetry_home = os.getenv("POETRY_HOME") + if poetry_home: + return Path(poetry_home).expanduser() + + from poetry.utils.env import EnvManager + + return EnvManager.get_system_env(True).path