Skip to content

Commit

Permalink
Address CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Oct 27, 2024
1 parent 27d31bd commit 88e64b0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,10 @@ poetry run my-script

Note that this command has no option.

## shell

The `shell` command was moved to a plugin: [`poetry-plugin-shell`](https://github.com/python-poetry/poetry-plugin-shell)

## check

The `check` command validates the content of the `pyproject.toml` file
Expand Down
9 changes: 6 additions & 3 deletions docs/managing-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ poetry env use system

## Activating the environment

`poetry env activate` command prints the activate command to the console. This way you won't leave the current shell.
You can then feed the output to `eval` to activate the environment. This way is the closest to manually activating the environment.

{{% note %}}
Looking for `poetry shell`? It was moved to a plugin: [`poetry-plugin-shell`](https://github.com/python-poetry/poetry-plugin-shell)
{{% /note %}}

The `poetry env activate` command prints the activate command of the virtual environment to the console.
You can run the output command manually or feed it to the eval command of your shell to activate the environment.
This way you won't leave the current shell.

{{< tabs tabTotal="3" tabID1="bash-csh-zsh" tabID2="fish" tabID3="powershell" tabName1="Bash/Zsh/Csh" tabName2="Fish" tabName3="Powershell" >}}

{{< tab tabID="bash-csh-zsh" >}}
Expand All @@ -99,13 +100,15 @@ $ eval $(poetry env activate)

```bash
$ eval (poetry env activate)
(test-project-for-test) $ # Virtualenv entered
```

{{< /tab >}}
{{< tab tabID="powershell" >}}

```ps1
PS1> Invoke-Expression (poetry env activate)
(test-project-for-test) PS1> # Virtualenv entered
```

{{< /tab >}}
Expand Down
21 changes: 13 additions & 8 deletions src/poetry/console/commands/env/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class ShellNotSupportedError(Exception):

class EnvActivateCommand(Command):
name = "env activate"
description = "Print the command to activate a virtual environment"
description = "Print the command to activate a virtual environment."

def handle(self) -> int:
from poetry.utils.env import EnvManager

env = EnvManager(self.poetry).get()

if command := self.get_activate_command(env):
if command := self._get_activate_command(env):
self.line(command)
return 0
else:
raise ShellNotSupportedError(
"Discovered shell doesn't have an activator in virtual environment"
)

def get_activate_command(self, env: Env) -> str:
def _get_activate_command(self, env: Env) -> str:
try:
shell, _ = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
Expand All @@ -48,17 +48,22 @@ def get_activate_command(self, env: Env) -> str:
command, filename = "source", "activate.csh"
elif shell in ["powershell", "pwsh"]:
command, filename = ".", "Activate.ps1"
elif shell == "cmd":
command, filename = ".", "activate.bat"
else:
command, filename = "source", "activate"

if (activation_script := env.bin_dir / filename).exists():
if WINDOWS:
return f"{self.quote(str(activation_script), shell)}"
return f"{command} {self.quote(str(activation_script), shell)}"
return f"{self._quote(str(activation_script), shell)}"
return f"{command} {self._quote(str(activation_script), shell)}"
return ""

@staticmethod
def quote(command: str, shell: str) -> str:
if shell in ["powershell", "pwsh"] or WINDOWS:
return "{}".format(command.replace("'", "''"))
def _quote(command: str, shell: str) -> str:
if WINDOWS:
if shell == "cmd":
return f'"{command}"'
if shell in ["powershell", "pwsh"]:
return f'& "{command}"'
return shlex.quote(command)
2 changes: 1 addition & 1 deletion tests/console/commands/env/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_env_activate_prints_correct_script_windows(
@pytest.mark.parametrize(
"shell, command, ext",
(
("pwsh", ".", "Activate.ps1"),
("cmd", ".", "activate.bat")("pwsh", ".", "Activate.ps1"),
("powershell", ".", "Activate.ps1"),
),
)
Expand Down

0 comments on commit 88e64b0

Please sign in to comment.