|
| 1 | +$PythonInstaller = "$PSScriptRoot\python3.7.3.exe" |
| 2 | +$StepNumber = 1 |
| 3 | + |
| 4 | +function Write-StepTitle([String] $StepMessage) { |
| 5 | + $StepMessage="$Script:StepNumber. $StepMessage" |
| 6 | + $MessageLength=$StepMessage.length |
| 7 | + $RepeatedStars = "*" * $MessageLength |
| 8 | + Write-Host "" |
| 9 | + Write-Host $RepeatedStars |
| 10 | + Write-Host $StepMessage |
| 11 | + Write-Host $RepeatedStars |
| 12 | + $Script:StepNumber = $Script:StepNumber + 1 |
| 13 | +} |
| 14 | + |
| 15 | +function Get-PythonExecutable { |
| 16 | + Write-StepTitle "Looking for existing Python 3.7.3 installation." |
| 17 | + return Get-WmiObject -Namespace "root/cimv2" -Class Win32_Product -Filter "Name Like 'Python 3.7.3 Executables%'" |
| 18 | +} |
| 19 | + |
| 20 | +function Get-PythonInstallationTarget { |
| 21 | + if ([Environment]::Is64BitOperatingSystem) { |
| 22 | + Write-StepTitle "Downloading x64 version of Python." |
| 23 | + return "https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64-webinstall.exe" |
| 24 | + } else { |
| 25 | + Write-StepTitle "Downloading x86 version of Python." |
| 26 | + return "https://www.python.org/ftp/python/3.7.3/python-3.7.3-webinstall.exe" |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function Get-PythonMSI { |
| 31 | + if ([System.IO.File]::Exists($PythonInstaller)) { |
| 32 | + Remove-Item $PythonInstaller |
| 33 | + } |
| 34 | + $url = Get-PythonInstallationTarget |
| 35 | + $client = New-Object System.Net.WebClient |
| 36 | + try { |
| 37 | + $client.DownloadFile($url, $PythonInstaller) |
| 38 | + } catch { |
| 39 | + Write-Host "Failed to download Python. The following exception was raised:" -ForegroundColor Red |
| 40 | + Write-Host $_.exception -ForegroundColor Red |
| 41 | + |
| 42 | + Exit 1 |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function Install-Python { |
| 47 | + Write-StepTitle "Installing Python. Do not close this window." |
| 48 | + Write-Host "Installing Python. Do not close this window." |
| 49 | + $install = Start-Process $PythonInstaller -ArgumentList "InstallAllUsers=1 PrependPath=1" -PassThru -wait |
| 50 | + if ($install.ExitCode -eq 0) { |
| 51 | + Write-Host "Installation completed successfully." -ForegroundColor Green |
| 52 | + } elseif ($install.ExitCode -eq 1602) { |
| 53 | + Write-Host "Installer was exited by the user." -ForegroundColor Red |
| 54 | + Exit 1 |
| 55 | + } else { |
| 56 | + Write-Host "Installation failed with exit code $install.ExitCode" -ForegroundColor Red |
| 57 | + Exit 1 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function Update-UserEnvironmentPath { |
| 62 | + Write-StepTitle "Updating Environment PATH of this shell." |
| 63 | + $env:Path = |
| 64 | + [System.Environment]::GetEnvironmentVariable("Path","Machine") + |
| 65 | + ";" + |
| 66 | + [System.Environment]::GetEnvironmentVariable("Path","User") |
| 67 | +} |
| 68 | + |
| 69 | +function Install-Virtualenv { |
| 70 | + Write-StepTitle "Installing virtualenv" |
| 71 | + Invoke-Expression "pip install virtualenv --target $PSScriptRoot\virtualenv --upgrade" |
| 72 | +} |
| 73 | + |
| 74 | +$PythonExecutable = Get-PythonExecutable |
| 75 | +if ($PythonExecutable.count -eq 0) { |
| 76 | + Get-PythonMSI |
| 77 | + Install-Python |
| 78 | + Update-UserEnvironmentPath |
| 79 | + Remove-Item $PythonInstaller |
| 80 | +} else { |
| 81 | + Write-Host "Python 3.7.3 is already installed." -ForegroundColor Green |
| 82 | +} |
| 83 | +Install-Virtualenv |
0 commit comments