shell.py (poetry-1.1.15) | : | shell.py (poetry-1.2.0) | ||
---|---|---|---|---|
from __future__ import annotations | ||||
import sys | import sys | |||
from distutils.util import strtobool | ||||
from os import environ | from os import environ | |||
from typing import TYPE_CHECKING | ||||
from typing import cast | ||||
from .env_command import EnvCommand | from poetry.console.commands.env_command import EnvCommand | |||
class ShellCommand(EnvCommand): | if TYPE_CHECKING: | |||
from poetry.utils.env import VirtualEnv | ||||
class ShellCommand(EnvCommand): | ||||
name = "shell" | name = "shell" | |||
description = "Spawns a shell within the virtual environment." | description = "Spawns a shell within the virtual environment." | |||
help = """The <info>shell</> command spawns a shell, according to the | help = """The <info>shell</> command spawns a shell, according to the | |||
<comment>$SHELL</> environment variable, within the virtual environment. | <comment>$SHELL</> environment variable, within the virtual environment. | |||
If one doesn't exist yet, it will be created. | If one doesn't exist yet, it will be created. | |||
""" | """ | |||
def handle(self): | def handle(self) -> int: | |||
from poetry.utils.shell import Shell | from poetry.utils.shell import Shell | |||
# Check if it's already activated or doesn't exist and won't be created | # Check if it's already activated or doesn't exist and won't be created | |||
venv_activated = strtobool(environ.get("POETRY_ACTIVE", "0")) or getattr | if self._is_venv_activated(): | |||
( | ||||
sys, "real_prefix", sys.prefix | ||||
) == str(self.env.path) | ||||
if venv_activated: | ||||
self.line( | self.line( | |||
"Virtual environment already activated: " | f"Virtual environment already activated: <info>{self.env.path}</ | |||
"<info>{}</>".format(self.env.path) | >" | |||
) | ) | |||
return | return 0 | |||
self.line("Spawning shell within <info>{}</>".format(self.env.path)) | self.line(f"Spawning shell within <info>{self.env.path}</>") | |||
# Be sure that we have the right type of environment. | ||||
env = self.env | ||||
assert env.is_venv() | ||||
env = cast("VirtualEnv", env) | ||||
# Setting this to avoid spawning unnecessary nested shells | # Setting this to avoid spawning unnecessary nested shells | |||
environ["POETRY_ACTIVE"] = "1" | environ["POETRY_ACTIVE"] = "1" | |||
shell = Shell.get() | shell = Shell.get() | |||
shell.activate(self.env) | shell.activate(env) | |||
environ.pop("POETRY_ACTIVE") | environ.pop("POETRY_ACTIVE") | |||
return 0 | ||||
def _is_venv_activated(self) -> bool: | ||||
return bool(environ.get("POETRY_ACTIVE")) or getattr( | ||||
sys, "real_prefix", sys.prefix | ||||
) == str(self.env.path) | ||||
End of changes. 13 change blocks. | ||||
14 lines changed or deleted | 20 lines changed or added |