From: Steve Dower <steve.dower@microsoft.com>
Date: Tue, 18 Jul 2017 17:31:51 +0000 (+0200)
Subject: bpo-30450: Fix logic for retrying nuget.exe download (#2744)
X-Git-Tag: v3.7.0a1~406
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5feda33a35d9413e2073411b848dc49d94c57497;p=python

bpo-30450: Fix logic for retrying nuget.exe download (#2744)

Fix logic for retrying nuget.exe download with Python.
Add support for HOST_PYTHON variable.
Clear internal environment variables used in find_python.bat
Use HOST_PYTHON as the actual Python if it is recent enough.
Adds HOST_PYTHON variable to AppVeyor configuration
---

diff --git a/.github/appveyor.yml b/.github/appveyor.yml
index 06b9b2f87b..deb0e309a9 100644
--- a/.github/appveyor.yml
+++ b/.github/appveyor.yml
@@ -9,6 +9,8 @@ build_script:
 - cmd: PCbuild\build.bat -e
 test_script:
 - cmd: PCbuild\rt.bat -q -uall -u-cpu -rwW --slowest --timeout=1200 --fail-env-changed -j0
+environment:
+  HOST_PYTHON: C:\Python36\python.exe
 
 # Only trigger AppVeyor if actual code or its configuration changes
 only_commits:
diff --git a/PCbuild/find_python.bat b/PCbuild/find_python.bat
index 82358c864e..ba3a0f5a10 100644
--- a/PCbuild/find_python.bat
+++ b/PCbuild/find_python.bat
@@ -30,20 +30,28 @@
 @rem If we have Python in externals, use that one
 @if exist "%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe" (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") & (set _Py_Python_Source=found in externals directory) & goto :found
 
+@rem If HOST_PYTHON is recent enough, use that
+@if NOT "%HOST_PYTHON%"=="" @%HOST_PYTHON% -c "import sys; assert sys.version_info[:2] >= (3, 6)" >nul 2>nul && (set PYTHON="%HOST_PYTHON%") && (set _Py_Python_Source=found as HOST_PYTHON) && goto :found
+
 @rem If py.exe finds a recent enough version, use that one
 @py -3.6 -V >nul 2>&1 && (set PYTHON=py -3.6) && (set _Py_Python_Source=found with py.exe) && goto :found
 
 @if NOT exist "%_Py_EXTERNALS_DIR%" mkdir "%_Py_EXTERNALS_DIR%"
 @set _Py_NUGET=%NUGET%
 @set _Py_NUGET_URL=%NUGET_URL%
-@if "%_Py_NUGET%"=="" (set _Py_NUGET=%EXTERNALS_DIR%\nuget.exe)
+@set _Py_HOST_PYTHON=%HOST_PYTHON%
+@if "%_Py_HOST_PYTHON%"=="" set _Py_HOST_PYTHON=py
+@if "%_Py_NUGET%"=="" (set _Py_NUGET=%_Py_EXTERNALS_DIR%\nuget.exe)
 @if "%_Py_NUGET_URL%"=="" (set _Py_NUGET_URL=https://aka.ms/nugetclidl)
 @if NOT exist "%_Py_NUGET%" (
     @echo Downloading nuget...
     @rem NB: Must use single quotes around NUGET here, NOT double!
     @rem Otherwise, a space in the path would break things
     @rem If it fails, retry with any available copy of Python
-    @powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%' || @py -c "%~dp0\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%"
+    @powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%'
+    @if errorlevel 1 (
+        @%_Py_HOST_PYTHON% "%~dp0\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%"
+    )
 )
 @echo Installing Python via nuget...
 @"%_Py_NUGET%" install pythonx86 -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%"
@@ -51,8 +59,17 @@
 @if not errorlevel 1 (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") & (set _Py_Python_Source=found on nuget.org) & goto :found
 
 
+@set _Py_Python_Source=
+@set _Py_EXTERNALS_DIR=
+@set _Py_NUGET=
+@set _Py_NUGET_URL=
+@set _Py_HOST_PYTHON=
 @exit /b 1
 
 :found
 @echo Using %PYTHON% (%_Py_Python_Source%)
 @set _Py_Python_Source=
+@set _Py_EXTERNALS_DIR=
+@set _Py_NUGET=
+@set _Py_NUGET_URL=
+@set _Py_HOST_PYTHON=