| 1 |
#!/usr/bin/env powershell |
| 2 |
# Install Python 3.8 for x64 and x86 in order to build wheels on Windows. |
| 3 |
|
| 4 |
Set-StrictMode -Version 2 |
| 5 |
$ErrorActionPreference = 'Stop' |
| 6 |
|
| 7 |
trap { |
| 8 |
$ErrorActionPreference = "Continue" |
| 9 |
Write-Error $_ |
| 10 |
exit 1 |
| 11 |
} |
| 12 |
|
| 13 |
# Avoid "Could not create SSL/TLS secure channel" |
| 14 |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 15 |
|
| 16 |
function Install-Python { |
| 17 |
Param( |
| 18 |
[string]$PythonVersion, |
| 19 |
[string]$PythonInstaller, |
| 20 |
[string]$PythonInstallPath, |
| 21 |
[string]$PythonInstallerHash |
| 22 |
) |
| 23 |
$PythonInstallerUrl = "https://www.python.org/ftp/python/$PythonVersion/$PythonInstaller.exe" |
| 24 |
$PythonInstallerPath = "C:\tools\$PythonInstaller.exe" |
| 25 |
|
| 26 |
# Downloads installer |
| 27 |
Write-Host "Downloading the Python installer: $PythonInstallerUrl => $PythonInstallerPath" |
| 28 |
Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $PythonInstallerPath |
| 29 |
|
| 30 |
# Validates checksum |
| 31 |
$HashFromDownload = Get-FileHash -Path $PythonInstallerPath -Algorithm MD5 |
| 32 |
if ($HashFromDownload.Hash -ne $PythonInstallerHash) { |
| 33 |
throw "Invalid Python installer: failed checksum!" |
| 34 |
} |
| 35 |
Write-Host "Python installer $PythonInstallerPath validated." |
| 36 |
|
| 37 |
# Installs Python |
| 38 |
& $PythonInstallerPath /passive InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir=$PythonInstallPath |
| 39 |
if (-Not $?) { |
| 40 |
throw "The Python installation exited with error!" |
| 41 |
} |
| 42 |
|
| 43 |
# NOTE(lidiz) Even if the install command finishes in the script, that |
| 44 |
# doesn't mean the Python installation is finished. If using "ps" to check |
| 45 |
# for running processes, you might see ongoing installers at this point. |
| 46 |
# So, we needs this "hack" to reliably validate that the Python binary is |
| 47 |
# functioning properly. |
| 48 |
|
| 49 |
# Wait for the installer process |
| 50 |
Wait-Process -Name $PythonInstaller -Timeout 300 |
| 51 |
Write-Host "Installation process exits normally." |
| 52 |
|
| 53 |
# Validate Python binary |
| 54 |
$PythonBinary = "$PythonInstallPath\python.exe" |
| 55 |
& $PythonBinary -c 'print(42)' |
| 56 |
Write-Host "Python binary works properly." |
| 57 |
|
| 58 |
# Installs pip |
| 59 |
& $PythonBinary -m ensurepip --user |
| 60 |
|
| 61 |
Write-Host "Python $PythonVersion installed by $PythonInstaller at $PythonInstallPath." |
| 62 |
} |
| 63 |
|
| 64 |
# Python 3.8 |
| 65 |
$Python38x86Config = @{ |
| 66 |
PythonVersion = "3.8.10" |
| 67 |
PythonInstaller = "python-3.8.10" |
| 68 |
PythonInstallPath = "C:\Python38_32bit" |
| 69 |
PythonInstallerHash = "b355cfc84b681ace8908ae50908e8761" |
| 70 |
} |
| 71 |
Install-Python @Python38x86Config |
| 72 |
|
| 73 |
$Python38x64Config = @{ |
| 74 |
PythonVersion = "3.8.10" |
| 75 |
PythonInstaller = "python-3.8.10-amd64" |
| 76 |
PythonInstallPath = "C:\Python38" |
| 77 |
PythonInstallerHash = "62cf1a12a5276b0259e8761d4cf4fe42" |
| 78 |
} |
| 79 |
Install-Python @Python38x64Config |
| 80 |
|
| 81 |
# Python 3.9 |
| 82 |
$Python39x86Config = @{ |
| 83 |
PythonVersion = "3.9.11" |
| 84 |
PythonInstaller = "python-3.9.11" |
| 85 |
PythonInstallPath = "C:\Python39_32bit" |
| 86 |
PythonInstallerHash = "4210652b14a030517046cdf111c09c1e" |
| 87 |
} |
| 88 |
Install-Python @Python39x86Config |
| 89 |
|
| 90 |
$Python39x64Config = @{ |
| 91 |
PythonVersion = "3.9.11" |
| 92 |
PythonInstaller = "python-3.9.11-amd64" |
| 93 |
PythonInstallPath = "C:\Python39" |
| 94 |
PythonInstallerHash = "fef52176a572efd48b7148f006b25801" |
| 95 |
} |
| 96 |
Install-Python @Python39x64Config |
| 97 |
|
| 98 |
# Python 3.10 |
| 99 |
$Python310x86Config = @{ |
| 100 |
PythonVersion = "3.10.3" |
| 101 |
PythonInstaller = "python-3.10.3" |
| 102 |
PythonInstallPath = "C:\Python310_32bit" |
| 103 |
PythonInstallerHash = "6a336cb2aca62dd05805316ab3aaf2b5" |
| 104 |
} |
| 105 |
Install-Python @Python310x86Config |
| 106 |
|
| 107 |
$Python310x64Config = @{ |
| 108 |
PythonVersion = "3.10.3" |
| 109 |
PythonInstaller = "python-3.10.3-amd64" |
| 110 |
PythonInstallPath = "C:\Python310" |
| 111 |
PythonInstallerHash = "9ea305690dbfd424a632b6a659347c1e" |
| 112 |
} |
| 113 |
Install-Python @Python310x64Config |
| 114 |
|
| 115 |
# Python 3.11 |
| 116 |
$Python311x86Config = @{ |
| 117 |
PythonVersion = "3.11.0" |
| 118 |
PythonInstaller = "python-3.11.0rc1" |
| 119 |
PythonInstallPath = "C:\Python311_32bit" |
| 120 |
PythonInstallerHash = "d2e5420e53d9e71c82b4a19763dbaa12" |
| 121 |
} |
| 122 |
Install-Python @Python311x86Config |
| 123 |
|
| 124 |
$Python311x64Config = @{ |
| 125 |
PythonVersion = "3.11.0" |
| 126 |
PythonInstaller = "python-3.11.0rc1-amd64" |
| 127 |
PythonInstallPath = "C:\Python311" |
| 128 |
PythonInstallerHash = "5943d8702e40a5ccd62e5a8d4c8852aa" |
| 129 |
} |
| 130 |
Install-Python @Python311x64Config |