Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 1 | @rem |
| 2 | @rem Searches for python.exe and may download a private copy from nuget. |
| 3 | @rem |
| 4 | @rem This file is supposed to modify the state of the caller (specifically |
| 5 | @rem the MSBUILD variable), so we do not use setlocal or echo, and avoid |
| 6 | @rem changing any other persistent state. |
| 7 | @rem |
| 8 | |
| 9 | @rem No arguments provided means do full search |
| 10 | @if '%1' EQU '' goto :begin_search |
| 11 | |
| 12 | @rem One argument may be the full path. Use a goto so we don't try to |
| 13 | @rem parse the next if statement - incorrect quoting in the multi-arg |
| 14 | @rem case can cause us to break immediately. |
| 15 | @if '%2' EQU '' goto :one_arg |
| 16 | |
| 17 | @rem Entire command line may represent the full path if quoting failed. |
| 18 | @if exist "%*" (set PYTHON="%*") & (set _Py_Python_Source=from environment) & goto :found |
| 19 | @goto :begin_search |
| 20 | |
| 21 | :one_arg |
| 22 | @if exist "%~1" (set PYTHON="%~1") & (set _Py_Python_Source=from environment) & goto :found |
| 23 | |
| 24 | :begin_search |
| 25 | @set PYTHON= |
| 26 | |
antektek | 6aedfa6 | 2019-01-10 01:19:29 +0100 | [diff] [blame] | 27 | @set _Py_EXTERNALS_DIR=%EXTERNALS_DIR% |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 28 | @if "%_Py_EXTERNALS_DIR%"=="" (set _Py_EXTERNALS_DIR=%~dp0\..\externals) |
| 29 | |
| 30 | @rem If we have Python in externals, use that one |
| 31 | @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 |
| 32 | |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 33 | @rem If HOST_PYTHON is recent enough, use that |
Steve Dower | 01423cb | 2018-02-17 18:59:03 -0800 | [diff] [blame] | 34 | @if NOT "%HOST_PYTHON%"=="" @%HOST_PYTHON% -Ec "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 |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 35 | |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 36 | @rem If py.exe finds a recent enough version, use that one |
Steve Dower | 6750922 | 2018-09-17 14:41:53 -0700 | [diff] [blame] | 37 | @for %%p in (3.7 3.6) do @py -%%p -EV >nul 2>&1 && (set PYTHON=py -%%p) && (set _Py_Python_Source=found %%p with py.exe) && goto :found |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 38 | |
| 39 | @if NOT exist "%_Py_EXTERNALS_DIR%" mkdir "%_Py_EXTERNALS_DIR%" |
| 40 | @set _Py_NUGET=%NUGET% |
| 41 | @set _Py_NUGET_URL=%NUGET_URL% |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 42 | @set _Py_HOST_PYTHON=%HOST_PYTHON% |
| 43 | @if "%_Py_HOST_PYTHON%"=="" set _Py_HOST_PYTHON=py |
| 44 | @if "%_Py_NUGET%"=="" (set _Py_NUGET=%_Py_EXTERNALS_DIR%\nuget.exe) |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 45 | @if "%_Py_NUGET_URL%"=="" (set _Py_NUGET_URL=https://aka.ms/nugetclidl) |
| 46 | @if NOT exist "%_Py_NUGET%" ( |
| 47 | @echo Downloading nuget... |
| 48 | @rem NB: Must use single quotes around NUGET here, NOT double! |
| 49 | @rem Otherwise, a space in the path would break things |
Steve Dower | 588836d | 2017-07-17 14:55:28 +0200 | [diff] [blame] | 50 | @rem If it fails, retry with any available copy of Python |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 51 | @powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%' |
| 52 | @if errorlevel 1 ( |
Steve Dower | 01423cb | 2018-02-17 18:59:03 -0800 | [diff] [blame] | 53 | @%_Py_HOST_PYTHON% -E "%~dp0\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%" |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 54 | ) |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 55 | ) |
| 56 | @echo Installing Python via nuget... |
| 57 | @"%_Py_NUGET%" install pythonx86 -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%" |
Steve Dower | 6750922 | 2018-09-17 14:41:53 -0700 | [diff] [blame] | 58 | @rem Quote it here; it's not quoted later because "py -x.y" wouldn't work |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 59 | @if not errorlevel 1 (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") & (set _Py_Python_Source=found on nuget.org) & goto :found |
| 60 | |
| 61 | |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 62 | @set _Py_Python_Source= |
| 63 | @set _Py_EXTERNALS_DIR= |
| 64 | @set _Py_NUGET= |
| 65 | @set _Py_NUGET_URL= |
| 66 | @set _Py_HOST_PYTHON= |
Steve Dower | 68d663c | 2017-07-17 11:15:48 +0200 | [diff] [blame] | 67 | @exit /b 1 |
| 68 | |
| 69 | :found |
| 70 | @echo Using %PYTHON% (%_Py_Python_Source%) |
| 71 | @set _Py_Python_Source= |
Steve Dower | 5feda33 | 2017-07-18 19:31:51 +0200 | [diff] [blame] | 72 | @set _Py_EXTERNALS_DIR= |
| 73 | @set _Py_NUGET= |
| 74 | @set _Py_NUGET_URL= |
| 75 | @set _Py_HOST_PYTHON= |