PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/build_tools/windows/windows_testing_downloader.ps1

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