From 88e64b07fa37f5ef32ab2555558bab1abf5e1e55 Mon Sep 17 00:00:00 2001 From: Bartosz Sokorski Date: Sun, 27 Oct 2024 17:12:06 +0100 Subject: [PATCH] Address CR issues --- docs/cli.md | 4 ++++ docs/managing-environments.md | 9 ++++++--- src/poetry/console/commands/env/activate.py | 21 +++++++++++++-------- tests/console/commands/env/test_activate.py | 2 +- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 750c914eb80..37f1695de6b 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -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 diff --git a/docs/managing-environments.md b/docs/managing-environments.md index 7be98626825..b3eaf655be3 100644 --- a/docs/managing-environments.md +++ b/docs/managing-environments.md @@ -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" >}} @@ -99,6 +100,7 @@ $ eval $(poetry env activate) ```bash $ eval (poetry env activate) +(test-project-for-test) $ # Virtualenv entered ``` {{< /tab >}} @@ -106,6 +108,7 @@ $ eval (poetry env activate) ```ps1 PS1> Invoke-Expression (poetry env activate) +(test-project-for-test) PS1> # Virtualenv entered ``` {{< /tab >}} diff --git a/src/poetry/console/commands/env/activate.py b/src/poetry/console/commands/env/activate.py index bdc25297159..0630a7528ca 100644 --- a/src/poetry/console/commands/env/activate.py +++ b/src/poetry/console/commands/env/activate.py @@ -20,14 +20,14 @@ 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: @@ -35,7 +35,7 @@ def handle(self) -> int: "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: @@ -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) diff --git a/tests/console/commands/env/test_activate.py b/tests/console/commands/env/test_activate.py index 15c29d18713..2fb3664ae2b 100644 --- a/tests/console/commands/env/test_activate.py +++ b/tests/console/commands/env/test_activate.py @@ -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"), ), )