Commits

Arun Mahapatra  committed e71302e

Replace use of WOW6432Node registry keys with reg.exe

  • Participants
  • Parent commits cbb4c22

Comments (0)

Files changed (1)

File VirtualEnvWrapper/Win.psm1

 
 function switchPythonCore
 {
-    $pythonCoreRegistry = "HKCU:/Software/Python/PythonCore"
     $currPython = getCurrentPythonExe
     $currPythonW = getCurrentPythonWExe
     $pyVer = & { $OFS = ""; "$((& "python.exe" "-V" 2>&1).exception.message.split(" ")[1][0..2])" }
     $pyArch = $(& "python.exe" -c 'import platform; print platform.architecture()[0]')
+    $pyRegRoot = "HKCU\Software\Python\PythonCore"
+    $regView = $null
 
-    if ($pyArch -eq "32bit")
+    # Python installed for current user stays in HKCU irrespective of architecture
+    $null = & "reg.exe" "query" "$pyRegRoot\$pyVer\InstallPath" 2> $null
+    if ($lastExitCode -ne 0)
     {
-        $pythonCoreRegistry = "HKLM:/Software/Wow6432Node/Python/PythonCore"
+        if ($pyArch -eq "32bit")
+        {
+            $regView = "/reg:32"
+        }
+
+        # System wide python install goes into architecture specific registry in HKLM
+        $pyRegRoot = "HKLM\Software\Python\PythonCore"
+        $null = & "reg.exe" "query" "$pyRegRoot\$pyVer\InstallPath" "$regView" 2> $null
+        if ($lastExitCode -ne 0)
+        {
+            throw "Unable to locate python install path from registry. Is python installed?"
+        }
     }
 
     if ($currPython -like "*Scripts*")
     {
         $pyBase = resolve-path (split-path $currPython -parent)
     }
-    if (-not(test-path $pythonCoreRegistry))
-    {
-        new-item "$pythonCoreRegistry/$pyVer/InstallPath" -Force > $null
-        new-item "$pythonCoreRegistry/$pyVer/PythonPath" > $null
-    }
-    set-itemproperty -path "$pythonCoreRegistry/$pyVer/InstallPath" -name "(default)" -value $pyBase
-    set-itemproperty -path "$pythonCoreRegistry/$pyVer/PythonPath" -name "(default)" -value "$pyBase\Lib;$pyBase\DLLs;$pyBase\Lib\lib-tk"
+
+    $null = & 'reg.exe' 'add' "$pyRegRoot\$pyVer\InstallPath" '/ve' '/d' "$pyBase" '/t' 'REG_SZ' "$regView" '/f'
+    $null = & 'reg.exe' 'add' "$pyRegRoot\$pyVer\PythonPath" '/ve' '/d' "$pyBase\Lib;$pyBase\DLLs;$pyBase\Lib\lib-tk" '/t' 'REG_SZ' "$regView" '/f'
 }
 
 function Switch-DefaultPython