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

Shim python interpreter for windows if interpreter uses reparse points #5874

Merged
merged 6 commits into from
Dec 7, 2020

Conversation

viveklak
Copy link
Contributor

@viveklak viveklak commented Dec 7, 2020

Alternative implementation to #5775
Fixes #5388

Uses a shim script to workaround golang/go#42919

Copy link
Member

@justinvp justinvp left a comment

Choose a reason for hiding this comment

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

LGTM otherwise.

One question I have is around our IsVirtualEnv and VirtualEnvCommand functions.

  • IsVirtualEnv checks if there's a python.exe binary inside the specified virtual environment directory, via call to os.Stat looking for Scripts/python.exe
  • VirtualEnvCommand returns an exec.Cmd for the binary in Scripts/<bin>.exe. This function is used to run Scripts/python.exe inside the virtual environment.

Do these work on systems where Python is installed from the app store? (Basically, when we run pulumi-python-shim.cmd <resolved_execution_alias_python_binary> -m venv venv to create the virtual environment, in the resulting venv directory, what type of file is venv/Scripts/python.exe? Can we successfully os.Stat and exec.Cmd it, or is it one of these execution aliases that needs Lstat and shimming?)

A way to test it: With the changes in this PR, create a standard Python Pulumi project via pulumi new python and then run pulumi up. That should exercise these code paths.

Comment on lines 6 to 18
set "python_interpreter=%1"

SHIFT

set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop
:afterloop

%python_interpreter% %params%
Copy link
Member

Choose a reason for hiding this comment

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

Could all this be replaced with a simple two character one liner?

Suggested change
set "python_interpreter=%1"
SHIFT
set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop
:afterloop
%python_interpreter% %params%
%*

I think this will run the first arg as the command to run with the remaining as args to the command, but I am not a batch script expert and don't have my Windows WorkSpace booted up and handy, so haven't tried it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed your advice mostly but left the initial command hard coded.

@@ -0,0 +1,8 @@
//+build !windows
Copy link
Member

Choose a reason for hiding this comment

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

Copyright header

@@ -0,0 +1,19 @@
package python
Copy link
Member

Choose a reason for hiding this comment

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

Copyright header

@viveklak
Copy link
Contributor Author

viveklak commented Dec 7, 2020

LGTM otherwise.

One question I have is around our IsVirtualEnv and VirtualEnvCommand functions.

  • IsVirtualEnv checks if there's a python.exe binary inside the specified virtual environment directory, via call to os.Stat looking for Scripts/python.exe
  • VirtualEnvCommand returns an exec.Cmd for the binary in Scripts/<bin>.exe. This function is used to run Scripts/python.exe inside the virtual environment.

Do these work on systems where Python is installed from the app store? (Basically, when we run pulumi-python-shim.cmd <resolved_execution_alias_python_binary> -m venv venv to create the virtual environment, in the resulting venv directory, what type of file is venv/Scripts/python.exe? Can we successfully os.Stat and exec.Cmd it, or is it one of these execution aliases that needs Lstat and shimming?)

Sorry - I got lazy updating the PR body last night. I did test with the venv setup - i.e. create a new project with python and make sure the python within the venv isn't affected. I was able to do a pulumi up just fine. What I have seen is that the venv's python interpreter is a regular file with a non-zero length so its not affected by the bug.

@@ -36,6 +36,7 @@ RunGoBuild "github.com/pulumi/pulumi/sdk/v2/go/pulumi-language-go" "sdk" "pulumi
CopyPackage "$Root\sdk\nodejs\bin" "pulumi"

Copy-Item "$Root\sdk\nodejs\dist\pulumi-resource-pulumi-nodejs.cmd" "$PublishDir\bin"
Copy-Item "$Root\sdk\nodejs\dist\pulumi-python-shim.cmd" "$PublishDir\bin"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that we need the pulumi-python-shim.cmd packaged as part of the plugin and be installed in PATH. Based on what I saw, this should be sufficient but I am not familiar enough with the mechanics of the build/publish for windows to know for sure this works. My crappy windows VM can't handle the build for pulumi so I am leaving this to CI to build and will validate the published zip to check if the script makes it in. FYI @justinvp

@viveklak
Copy link
Contributor Author

viveklak commented Dec 7, 2020

Merging so we can test with dev build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Windows 10: Failed to locate any of ["python3" "python"] on your PATH
2 participants