Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-37354: Make Powershell Activate.ps1 script static to allow for signing #14967

Merged
merged 6 commits into from
Aug 12, 2019

Conversation

d3r3kk
Copy link
Contributor

@d3r3kk d3r3kk commented Jul 26, 2019

Make Powershell Activation.ps1 script static and therefore sign-able. The script now determines the interpreter path based on where the Activate script is. The prompt is determined based on the directory name of the parent directory the script is located in, or can be overridden via an argument to the Activate script, or from within the pyvenv.cfg file.

Tested On:

OS Powershell Version Powershell Build Version
Windows 8.1 5.1.14409.1018 10.0.14409.1018
Windows 7 SP1 5.1.14409.1018 10.0.14409.1018
Windows Server 2012R2 4.0 6.3.9600.19170
Windows 10 5.1.18362.145 10.0.18362.145
Debian 10.0 (Buster) 6.2.2 (Core) ?

https://bugs.python.org/issue37354

d3r3kk and others added 5 commits July 25, 2019 09:56
- Remove use of replacement text in the script

- Make use of the pyvenv.cfg file for prompt value.

- Add parameters to allow more flexibility

- Make use of the current path, and assumptions about where �env puts things, to compensate

- Make the script a bit more 'idiomatic' Powershell

- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
- with prompt
- with usage in Linux on PowerShell core
@d3r3kk
Copy link
Contributor Author

d3r3kk commented Jul 26, 2019

Note, also fixes bpo-35667.

Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
$Prompt = $pyvenvCfg['prompt'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line that gave me the most nightmares when thinking about how to do this, because the prompt value in the config file is quoted and escaped (basically, it's repr(prompt)), so we need to do more processing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @zooba!

Agreed that the escaped character problem is a bit tricky in the context of Powershell... \n is not really available in Powershell, for instance, but could be replaced with the back-tick-quote `. Finding and replacing all types of escaped characters (newlines, tabs, colors?) might be a bit of a deep dark hole (there be ANSI-escaped coloured dragons there)!

Would it be appropriate to add handling of escaped characters in a different issue/PR? I don't expect that the prior version of this Activate.ps1 script handled this problem particularly well either (a brief test with the latest 3.8 beta shows a few hiccups right away).
Of course, I would be most happy to tackle that problem next!

What sample input would show the shortcoming of this implementation, so that I can write some unit tests and fix it correctly?

Just from thinking about it briefly I can think up input that contains:

  • newlines (\n, \r\n)
  • tabs (\t)
  • colours? (\u001b[31m, ... , \u001b[0m)

Ideally I'd love to show something that would have the class of input we need to be concerned about working with, and have it handled at least as well as the prior implementation handled it.

A specific scenario that is trivial to setup and play with, for example the newline escaped character \n in the prompt:

PS /home/d3r3kk/dev/venvtest> ../cpython/python -m venv --prompt "\nd3r3kk\n" .newline_prompt
PS /home/d3r3kk/dev/venvtest> cat ./.newline_prompt/pyvenv.cfg
home = /home/d3r3kk/dev/cpython
include-system-site-packages = false
version = 3.9.0
prompt = '\\nd3r3kk\\n'
PS /home/d3r3kk/dev/venvtest> ./.newline_prompt/bin/Activate.ps1
(\\nd3r3kk\\n) PS /home/d3r3kk/dev/venvtest> deactivate
PS /home/d3r3kk/dev/venvtest> exit

...and then in bash...

d3r3kk@deb:~/dev/venvtest$ . .newline_prompt/bin/activate
(
d3r3kk
) d3r3kk@deb:~/dev/venvtest$ deactivate
d3r3kk@deb:~/dev/venvtest$ 

...as we can see, the \\n being stored in the pyvenv.cfg is written to the prompt verbatim in Powershell with this change (and is also in the latest 3.8 beta release).

Note: it is possible in Powershell to replace some escaped character's \\ with a ` and it will work. (But definitely not colours, those are weird special creatures!).

@zooba zooba requested a review from vsajip July 29, 2019 23:50
Copy link
Member

@vsajip vsajip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a PowerShell tyro, so I can't comment on some of the finer points. The handling of escapes seems orthogonal to the making of the script to be entirely static, but I concur with the comment on the issue made by @zooba : "get it out in 3.8.0b4 and see how it fares".

Lib/venv/scripts/common/Activate.ps1 Outdated Show resolved Hide resolved
Lib/venv/scripts/common/Activate.ps1 Outdated Show resolved Hide resolved
Lib/venv/scripts/common/Activate.ps1 Outdated Show resolved Hide resolved
Lib/venv/scripts/common/Activate.ps1 Show resolved Hide resolved
@d3r3kk
Copy link
Contributor Author

d3r3kk commented Aug 7, 2019

Thanks for the review @vsajip! (and for introducing me to the term tyro). I've updated the PR with your suggestions.

Copy link
Member

@vsajip vsajip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing more to add to my earlier review.

@d3r3kk
Copy link
Contributor Author

d3r3kk commented Aug 12, 2019

@zooba anything else for me to address?

@zooba zooba merged commit 732775d into python:master Aug 12, 2019
@miss-islington
Copy link
Contributor

Thanks @d3r3kk for the PR, and @zooba for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 12, 2019
…gning (pythonGH-14967)

- Remove use of replacement text in the script
- Make use of the pyvenv.cfg file for prompt value.
- Add parameters to allow more flexibility
- Make use of the current path, and assumptions about where env puts things, to compensate
- Make the script a bit more 'idiomatic' Powershell
- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
(cherry picked from commit 732775d)

Co-authored-by: Derek Keeler <d3r3kk@users.noreply.github.com>
@bedevere-bot
Copy link

GH-15233 is a backport of this pull request to the 3.8 branch.

@d3r3kk d3r3kk deleted the pwsh_activate branch August 12, 2019 20:14
zooba pushed a commit that referenced this pull request Aug 12, 2019
…for signing (GH-14967)

- Remove use of replacement text in the script
- Make use of the pyvenv.cfg file for prompt value.
- Add parameters to allow more flexibility
- Make use of the current path, and assumptions about where env puts things, to compensate
- Make the script a bit more 'idiomatic' Powershell
- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
(cherry picked from commit 732775d)

Co-authored-by: Derek Keeler <d3r3kk@users.noreply.github.com>
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…gning (pythonGH-14967)

- Remove use of replacement text in the script
- Make use of the pyvenv.cfg file for prompt value.
- Add parameters to allow more flexibility
- Make use of the current path, and assumptions about where env puts things, to compensate
- Make the script a bit more 'idiomatic' Powershell
- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…gning (pythonGH-14967)

- Remove use of replacement text in the script
- Make use of the pyvenv.cfg file for prompt value.
- Add parameters to allow more flexibility
- Make use of the current path, and assumptions about where env puts things, to compensate
- Make the script a bit more 'idiomatic' Powershell
- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
…gning (pythonGH-14967)

- Remove use of replacement text in the script
- Make use of the pyvenv.cfg file for prompt value.
- Add parameters to allow more flexibility
- Make use of the current path, and assumptions about where env puts things, to compensate
- Make the script a bit more 'idiomatic' Powershell
- Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants