|
| 1 | +# Sample script to install Python and pip under Windows |
| 2 | +# Authors: Olivier Grisel, Jonathan Helmus and Kyle Kastner |
| 3 | +# License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ |
| 4 | + |
| 5 | +$MINICONDA_URL = "http://repo.continuum.io/miniconda/" |
| 6 | +$FSAVERAGE_URL = "http://faculty.washington.edu/larsoner/fsaverage_min.tar.gz" |
| 7 | + |
| 8 | +function DownloadExtractFsaverage () { |
| 9 | + $webclient = New-Object System.Net.WebClient |
| 10 | + $basedir = $pwd.Path + "\" |
| 11 | + $filepath = $basedir + "fsaverage_min.zip" |
| 12 | + # Download and retry up to 3 times in case of network transient errors. |
| 13 | + $url = $FSAVERAGE_URL |
| 14 | + Write-Host "Downloading" $url |
| 15 | + $retry_attempts = 2 |
| 16 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 17 | + try { |
| 18 | + $webclient.DownloadFile($url, $filepath) |
| 19 | + break |
| 20 | + } |
| 21 | + Catch [Exception]{ |
| 22 | + Start-Sleep 1 |
| 23 | + } |
| 24 | + } |
| 25 | + if (Test-Path $filepath) { |
| 26 | + Write-Host "File saved at" $filepath |
| 27 | + } else { |
| 28 | + # Retry once to get the error message if any at the last try |
| 29 | + $webclient.DownloadFile($url, $filepath) |
| 30 | + } |
| 31 | + # Now we extract |
| 32 | + $subjects_dir = $basedir + "\subjects" |
| 33 | + New-Item -ItemType directory -Path $subjects_dir |
| 34 | + $shell = new-object -com shell.application |
| 35 | + $zip = $shell.NameSpace($filepath) |
| 36 | + foreach($item in $zip.items()) |
| 37 | + { |
| 38 | + $shell.Namespace($subjects_dir).copyhere($item) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +function DownloadMiniconda ($python_version, $platform_suffix) { |
| 44 | + $webclient = New-Object System.Net.WebClient |
| 45 | + if ($python_version -eq "3.4") { |
| 46 | + $filename = "Miniconda3-3.5.5-Windows-" + $platform_suffix + ".exe" |
| 47 | + } else { |
| 48 | + $filename = "Miniconda-3.5.5-Windows-" + $platform_suffix + ".exe" |
| 49 | + } |
| 50 | + $url = $MINICONDA_URL + $filename |
| 51 | + |
| 52 | + $basedir = $pwd.Path + "\" |
| 53 | + $filepath = $basedir + $filename |
| 54 | + if (Test-Path $filename) { |
| 55 | + Write-Host "Reusing" $filepath |
| 56 | + return $filepath |
| 57 | + } |
| 58 | + |
| 59 | + # Download and retry up to 3 times in case of network transient errors. |
| 60 | + Write-Host "Downloading" $filename "from" $url |
| 61 | + $retry_attempts = 2 |
| 62 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 63 | + try { |
| 64 | + $webclient.DownloadFile($url, $filepath) |
| 65 | + break |
| 66 | + } |
| 67 | + Catch [Exception]{ |
| 68 | + Start-Sleep 1 |
| 69 | + } |
| 70 | + } |
| 71 | + if (Test-Path $filepath) { |
| 72 | + Write-Host "File saved at" $filepath |
| 73 | + } else { |
| 74 | + # Retry once to get the error message if any at the last try |
| 75 | + $webclient.DownloadFile($url, $filepath) |
| 76 | + } |
| 77 | + return $filepath |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +function InstallMiniconda ($python_version, $architecture, $python_home) { |
| 82 | + Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home |
| 83 | + if (Test-Path $python_home) { |
| 84 | + Write-Host $python_home "already exists, skipping." |
| 85 | + return $false |
| 86 | + } |
| 87 | + if ($architecture -eq "32") { |
| 88 | + $platform_suffix = "x86" |
| 89 | + } else { |
| 90 | + $platform_suffix = "x86_64" |
| 91 | + } |
| 92 | + $filepath = DownloadMiniconda $python_version $platform_suffix |
| 93 | + Write-Host "Installing" $filepath "to" $python_home |
| 94 | + $install_log = $python_home + ".log" |
| 95 | + $args = "/S /D=$python_home" |
| 96 | + Write-Host $filepath $args |
| 97 | + Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru |
| 98 | + if (Test-Path $python_home) { |
| 99 | + Write-Host "Python $python_version ($architecture) installation complete" |
| 100 | + } else { |
| 101 | + Write-Host "Failed to install Python in $python_home" |
| 102 | + Get-Content -Path $install_log |
| 103 | + Exit 1 |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +function InstallMinicondaPip ($python_home) { |
| 109 | + $pip_path = $python_home + "\Scripts\pip.exe" |
| 110 | + $conda_path = $python_home + "\Scripts\conda.exe" |
| 111 | + if (-not(Test-Path $pip_path)) { |
| 112 | + Write-Host "Installing pip..." |
| 113 | + $args = "install --yes pip" |
| 114 | + Write-Host $conda_path $args |
| 115 | + Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru |
| 116 | + } else { |
| 117 | + Write-Host "pip already installed." |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +function main () { |
| 123 | + DownloadExtractFsaverage |
| 124 | + InstallMiniconda $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON |
| 125 | + InstallMinicondaPip $env:PYTHON |
| 126 | +} |
| 127 | + |
| 128 | +main |
0 commit comments