Commit 04e4240c by Cauê Baasch de Souza Committed by Sébastien Eustace

Add shell command (#201)

* Add shell command

* Make shell command Python2-compatible

* Add provisional Windows support; handle unset SHELL variable

* Reformatted with black

* Handle already activated virtualenvs

* Add comments to shell command code

* Remove splat operator to support Python 2

* Use "1" for POETRY_ACTIVE instead of virtualenv path

* Handle non-existing virtualenv when virtualenvs.create is false

* Handle unexisting virtualenvs in a broader way

* Check if POETRY_ACTIVE is specifically set to a true value

* Use utils.shell to get path to running shell
parent 1a82099d
......@@ -28,6 +28,7 @@ from .commands import RemoveCommand
from .commands import RunCommand
from .commands import ScriptCommand
from .commands import SearchCommand
from .commands import ShellCommand
from .commands import ShowCommand
from .commands import UpdateCommand
from .commands import VersionCommand
......@@ -119,6 +120,7 @@ class Application(BaseApplication):
RunCommand(),
ScriptCommand(),
SearchCommand(),
ShellCommand(),
ShowCommand(),
UpdateCommand(),
VersionCommand(),
......
......@@ -13,6 +13,7 @@ from .remove import RemoveCommand
from .run import RunCommand
from .script import ScriptCommand
from .search import SearchCommand
from .shell import ShellCommand
from .show import ShowCommand
from .update import UpdateCommand
from .version import VersionCommand
from os import environ
from distutils.util import strtobool
from .venv_command import VenvCommand
class ShellCommand(VenvCommand):
"""
Spawns a shell within the virtual environment.
shell [options]
"""
help = """The <info>shell</> command spawns a shell, according to the
<comment>$SHELL</> environment variable, within the virtual environment.
If one doesn't exist yet, it will be created.
"""
def handle(self):
from poetry.utils.shell import Shell
# Check if it's already activated or doesn't exist and won't be created
if strtobool(environ.get("POETRY_ACTIVE", "0")) or not self.venv.is_venv():
current_venv = environ.get("VIRTUAL_ENV")
if current_venv:
self.line(
"Virtual environment already activated: "
"<info>{}</>".format(current_venv)
)
else:
self.error("Virtual environment wasn't found")
return
self.line("Spawning shell within <info>{}</>".format(self.venv.venv))
# Setting this to avoid spawning unnecessary nested shells
environ["POETRY_ACTIVE"] = "1"
shell = Shell.get()
self.venv.execute(shell.path)
environ.pop("POETRY_ACTIVE")
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