|
| 1 | +# Author: Kyle Kastner <kastnerkyle@gmail.com> |
| 2 | +# License: BSD 3 clause |
| 3 | + |
| 4 | +# This script is a helper to download the base python, numpy, and scipy |
| 5 | +# packages from their respective websites. |
| 6 | +# To quickly execute the script, run the following Powershell command: |
| 7 | +# powershell.exe -ExecutionPolicy unrestricted "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/scikit-learn/scikit-learn/master/continuous_integration/windows/windows_testing_downloader.ps1'))" |
| 8 | + |
| 9 | +# This is a stopgap solution to make Windows testing easier |
| 10 | +# until Windows CI issues are resolved. |
| 11 | + |
| 12 | +# Rackspace's default Windows VMs have several security features enabled by default. |
| 13 | +# The DisableInternetExplorerESC function disables a feature which |
| 14 | +# prevents any webpage from opening without explicit permission. |
| 15 | +# This is a default setting of Windows VMs on Rackspace, and makes it annoying to |
| 16 | +# download other packages to test! |
| 17 | + |
| 18 | +# Powershell scripts are also disabled by default. One must run the command: |
| 19 | +# set-executionpolicy unrestricted |
| 20 | +# from a Powershell terminal with administrator rights to enable scripts. |
| 21 | +# To start an administrator Powershell terminal, right click second icon from the left on Windows Server 2012's bottom taskbar. |
| 22 | + |
| 23 | +param ( |
| 24 | + [string]$python = "None", |
| 25 | + [string]$nogit = "False" |
| 26 | +) |
| 27 | + |
| 28 | +function DisableInternetExplorerESC { |
| 29 | + # Disables InternetExplorerESC to enable easier manual downloads of testing packages. |
| 30 | + # https://stackoverflow.com/questions/9368305/disable-ie-security-on-windows-server-via-powershell |
| 31 | + $AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" |
| 32 | + $UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" |
| 33 | + Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0 |
| 34 | + Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0 |
| 35 | + Stop-Process -Name Explorer |
| 36 | + Write-Host "IE Enhanced Security Configuration (ESC) has been disabled." -ForegroundColor Green |
| 37 | +} |
| 38 | + |
| 39 | +function DownloadPackages ($package_dict, $append_string) { |
| 40 | + $webclient = New-Object System.Net.WebClient |
| 41 | + |
| 42 | + ForEach ($key in $package_dict.Keys) { |
| 43 | + $url = $package_dict[$key] |
| 44 | + $file = $key + $append_string |
| 45 | + if ($url -match "(\.*exe$)") { |
| 46 | + $file = $file + ".exe" |
| 47 | + } elseif ($url -match "(\.*msi$)") { |
| 48 | + $file = $file + ".msi" |
| 49 | + } else { |
| 50 | + $file = $file + ".py" |
| 51 | + } |
| 52 | + $basedir = $pwd.Path + "\" |
| 53 | + $filepath = $basedir + $file |
| 54 | + Write-Host "Downloading" $file "from" $url |
| 55 | + |
| 56 | + # Retry up to 5 times in case of network transient errors. |
| 57 | + $retry_attempts = 5 |
| 58 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 59 | + try{ |
| 60 | + $webclient.DownloadFile($url, $filepath) |
| 61 | + break |
| 62 | + } |
| 63 | + Catch [Exception]{ |
| 64 | + Start-Sleep 1 |
| 65 | + } |
| 66 | + } |
| 67 | + Write-Host "File saved at" $filepath |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function InstallPython($match_string) { |
| 72 | + $pkg_regex = "python" + $match_string + "*" |
| 73 | + $pkg = Get-ChildItem -Filter $pkg_regex -Name |
| 74 | + Invoke-Expression -Command "msiexec /qn /i $pkg" |
| 75 | + |
| 76 | + Write-Host "Installing Python" |
| 77 | + Start-Sleep 25 |
| 78 | + Write-Host "Python installation complete" |
| 79 | +} |
| 80 | + |
| 81 | +function InstallPip($match_string, $python_version) { |
| 82 | + $pkg_regex = "get-pip" + $match_string + "*" |
| 83 | + $py = $python_version -replace "\." |
| 84 | + $pkg = Get-ChildItem -Filter $pkg_regex -Name |
| 85 | + $python_path = "C:\Python" + $py + "\python.exe" |
| 86 | + Invoke-Expression -Command "$python_path $pkg" |
| 87 | +} |
| 88 | + |
| 89 | +function EnsurePip($python_version) { |
| 90 | + $py = $python_version -replace "\." |
| 91 | + $python_path = "C:\Python" + $py + "\python.exe" |
| 92 | + Invoke-Expression -Command "$python_path -m ensurepip" |
| 93 | +} |
| 94 | + |
| 95 | +function GetPythonHome($python_version) { |
| 96 | + $py = $python_version -replace "\." |
| 97 | + $pypath = "C:\Python" + $py + "\" |
| 98 | + return $pypath |
| 99 | +} |
| 100 | + |
| 101 | +function GetPipPath($python_version) { |
| 102 | + $py = $python_version -replace "\." |
| 103 | + $pypath = GetPythonHome $python_version |
| 104 | + if ($py.StartsWith("3")) { |
| 105 | + $pip = $pypath + "Scripts\pip3.exe" |
| 106 | + } else { |
| 107 | + $pip = $pypath + "Scripts\pip.exe" |
| 108 | + } |
| 109 | + return $pip |
| 110 | +} |
| 111 | + |
| 112 | +function PipInstall($pkg_name, $python_version, $extra_args) { |
| 113 | + $pip = GetPipPath $python_version |
| 114 | + Invoke-Expression -Command "$pip install $pkg_name" |
| 115 | +} |
| 116 | + |
| 117 | +function InstallNose($python_version) { |
| 118 | + PipInstall "nose" $python_version |
| 119 | +} |
| 120 | + |
| 121 | +function WheelInstall($name, $url, $python_version) { |
| 122 | + $pip = GetPipPath $python_version |
| 123 | + $args = "install --use-wheel --no-index" |
| 124 | + Invoke-Expression -Command "$pip $args $url $name" |
| 125 | +} |
| 126 | + |
| 127 | +function InstallWheel($python_version) { |
| 128 | + PipInstall "virtualenv" $python_version |
| 129 | + PipInstall "wheel" $python_version |
| 130 | +} |
| 131 | + |
| 132 | +function InstallNumpy($package_dict, $python_version) { |
| 133 | + #Don't pass name so we can use URL directly. |
| 134 | + WheelInstall "" $package_dict["numpy"] $python_version |
| 135 | +} |
| 136 | + |
| 137 | +function InstallScipy($package_dict, $python_version) { |
| 138 | + #Don't pass name so we can use URL directly. |
| 139 | + WheelInstall "" $package_dict["scipy"] $python_version |
| 140 | +} |
| 141 | + |
| 142 | +function InstallGit { |
| 143 | + $pkg_regex = "git*" |
| 144 | + $pkg = Get-ChildItem -Filter $pkg_regex -Name |
| 145 | + $pkg_cmd = $pwd.ToString() + "\" + $pkg + " /verysilent" |
| 146 | + Invoke-Expression -Command $pkg_cmd |
| 147 | + |
| 148 | + Write-Host "Installing Git" |
| 149 | + Start-Sleep 20 |
| 150 | + # Remove the installer - seems to cause weird issues with Git Bash |
| 151 | + Invoke-Expression -Command "rm git.exe" |
| 152 | + Write-Host "Git installation complete" |
| 153 | +} |
| 154 | + |
| 155 | +function ReadAndUpdateFromRegistry { |
| 156 | + # https://stackoverflow.com/questions/14381650/how-to-update-windows-powershell-session-environment-variables-from-registry |
| 157 | + foreach($level in "Machine","User") { |
| 158 | + [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % { |
| 159 | + # For Path variables, append the new values, if they're not already in there |
| 160 | + if($_.Name -match 'Path$') { |
| 161 | + $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';' |
| 162 | + } |
| 163 | + $_ |
| 164 | + } | Set-Content -Path { "Env:$($_.Name)" } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +function UpdatePaths($python_version) { |
| 169 | + #This function makes local path updates required in order to install Python and supplementary packages in a single shell. |
| 170 | + $pypath = GetPythonHome $python_version |
| 171 | + $env:PATH = $env:PATH + ";" + $pypath |
| 172 | + $env:PYTHONPATH = $pypath + "DLLs;" + $pypath + "Lib;" + $pypath + "Lib\site-packages" |
| 173 | + $env:PYTHONHOME = $pypath |
| 174 | + Write-Host "PYTHONHOME temporarily set to" $env:PYTHONHOME |
| 175 | + Write-Host "PYTHONPATH temporarily set to" $env:PYTHONPATH |
| 176 | + Write-Host "PATH temporarily set to" $env:PATH |
| 177 | +} |
| 178 | + |
| 179 | +function Python27URLs { |
| 180 | + # Function returns a dictionary of packages to download for Python 2.7. |
| 181 | + $urls = @{ |
| 182 | + "python" = "https://www.python.org/ftp/python/2.7.7/python-2.7.7.msi" |
| 183 | + "numpy" = "http://28daf2247a33ed269873-7b1aad3fab3cc330e1fd9d109892382a.r6.cf2.rackcdn.com/numpy-1.8.1-cp27-none-win32.whl" |
| 184 | + "scipy" = "http://28daf2247a33ed269873-7b1aad3fab3cc330e1fd9d109892382a.r6.cf2.rackcdn.com/scipy-0.14.0-cp27-none-win32.whl" |
| 185 | + "get-pip" = "https://bootstrap.pypa.io/get-pip.py" |
| 186 | + } |
| 187 | + return $urls |
| 188 | +} |
| 189 | + |
| 190 | +function Python34URLs { |
| 191 | + # Function returns a dictionary of packages to download for Python 3.4. |
| 192 | + $urls = @{ |
| 193 | + "python" = "https://www.python.org/ftp/python/3.4.1/python-3.4.1.msi" |
| 194 | + "numpy" = "http://28daf2247a33ed269873-7b1aad3fab3cc330e1fd9d109892382a.r6.cf2.rackcdn.com/numpy-1.8.1-cp34-none-win32.whl" |
| 195 | + "scipy" = "http://28daf2247a33ed269873-7b1aad3fab3cc330e1fd9d109892382a.r6.cf2.rackcdn.com/scipy-0.14.0-cp34-none-win32.whl" |
| 196 | + } |
| 197 | + return $urls |
| 198 | +} |
| 199 | + |
| 200 | +function GitURLs { |
| 201 | + # Function returns a dictionary of packages to download for Git |
| 202 | + $urls = @{ |
| 203 | + "git" = "https://github.com/msysgit/msysgit/releases/download/Git-1.9.4-preview20140611/Git-1.9.4-preview20140611.exe" |
| 204 | + } |
| 205 | + return $urls |
| 206 | +} |
| 207 | + |
| 208 | +function main { |
| 209 | + $versions = @{ |
| 210 | + "2.7" = Python27URLs |
| 211 | + "3.4" = Python34URLs |
| 212 | + } |
| 213 | + |
| 214 | + if ($nogit -eq "False") { |
| 215 | + Write-Host "Downloading and installing Gitbash" |
| 216 | + $urls = GitURLs |
| 217 | + DownloadPackages $urls "" |
| 218 | + InstallGit ".exe" |
| 219 | + } |
| 220 | + |
| 221 | + if (($python -eq "None")) { |
| 222 | + Write-Host "Installing all supported python versions" |
| 223 | + Write-Host "Current versions supported are:" |
| 224 | + ForEach ($key in $versions.Keys) { |
| 225 | + Write-Host $key |
| 226 | + $all_python += @($key) |
| 227 | + } |
| 228 | + } elseif(!($versions.ContainsKey($python))) { |
| 229 | + Write-Host "Python version not recognized!" |
| 230 | + Write-Host "Pass python version with -python" |
| 231 | + Write-Host "Current versions supported are:" |
| 232 | + ForEach ($key in $versions.Keys) { |
| 233 | + Write-Host $key |
| 234 | + } |
| 235 | + return |
| 236 | + } else { |
| 237 | + $all_python += @($python) |
| 238 | + } |
| 239 | + ForEach ($py in $all_python) { |
| 240 | + Write-Host "Installing Python" $py |
| 241 | + DisableInternetExplorerESC |
| 242 | + $pystring = $py -replace "\." |
| 243 | + $pystring = "_py" + $pystring |
| 244 | + $package_dict = $versions[$py] |
| 245 | + |
| 246 | + # This will download the whl packages as well which is |
| 247 | + # clunky but makes configuration simpler. |
| 248 | + DownloadPackages $package_dict $pystring |
| 249 | + UpdatePaths $py |
| 250 | + InstallPython $pystring |
| 251 | + ReadAndUpdateFromRegistry |
| 252 | + if ($package_dict.ContainsKey("get-pip")) { |
| 253 | + InstallPip $pystring $py |
| 254 | + } else { |
| 255 | + EnsurePip $py |
| 256 | + } |
| 257 | + InstallNose $py |
| 258 | + InstallWheel $py |
| 259 | + |
| 260 | + # The installers below here use wheel packages. |
| 261 | + # Wheels were created from CGohlke's installers with |
| 262 | + # wheel convert <exefile> |
| 263 | + # These are hosted in Rackspace Cloud Files. |
| 264 | + InstallNumpy $package_dict $py |
| 265 | + InstallScipy $package_dict $py |
| 266 | + } |
| 267 | + return |
| 268 | +} |
| 269 | + |
| 270 | +main |
0 commit comments