From db4ce94ebb3e42a05aa49bf57c565d889e44f603 Mon Sep 17 00:00:00 2001
From: Lidi Zheng <scallopsky@gmail.com>
Date: Mon, 2 Dec 2019 10:59:03 -0800
Subject: [PATCH] Revert "Revert "Release Python3.8 wheels for Windows""

---
 .../helper_scripts/install_python38.ps1       | 78 +++++++++++++++++++
 .../helper_scripts/prepare_build_windows.bat  |  3 +
 .../windows/grpc_build_artifacts.bat          |  7 --
 .../windows/grpc_build_packages.bat           |  6 --
 .../internal_ci/windows/grpc_distribtests.bat |  6 --
 .../windows/grpc_distribtests_standalone.bat  |  6 --
 tools/run_tests/artifacts/artifact_targets.py | 14 ++--
 .../artifacts/build_artifact_python.bat       |  6 +-
 8 files changed, 90 insertions(+), 36 deletions(-)
 create mode 100644 tools/internal_ci/helper_scripts/install_python38.ps1

diff --git a/tools/internal_ci/helper_scripts/install_python38.ps1 b/tools/internal_ci/helper_scripts/install_python38.ps1
new file mode 100644
index 0000000000..e47f83b012
--- /dev/null
+++ b/tools/internal_ci/helper_scripts/install_python38.ps1
@@ -0,0 +1,78 @@
+#!/usr/bin/env powershell
+# Install Python 3.8 for x64 and x86 in order to build wheels on Windows.
+
+Set-StrictMode -Version 2
+
+# Avoid "Could not create SSL/TLS secure channel"
+[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+
+function Install-Python {
+    Param(
+        [string]$PythonVersion,
+        [string]$PythonInstaller,
+        [string]$PythonInstallPath,
+        [string]$PythonInstallerHash
+    )
+    $PythonInstallerUrl = "https://www.python.org/ftp/python/$PythonVersion/$PythonInstaller"
+    $PythonInstallerPath = "C:\tools\$PythonInstaller"
+
+    # Downloads installer
+    Write-Host "Downloading the Python installer: $PythonInstallerUrl => $PythonInstallerPath"
+    Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $PythonInstallerPath
+
+    # Validates checksum
+    $HashFromDownload = Get-FileHash -Path $PythonInstallerPath -Algorithm MD5
+    if ($HashFromDownload.Hash -ne $PythonInstallerHash) {
+        throw "Invalid Python installer: failed checksum!"
+    }
+    Write-Host "Python installer $PythonInstallerPath validated."
+
+    # Installs Python
+    & $PythonInstallerPath /passive InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir=$PythonInstallPath
+    if (-Not $?) {
+        throw "The Python installation exited with error!"
+    }
+
+    # Validates Python binary
+    # NOTE(lidiz) Even if the install command finishes in the script, that
+    # doesn't mean the Python installation is finished. If using "ps" to check
+    # for running processes, you might see ongoing installers at this point.
+    # So, we needs this "hack" to reliably validate that the Python binary is
+    # functioning properly.
+    $ValidationStartTime = Get-Date
+    $EarlyExitDDL = $ValidationStartTime.addminutes(5)
+    $PythonBinary = "$PythonInstallPath\python.exe"
+    While ($True) {
+        $CurrentTime = Get-Date
+        if ($CurrentTime -ge $EarlyExitDDL) {
+            throw "Invalid Python installation! Timeout!"
+        }
+        & $PythonBinary -c 'print(42)'
+        if ($?) {
+            Write-Host "Python binary works properly."
+            break
+        }
+        Start-Sleep -Seconds 1
+    }
+
+    # Installs pip
+    & $PythonBinary -m ensurepip --user
+
+    Write-Host "Python $PythonVersion installed by $PythonInstaller at $PythonInstallPath."
+}
+
+$Python38x86Config = @{
+    PythonVersion = "3.8.0"
+    PythonInstaller = "python-3.8.0.exe"
+    PythonInstallPath = "C:\Python38_32bit"
+    PythonInstallerHash = "412a649d36626d33b8ca5593cf18318c"
+}
+Install-Python @Python38x86Config
+
+$Python38x64Config = @{
+    PythonVersion = "3.8.0"
+    PythonInstaller = "python-3.8.0-amd64.exe"
+    PythonInstallPath = "C:\Python38"
+    PythonInstallerHash = "29ea87f24c32f5e924b7d63f8a08ee8d"
+}
+Install-Python @Python38x64Config
diff --git a/tools/internal_ci/helper_scripts/prepare_build_windows.bat b/tools/internal_ci/helper_scripts/prepare_build_windows.bat
index d129332c3b..bfd812345a 100644
--- a/tools/internal_ci/helper_scripts/prepare_build_windows.bat
+++ b/tools/internal_ci/helper_scripts/prepare_build_windows.bat
@@ -35,6 +35,9 @@ python -m pip install google-api-python-client
 powershell -File src\csharp\install_dotnet_sdk.ps1
 set PATH=%LOCALAPPDATA%\Microsoft\dotnet;%PATH%
 
+@rem Install Python 3.8.0
+powershell -File tools\internal_ci\helper_scripts\install_python38.ps1
+
 @rem Newest version of Go is required to be able to build boringssl with cmake
 @rem TODO(jtattermusch): try to eliminate the dependency on Go
 choco install golang -y --version 1.13.1 --limit-output
diff --git a/tools/internal_ci/windows/grpc_build_artifacts.bat b/tools/internal_ci/windows/grpc_build_artifacts.bat
index 190e848b26..be47fe670b 100644
--- a/tools/internal_ci/windows/grpc_build_artifacts.bat
+++ b/tools/internal_ci/windows/grpc_build_artifacts.bat
@@ -12,13 +12,6 @@
 @rem See the License for the specific language governing permissions and
 @rem limitations under the License.
 
-@rem Move python installation from _32bit to _32bits where they are expected by python artifact builder
-@rem TODO(jtattermusch): get rid of this hack
-rename C:\Python27_32bit Python27_32bits
-rename C:\Python35_32bit Python35_32bits
-rename C:\Python36_32bit Python36_32bits
-rename C:\Python37_32bit Python37_32bits
-
 @rem Boringssl build no longer supports yasm
 choco uninstall yasm -y --limit-output
 choco install nasm -y --limit-output
diff --git a/tools/internal_ci/windows/grpc_build_packages.bat b/tools/internal_ci/windows/grpc_build_packages.bat
index 3cf90af39e..19a805adfc 100644
--- a/tools/internal_ci/windows/grpc_build_packages.bat
+++ b/tools/internal_ci/windows/grpc_build_packages.bat
@@ -12,12 +12,6 @@
 @rem See the License for the specific language governing permissions and
 @rem limitations under the License.
 
-@rem Move python installation from _32bit to _32bits where they are expected by python artifact builder
-@rem TODO(jtattermusch): get rid of this hack
-rename C:\Python27_32bit Python27_32bits
-rename C:\Python35_32bit Python35_32bits
-rename C:\Python36_32bit Python36_32bits
-
 @rem enter repo root
 cd /d %~dp0\..\..\..
 
diff --git a/tools/internal_ci/windows/grpc_distribtests.bat b/tools/internal_ci/windows/grpc_distribtests.bat
index effeee649b..72d7076ec9 100644
--- a/tools/internal_ci/windows/grpc_distribtests.bat
+++ b/tools/internal_ci/windows/grpc_distribtests.bat
@@ -12,12 +12,6 @@
 @rem See the License for the specific language governing permissions and
 @rem limitations under the License.
 
-@rem Move python installation from _32bit to _32bits where they are expected by python artifact builder
-@rem TODO(jtattermusch): get rid of this hack
-rename C:\Python27_32bit Python27_32bits
-rename C:\Python35_32bit Python35_32bits
-rename C:\Python36_32bit Python36_32bits
-
 @rem enter repo root
 cd /d %~dp0\..\..\..
 
diff --git a/tools/internal_ci/windows/grpc_distribtests_standalone.bat b/tools/internal_ci/windows/grpc_distribtests_standalone.bat
index b33b99c71e..8033467414 100644
--- a/tools/internal_ci/windows/grpc_distribtests_standalone.bat
+++ b/tools/internal_ci/windows/grpc_distribtests_standalone.bat
@@ -12,12 +12,6 @@
 @rem See the License for the specific language governing permissions and
 @rem limitations under the License.
 
-@rem Move python installation from _32bit to _32bits where they are expected by python artifact builder
-@rem TODO(jtattermusch): get rid of this hack
-rename C:\Python27_32bit Python27_32bits
-rename C:\Python35_32bit Python35_32bits
-rename C:\Python36_32bit Python36_32bits
-
 @rem enter repo root
 cd /d %~dp0\..\..\..
 
diff --git a/tools/run_tests/artifacts/artifact_targets.py b/tools/run_tests/artifacts/artifact_targets.py
index 0709db9469..c1288cd091 100644
--- a/tools/run_tests/artifacts/artifact_targets.py
+++ b/tools/run_tests/artifacts/artifact_targets.py
@@ -407,18 +407,16 @@ def targets():
         PythonArtifact('macos', 'x64', 'python3.6'),
         PythonArtifact('macos', 'x64', 'python3.7'),
         PythonArtifact('macos', 'x64', 'python3.8'),
-        PythonArtifact('windows', 'x86', 'Python27_32bits'),
-        PythonArtifact('windows', 'x86', 'Python35_32bits'),
-        PythonArtifact('windows', 'x86', 'Python36_32bits'),
-        PythonArtifact('windows', 'x86', 'Python37_32bits'),
-        # TODO(https://github.com/grpc/grpc/issues/20615) Enable this artifact
-        # PythonArtifact('windows', 'x86', 'Python38_32bits'),
+        PythonArtifact('windows', 'x86', 'Python27_32bit'),
+        PythonArtifact('windows', 'x86', 'Python35_32bit'),
+        PythonArtifact('windows', 'x86', 'Python36_32bit'),
+        PythonArtifact('windows', 'x86', 'Python37_32bit'),
+        PythonArtifact('windows', 'x86', 'Python38_32bit'),
         PythonArtifact('windows', 'x64', 'Python27'),
         PythonArtifact('windows', 'x64', 'Python35'),
         PythonArtifact('windows', 'x64', 'Python36'),
         PythonArtifact('windows', 'x64', 'Python37'),
-        # TODO(https://github.com/grpc/grpc/issues/20615) Enable this artifact
-        # PythonArtifact('windows', 'x64', 'Python38'),
+        PythonArtifact('windows', 'x64', 'Python38'),
         RubyArtifact('linux', 'x64'),
         RubyArtifact('macos', 'x64'),
         PHPArtifact('linux', 'x64')
diff --git a/tools/run_tests/artifacts/build_artifact_python.bat b/tools/run_tests/artifacts/build_artifact_python.bat
index 88a331e278..b1e0ee7d0f 100644
--- a/tools/run_tests/artifacts/build_artifact_python.bat
+++ b/tools/run_tests/artifacts/build_artifact_python.bat
@@ -15,10 +15,10 @@
 @rem set path to python & mingw compiler
 set PATH=C:\%1;C:\%1\scripts;C:\msys64\mingw%2\bin;C:\tools\msys64\mingw%2\bin;%PATH%
 
-pip install --upgrade six
+python -m pip install --upgrade six
 @rem some artifacts are broken for setuptools 38.5.0. See https://github.com/grpc/grpc/issues/14317
-pip install --upgrade setuptools==38.2.4
-pip install -rrequirements.txt
+python -m pip install --upgrade setuptools==38.2.4
+python -m pip install -rrequirements.txt
 
 set GRPC_PYTHON_BUILD_WITH_CYTHON=1
 
-- 
GitLab