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