Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rewrite of activate.ps1 #261

Closed
wants to merge 12 commits into from

4 participants

@hwiechers

This rewrites activate.ps1.

The script is now significantly cleaner.
It also handles issues where a user installs a 32-bit Python or installs it for 'current user only'.

@KWierso
Owner

@mhammond Would you be able to review this? If not, you could pass it on to @mykmelez

@mhammond
Owner

The script doesn't seem to work for me - I get:

| Error: A recent version of Python 2.x must be installed.

I'm not at all familiar with powershell so my attempts to debug have failed, but I'm happy to help diagnose this if you give me some pointers. The closest I got to diagnosing this was to add a block:

@('HKCU:SOFTWARE\Python\PythonCore\*\InstallPath',
'HKLM:SOFTWARE\Python\PythonCore\*\InstallPath',
'HKLM:SOFTWARE\Wow6432Node\Python\PythonCore\*\InstallPath',
'HKCU:SOFTWARE\Wow6432Node\Python\PythonCore\*\InstallPath') | 
    Where-Object { Test-Path $_ } | echo

and the output from that was:

HKLM:SOFTWARE\Python\PythonCore*\InstallPath
HKLM:SOFTWARE\Wow6432Node\Python\PythonCore*\InstallPath

which seems expected (I do have both 32bit and 64bit versions of Python installed)

@hwiechers

I broke the script in d4340ed where I renamed $PyRegKey to $PyInstallPathKey
everywhere except the minimum version check.

I think I missed this because I was using global variables in the script and
the $PyRegKey variable was still around from a previous run.

71a941b fixes the bug and 16bd172 scopes all variables to script.

Please try the new version.

@mhammond
Owner

It looks closer, but still doesn't quite work for me - although that is because I have a non-standard setup. My Python directories have been built from source, thus the executable is in a "PCBuild" directory (for 32bit versions) or "PCBuild\amd64" (for 64bit versions). Note that activate.bat now handles this, so it would be awesome if this script did too. OTOH though, I wouldn't say that should be a hard-requirement, so if you don't want to add that support, I'd still have to say this version looks OK at face value.

Henri Wiechers added some commits
Henri Wiechers activate.ps1: Look for python.exe inside PCBuild and PBBuild\amd64. 519b8e9
Henri Wiechers activate.ps1: Move environment changes to after finding python.exe
This will leave the environment unmodified if python.exe can't be found.
1ed4ea1
@hwiechers

519b8e9 makes the script search the PCBuild and PCBuild\amd64 subdirectories
of the Python install path for python.exe. The parent directory of python.exe
is added to PATH instead of the install path.

I don't actually know if that works because I was unable to test it.

Please let me know.

1ed4ea1 delays changing the environment until python.exe. This avoids
unnecessary modification when python.exe can't be found.

bin/activate.ps1
((117 lines not shown))
}
-if (Test-Path Env:_OLD_PYTHONPATH) {
- $Env:PYTHONPATH = $Env:_OLD_PYTHONPATH;
-}
-else {
- $Env:PYTHONPATH = '';
+$script:PyInstallPath = $PyInstallPathKey.GetValue('');
+ForEach($subdir in @('', 'PCBuild', 'PCBuild\amd64')) {
+ $script:PyExeDir = Join-Path $PyInstallPath $_;
@mhammond Owner

It looks like this line should use $subdir instead of $_. With that change it works for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@hwiechers

Fixed.

I also fixed that the "Unable to find python.exe" wouldn't fire.

@mhammond
Owner

One meta question I don't understand - why do we need activate.ps1 at all? It seems running activate.bat from a powershell session works fine.

@hwiechers

The problem is that the batch files can't modify Powershell's environment variables.
Also batch file won't be able to set the prompt since that requires changing a Powershell function.

@Gozala
Owner

@hwiechers @mhammond any plans on finishing this ?

@hwiechers

I think it was finished but it was so long ago that changes to the SDK might have broken it.

Does anyone else want it?
It seemed like I was the only one and I haven't played with the SDK for a long time.

@mhammond
Owner

Nope - I don't think I ever intended to do a rewrite - I don't user powershell.

@Gozala
Owner

If anyone want's to update patch and land you're more then welcome to do it.
For now though I'll just close this one.

@Gozala Gozala closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2011
  1. Rewrote activate.ps1.

    Henri Wiechers authored
Commits on Nov 13, 2011
  1. activate.ps1: Formatting fixes.

    Henri Wiechers authored
  2. activate.ps1: Added check that Python 2.x is installed.

    Henri Wiechers authored
  3. activate.ps1: Formatting and variable naming.

    Henri Wiechers authored
  4. activate.py: Moved experimental note to start of script.

    Henri Wiechers authored
Commits on Nov 24, 2011
  1. activate.ps1: Fixed incorrectly named variable.

    Henri Wiechers authored
  2. activate.ps1: Scoped script variables correctly.

    Henri Wiechers authored
  3. activate.ps1: Move environment changes to after finding python.exe

    Henri Wiechers authored
    This will leave the environment unmodified if python.exe can't be found.
This page is out of date. Refresh to see the latest.
Showing with 81 additions and 75 deletions.
  1. +81 −75 bin/activate.ps1
View
156 bin/activate.ps1
@@ -1,95 +1,101 @@
-$Env:VIRTUAL_ENV = (gl);
-$Env:CUDDLEFISH_ROOT = $Env:VIRTUAL_ENV;
+# This is a translation of `activate` with the addition of putting
+# the latest 2.x Python version (>=PYTHON_MIN_VERSION) on the Path.
-# http://stackoverflow.com/questions/5648931/powershell-test-if-registry-value-exists/5652674#5652674
-Function Test-RegistryValue {
- param(
- [Alias("PSPath")]
- [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
- [String]$Path
- ,
- [Parameter(Position = 1, Mandatory = $true)]
- [String]$Name
- ,
- [Switch]$PassThru
- )
-
- process {
- if (Test-Path $Path) {
- $Key = Get-Item -LiteralPath $Path
- if ($Key.GetValue($Name, $null) -ne $null) {
- if ($PassThru) {
- Get-ItemProperty $Path $Name
- } else {
- $true
- }
- } else {
- $false
- }
- } else {
- $false
- }
+"Note: this PowerShell SDK activation script is experimental."
+
+$script:PYTHON_MIN_VERSION = [decimal]2.5
+
+function global:deactivate($nondestructive) {
+ if (Test-Path Env:_OLD_VIRTUAL_PATH) {
+ $Env:Path = $Env:_OLD_VIRTUAL_PATH;
+ Remove-Item Env:_OLD_VIRTUAL_PATH;
}
-}
-$WINCURVERKEY = 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion';
-$WIN64 = (Test-RegistryValue $WINCURVERKEY 'ProgramFilesDir (x86)');
+ if (Test-Path Function:_OLD_VIRTUAL_PROMPT) {
+ Set-Content Function:Prompt (Get-Content Function:_OLD_VIRTUAL_PROMPT);
+ Remove-Item Function:_OLD_VIRTUAL_PROMPT;
+ }
-if($WIN64) {
- $PYTHONKEY='HKLM:SOFTWARE\Wow6432Node\Python\PythonCore';
-}
-else {
- $PYTHONKEY='HKLM:SOFTWARE\Python\PythonCore';
-}
+ if (Test-Path Env:_OLD_PYTHONPATH) {
+ if ($Env:_OLD_PYTHONPATH -ne 'NONE') {
+ $Env:PYTHONPATH = $Env:_OLD_PYTHONPATH;
+ }
+ else {
+ Remove-Item Env:PYTHONPATH;
+ }
+ Remove-Item Env:_OLD_PYTHONPATH;
+ }
-$Env:PYTHONVERSION = '';
-$Env:PYTHONINSTALL = '';
+ if (Test-Path Env:CUDDLEFISH_ROOT) {
+ Remove-Item Env:CUDDLEFISH_ROOT;
+ }
-foreach ($version in @('2.6', '2.5', '2.4')) {
- if (Test-RegistryValue "$PYTHONKEY\$version\InstallPath" '(default)') {
- $Env:PYTHONVERSION = $version;
+ if (Test-Path Env:VIRTUAL_ENV) {
+ Remove-Item Env:VIRTUAL_ENV;
}
-}
-if ($Env:PYTHONVERSION) {
- $Env:PYTHONINSTALL = (Get-Item "$PYTHONKEY\$version\InstallPath)").'(default)';
+ if (-not $nondestructive) {
+ Remove-Item Function:deactivate;
+ }
}
-if ($Env:PYTHONINSTALL) {
- $Env:Path += ";$Env:PYTHONINSTALL";
+deactivate $True;
+
+$script:PyInstallPathKey = (
+ @('HKCU:SOFTWARE\Python\PythonCore\*\InstallPath',
+ 'HKLM:SOFTWARE\Python\PythonCore\*\InstallPath',
+ 'HKLM:SOFTWARE\Wow6432Node\Python\PythonCore\*\InstallPath',
+ 'HKCU:SOFTWARE\Wow6432Node\Python\PythonCore\*\InstallPath') |
+ Where-Object { Test-Path $_ } |
+ ForEach-Object {
+ Get-ChildItem $_ |
+ Add-Member -MemberType ScriptProperty `
+ -Name ParentName -Value {Split-Path -Leaf $this.PSParentPath} `
+ -PassThru |
+ Where-Object { $_.ParentName -match '2\.\d+$' } |
+ Add-Member -MemberType ScriptProperty `
+ -Name Version -Value {[decimal]$this.ParentName} -PassThru |
+ Where-Object { $_.Version -ge $PYTHON_MIN_VERSION }
+ Sort-Object Version |
+ Select-Object -Last 1
+ } |
+ Sort-Object Version |
+ Select-Object -Last 1 );
+
+if (!$PyInstallPathKey) {
+ "Error: A recent version of Python 2.x must be installed.";
+ return
}
-if (Test-Path Env:_OLD_PYTHONPATH) {
- $Env:PYTHONPATH = $Env:_OLD_PYTHONPATH;
-}
-else {
- $Env:PYTHONPATH = '';
+$script:PyInstallPath = $PyInstallPathKey.GetValue('');
+ForEach($subdir in @('', 'PCBuild', 'PCBuild\amd64')) {
+ $script:PossiblePyExePath = (
+ Join-Path (Join-Path $PyInstallPath $subdir) python.exe);
+ if (Test-Path $PossiblePyExePath) {
+ $script:PyExeDir = Split-Path $PossiblePyExePath
+ break;
+ }
}
-$Env:_OLD_PYTHONPATH=$Env:PYTHONPATH;
-$Env:PYTHONPATH= "$Env:VIRTUAL_ENV\python-lib;$Env:PYTHONPATH";
-
-if (Test-Path Function:_OLD_VIRTUAL_PROMPT) {
- Set-Content Function:Prompt (Get-Content Function:_OLD_VIRTUAL_PROMPT);
+if (!$PyExeDir) {
+ "Error: Unable to find python.exe in installation path " + $PyInstallPath
+ return;
}
-else {
- function global:_OLD_VIRTUAL_PROMPT {}
-}
-
-Set-Content Function:_OLD_VIRTUAL_PROMPT (Get-Content Function:Prompt);
-function global:prompt { "($Env:VIRTUAL_ENV) $(_OLD_VIRTUAL_PROMPT)"; };
+if (Test-Path Env:PYTHONPATH) {
+ $Env:_OLD_PYTHONPATH = $Env:PYTHONPATH
+} else {
+ $Env:_OLD_PYTHONPATH = 'NONE'
+};
+$Env:_OLD_VIRTUAL_PATH = $Env:PATH;
-if (Test-Path Env:_OLD_VIRTUAL_PATH) {
- $Env:PATH = $Env:_OLD_VIRTUAL_PATH;
-}
-else {
- $Env:_OLD_VIRTUAL_PATH = $Env:PATH;
-}
-
-$Env:Path="$Env:VIRTUAL_ENV\bin;$Env:Path"
+$Env:VIRTUAL_ENV = (Get-Location);
+$Env:CUDDLEFISH_ROOT = $Env:VIRTUAL_ENV;
+$Env:PYTHONPATH = "$Env:VIRTUAL_ENV\python-lib;$Env:PYTHONPATH";
+$Env:PATH = "$PyExeDir;$Env:VIRTUAL_ENV\bin;$Env:PATH";
-[System.Console]::WriteLine("Note: this PowerShell SDK activation script is experimental.")
+function global:_OLD_VIRTUAL_PROMPT {};
+Set-Content Function:_OLD_VIRTUAL_PROMPT (Get-Content Function:Prompt);
+Set-Content Function:prompt { "($Env:VIRTUAL_ENV) $(_OLD_VIRTUAL_PROMPT)"; };
python -c "from jetpack_sdk_env import welcome; welcome()"
-
Something went wrong with that request. Please try again.