From d64ee1a5ba2007ae5fe085dd3495013d940a51bb Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 21 Sep 2018 15:27:26 -0700 Subject: bpo-32718: Make Activate.ps1 for venv cross-platform and available on all platforms (GH-9321) PowerShell Core 6.1 is the cross-platform port of Windows PowerShell. This change updates Activate.ps1 to not make Windows assumptions as well as installing it into the bin/Scripts directory on all operating systems. Requires PowerShell Core 6.1 for proper readline support once the shell has been activated for the virtual environment. --- Doc/whatsnew/3.8.rst | 9 +++- Lib/venv/scripts/common/Activate.ps1 | 56 ++++++++++++++++++++++ Lib/venv/scripts/nt/Activate.ps1 | 51 -------------------- .../2018-09-14-12-38-49.bpo-32718.ICYQbt.rst | 2 + 4 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 Lib/venv/scripts/common/Activate.ps1 delete mode 100644 Lib/venv/scripts/nt/Activate.ps1 create mode 100644 Misc/NEWS.d/next/Library/2018-09-14-12-38-49.bpo-32718.ICYQbt.rst diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 1c129a7..26928fb 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -136,6 +136,13 @@ pathlib contain characters unrepresentable at the OS level. (Contributed by Serhiy Storchaka in :issue:`33721`.) +venv +---- + +* :mod:`venv` now includes an ``Activate.ps1`` script on all platforms for + activating virtual environments under PowerShell Core 6.1. + (Contributed by Brett Cannon in :issue:`32718`.) + Optimizations ============= @@ -321,7 +328,7 @@ CPython bytecode changes * The interpreter loop has been simplified by moving the logic of unrolling the stack of blocks into the compiler. The compiler emits now explicit - instructions for adjusting the stack of values and calling the cleaning + instructions for adjusting the stack of values and calling the cleaning- up code for :keyword:`break`, :keyword:`continue` and :keyword:`return`. Removed opcodes :opcode:`BREAK_LOOP`, :opcode:`CONTINUE_LOOP`, diff --git a/Lib/venv/scripts/common/Activate.ps1 b/Lib/venv/scripts/common/Activate.ps1 new file mode 100644 index 0000000..de22962 --- /dev/null +++ b/Lib/venv/scripts/common/Activate.ps1 @@ -0,0 +1,56 @@ +function Script:add-bin([string]$envPath) { + $binPath = Join-Path -Path $env:VIRTUAL_ENV -ChildPath '__VENV_BIN_NAME__' + return ($binPath, $envPath) -join [IO.Path]::PathSeparator +} + +function global:deactivate ([switch]$NonDestructive) { + # Revert to original values + if (Test-Path function:_OLD_VIRTUAL_PROMPT) { + copy-item function:_OLD_VIRTUAL_PROMPT function:prompt + remove-item function:_OLD_VIRTUAL_PROMPT + } + + if (Test-Path env:_OLD_VIRTUAL_PYTHONHOME) { + copy-item env:_OLD_VIRTUAL_PYTHONHOME env:PYTHONHOME + remove-item env:_OLD_VIRTUAL_PYTHONHOME + } + + if (Test-Path env:_OLD_VIRTUAL_PATH) { + copy-item env:_OLD_VIRTUAL_PATH env:PATH + remove-item env:_OLD_VIRTUAL_PATH + } + + if (Test-Path env:VIRTUAL_ENV) { + remove-item env:VIRTUAL_ENV + } + + if (!$NonDestructive) { + # Self destruct! + remove-item function:deactivate + } +} + +deactivate -nondestructive + +$env:VIRTUAL_ENV="__VENV_DIR__" + +if (! $env:VIRTUAL_ENV_DISABLE_PROMPT) { + # Set the prompt to include the env name + # Make sure _OLD_VIRTUAL_PROMPT is global + function global:_OLD_VIRTUAL_PROMPT {""} + copy-item function:prompt function:_OLD_VIRTUAL_PROMPT + function global:prompt { + Write-Host -NoNewline -ForegroundColor Green '__VENV_PROMPT__' + _OLD_VIRTUAL_PROMPT + } +} + +# Clear PYTHONHOME +if (Test-Path env:PYTHONHOME) { + copy-item env:PYTHONHOME env:_OLD_VIRTUAL_PYTHONHOME + remove-item env:PYTHONHOME +} + +# Add the venv to the PATH +copy-item env:PATH env:_OLD_VIRTUAL_PATH +$env:PATH = add-bin $env:PATH diff --git a/Lib/venv/scripts/nt/Activate.ps1 b/Lib/venv/scripts/nt/Activate.ps1 deleted file mode 100644 index bf60869..0000000 --- a/Lib/venv/scripts/nt/Activate.ps1 +++ /dev/null @@ -1,51 +0,0 @@ -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - if (Test-Path function:_OLD_VIRTUAL_PROMPT) { - copy-item function:_OLD_VIRTUAL_PROMPT function:prompt - remove-item function:_OLD_VIRTUAL_PROMPT - } - - if (Test-Path env:_OLD_VIRTUAL_PYTHONHOME) { - copy-item env:_OLD_VIRTUAL_PYTHONHOME env:PYTHONHOME - remove-item env:_OLD_VIRTUAL_PYTHONHOME - } - - if (Test-Path env:_OLD_VIRTUAL_PATH) { - copy-item env:_OLD_VIRTUAL_PATH env:PATH - remove-item env:_OLD_VIRTUAL_PATH - } - - if (Test-Path env:VIRTUAL_ENV) { - remove-item env:VIRTUAL_ENV - } - - if (!$NonDestructive) { - # Self destruct! - remove-item function:deactivate - } -} - -deactivate -nondestructive - -$env:VIRTUAL_ENV="__VENV_DIR__" - -if (! $env:VIRTUAL_ENV_DISABLE_PROMPT) { - # Set the prompt to include the env name - # Make sure _OLD_VIRTUAL_PROMPT is global - function global:_OLD_VIRTUAL_PROMPT {""} - copy-item function:prompt function:_OLD_VIRTUAL_PROMPT - function global:prompt { - Write-Host -NoNewline -ForegroundColor Green '__VENV_PROMPT__' - _OLD_VIRTUAL_PROMPT - } -} - -# Clear PYTHONHOME -if (Test-Path env:PYTHONHOME) { - copy-item env:PYTHONHOME env:_OLD_VIRTUAL_PYTHONHOME - remove-item env:PYTHONHOME -} - -# Add the venv to the PATH -copy-item env:PATH env:_OLD_VIRTUAL_PATH -$env:PATH = "$env:VIRTUAL_ENV\__VENV_BIN_NAME__;$env:PATH" diff --git a/Misc/NEWS.d/next/Library/2018-09-14-12-38-49.bpo-32718.ICYQbt.rst b/Misc/NEWS.d/next/Library/2018-09-14-12-38-49.bpo-32718.ICYQbt.rst new file mode 100644 index 0000000..b60106a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-14-12-38-49.bpo-32718.ICYQbt.rst @@ -0,0 +1,2 @@ +The Activate.ps1 script from venv works with PowerShell Core 6.1 and is now +available under all operating systems. -- cgit v0.12