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 
Install-Pythonnull16 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.0"
67     PythonInstaller = "python-3.8.0"
68     PythonInstallPath = "C:\Python38_32bit"
69     PythonInstallerHash = "412a649d36626d33b8ca5593cf18318c"
70 }
71 Install-Python @Python38x86Config
72 
73 $Python38x64Config = @{
74     PythonVersion = "3.8.0"
75     PythonInstaller = "python-3.8.0-amd64"
76     PythonInstallPath = "C:\Python38"
77     PythonInstallerHash = "29ea87f24c32f5e924b7d63f8a08ee8d"
78 }
79 Install-Python @Python38x64Config
80 
81 # Python 3.9
82 $Python39x86Config = @{
83     PythonVersion = "3.9.0"
84     PythonInstaller = "python-3.9.0"
85     PythonInstallPath = "C:\Python39_32bit"
86     PythonInstallerHash = "4a2812db8ab9f2e522c96c7728cfcccb"
87 }
88 Install-Python @Python39x86Config
89 
90 $Python39x64Config = @{
91     PythonVersion = "3.9.0"
92     PythonInstaller = "python-3.9.0-amd64"
93     PythonInstallPath = "C:\Python39"
94     PythonInstallerHash = "b61a33dc28f13b561452f3089c87eb63"
95 }
96 Install-Python @Python39x64Config
97