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@set _Py_D=%~dp0 10 11@rem First argument -q means only show the command in output 12@if '%1' EQU '-q' (shift && set _Py_Quiet=1) 13 14@rem No arguments provided means do full search 15@if '%1' EQU '' goto :begin_search 16 17@rem One argument may be the full path. Use a goto so we don't try to 18@rem parse the next if statement - incorrect quoting in the multi-arg 19@rem case can cause us to break immediately. 20@if '%2' EQU '' goto :one_arg 21 22@rem Entire command line may represent the full path if quoting failed. 23@if exist "%*" (set PYTHON="%*") & (set _Py_Python_Source=from environment) & goto :found 24@goto :begin_search 25 26:one_arg 27@if exist "%~1" (set PYTHON="%~1") & (set _Py_Python_Source=from environment) & goto :found 28 29:begin_search 30@set PYTHON= 31 32@rem If PYTHON_FOR_BUILD is set, use that 33@if NOT "%PYTHON_FOR_BUILD%"=="" @(set PYTHON="%PYTHON_FOR_BUILD%") && (set _Py_Python_Source=found as PYTHON_FOR_BUILD) && goto :found 34 35@rem If there is an active virtual env, use that one 36@if NOT "%VIRTUAL_ENV%"=="" (set PYTHON="%VIRTUAL_ENV%\Scripts\python.exe") & (set _Py_Python_Source=found in virtual env) & goto :found 37 38@set _Py_EXTERNALS_DIR=%EXTERNALS_DIR% 39@if "%_Py_EXTERNALS_DIR%"=="" (set _Py_EXTERNALS_DIR=%_Py_D%\..\externals) 40 41@rem If we have Python in externals, use that one 42@if exist "%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe" ("%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe" -Ec "import sys; assert sys.version_info[:2] >= (3, 10)" >nul 2>nul) && (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") && (set _Py_Python_Source=found in externals directory) && goto :found || rmdir /Q /S "%_Py_EXTERNALS_DIR%\pythonx86" 43 44@rem If HOST_PYTHON is recent enough, use that 45@if NOT "%HOST_PYTHON%"=="" @%HOST_PYTHON% -Ec "import sys; assert sys.version_info[:2] >= (3, 10)" >nul 2>nul && (set PYTHON="%HOST_PYTHON%") && (set _Py_Python_Source=found as HOST_PYTHON) && goto :found 46 47@rem If py.exe finds a recent enough version, use that one 48@rem It is fine to add new versions to this list when they have released, 49@rem but we do not use prerelease builds here. 50@for %%p in (3.13 3.12 3.11 3.10) do @py -%%p -EV >nul 2>&1 && (set PYTHON=py -%%p) && (set _Py_Python_Source=found %%p with py.exe) && goto :found 51 52@if NOT exist "%_Py_EXTERNALS_DIR%" mkdir "%_Py_EXTERNALS_DIR%" 53@set _Py_NUGET=%NUGET% 54@set _Py_NUGET_URL=%NUGET_URL% 55@set _Py_HOST_PYTHON=%HOST_PYTHON% 56@if "%_Py_HOST_PYTHON%"=="" set _Py_HOST_PYTHON=py 57@if "%_Py_NUGET%"=="" (set _Py_NUGET=%_Py_EXTERNALS_DIR%\nuget.exe) 58@if "%_Py_NUGET_URL%"=="" (set _Py_NUGET_URL=https://aka.ms/nugetclidl) 59@if NOT exist "%_Py_NUGET%" ( 60 @if not "%_Py_Quiet%"=="1" @echo Downloading nuget... 61 @rem NB: Must use single quotes around NUGET here, NOT double! 62 @rem Otherwise, a space in the path would break things 63 @rem If it fails, retry with any available copy of Python 64 @powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%' 65 @if errorlevel 1 ( 66 @%_Py_HOST_PYTHON% -E "%_Py_D%\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%" 67 ) 68) 69 70@if not "%_Py_Quiet%"=="1" @echo Installing Python via nuget... 71@if not "%_Py_Quiet%"=="1" ( 72 @"%_Py_NUGET%" install pythonx86 -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%" 73) else ( 74 @"%_Py_NUGET%" install pythonx86 -Verbosity quiet -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%" 75) 76@rem Quote it here; it's not quoted later because "py -x.y" wouldn't work 77@if not errorlevel 1 (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") & (set _Py_Python_Source=found on nuget.org) & goto :found 78 79 80@set _Py_D= 81@set _Py_Quiet= 82@set _Py_Python_Source= 83@set _Py_EXTERNALS_DIR= 84@set _Py_NUGET= 85@set _Py_NUGET_URL= 86@set _Py_HOST_PYTHON= 87@exit /b 1 88 89:found 90@if "%_Py_Quiet%"=="1" (@echo %PYTHON%) else @echo Using %PYTHON% (%_Py_Python_Source%) 91 92@set _Py_D= 93@set _Py_Quiet= 94@set _Py_Python_Source= 95@set _Py_EXTERNALS_DIR= 96@set _Py_NUGET= 97@set _Py_NUGET_URL= 98@set _Py_HOST_PYTHON= 99