17db96d56Sopenharmony_ci@rem 27db96d56Sopenharmony_ci@rem Searches for python.exe and may download a private copy from nuget. 37db96d56Sopenharmony_ci@rem 47db96d56Sopenharmony_ci@rem This file is supposed to modify the state of the caller (specifically 57db96d56Sopenharmony_ci@rem the MSBUILD variable), so we do not use setlocal or echo, and avoid 67db96d56Sopenharmony_ci@rem changing any other persistent state. 77db96d56Sopenharmony_ci@rem 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci@set _Py_D=%~dp0 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci@rem First argument -q means only show the command in output 127db96d56Sopenharmony_ci@if '%1' EQU '-q' (shift && set _Py_Quiet=1) 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci@rem No arguments provided means do full search 157db96d56Sopenharmony_ci@if '%1' EQU '' goto :begin_search 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci@rem One argument may be the full path. Use a goto so we don't try to 187db96d56Sopenharmony_ci@rem parse the next if statement - incorrect quoting in the multi-arg 197db96d56Sopenharmony_ci@rem case can cause us to break immediately. 207db96d56Sopenharmony_ci@if '%2' EQU '' goto :one_arg 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci@rem Entire command line may represent the full path if quoting failed. 237db96d56Sopenharmony_ci@if exist "%*" (set PYTHON="%*") & (set _Py_Python_Source=from environment) & goto :found 247db96d56Sopenharmony_ci@goto :begin_search 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci:one_arg 277db96d56Sopenharmony_ci@if exist "%~1" (set PYTHON="%~1") & (set _Py_Python_Source=from environment) & goto :found 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci:begin_search 307db96d56Sopenharmony_ci@set PYTHON= 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci@rem If there is an active virtual env, use that one 337db96d56Sopenharmony_ci@if NOT "%VIRTUAL_ENV%"=="" (set PYTHON="%VIRTUAL_ENV%\Scripts\python.exe") & (set _Py_Python_Source=found in virtual env) & goto :found 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci@set _Py_EXTERNALS_DIR=%EXTERNALS_DIR% 367db96d56Sopenharmony_ci@if "%_Py_EXTERNALS_DIR%"=="" (set _Py_EXTERNALS_DIR=%_Py_D%\..\externals) 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci@rem If we have Python in externals, use that one 397db96d56Sopenharmony_ci@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, 8)" >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" 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci@rem If HOST_PYTHON is recent enough, use that 427db96d56Sopenharmony_ci@if NOT "%HOST_PYTHON%"=="" @%HOST_PYTHON% -Ec "import sys; assert sys.version_info[:2] >= (3, 9)" >nul 2>nul && (set PYTHON="%HOST_PYTHON%") && (set _Py_Python_Source=found as HOST_PYTHON) && goto :found 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci@rem If py.exe finds a recent enough version, use that one 457db96d56Sopenharmony_ci@for %%p in (3.10 3.9) do @py -%%p -EV >nul 2>&1 && (set PYTHON=py -%%p) && (set _Py_Python_Source=found %%p with py.exe) && goto :found 467db96d56Sopenharmony_ci 477db96d56Sopenharmony_ci@if NOT exist "%_Py_EXTERNALS_DIR%" mkdir "%_Py_EXTERNALS_DIR%" 487db96d56Sopenharmony_ci@set _Py_NUGET=%NUGET% 497db96d56Sopenharmony_ci@set _Py_NUGET_URL=%NUGET_URL% 507db96d56Sopenharmony_ci@set _Py_HOST_PYTHON=%HOST_PYTHON% 517db96d56Sopenharmony_ci@if "%_Py_HOST_PYTHON%"=="" set _Py_HOST_PYTHON=py 527db96d56Sopenharmony_ci@if "%_Py_NUGET%"=="" (set _Py_NUGET=%_Py_EXTERNALS_DIR%\nuget.exe) 537db96d56Sopenharmony_ci@if "%_Py_NUGET_URL%"=="" (set _Py_NUGET_URL=https://aka.ms/nugetclidl) 547db96d56Sopenharmony_ci@if NOT exist "%_Py_NUGET%" ( 557db96d56Sopenharmony_ci @echo Downloading nuget... 567db96d56Sopenharmony_ci @rem NB: Must use single quotes around NUGET here, NOT double! 577db96d56Sopenharmony_ci @rem Otherwise, a space in the path would break things 587db96d56Sopenharmony_ci @rem If it fails, retry with any available copy of Python 597db96d56Sopenharmony_ci @powershell.exe -Command Invoke-WebRequest %_Py_NUGET_URL% -OutFile '%_Py_NUGET%' 607db96d56Sopenharmony_ci @if errorlevel 1 ( 617db96d56Sopenharmony_ci @%_Py_HOST_PYTHON% -E "%_Py_D%\urlretrieve.py" "%_Py_NUGET_URL%" "%_Py_NUGET%" 627db96d56Sopenharmony_ci ) 637db96d56Sopenharmony_ci) 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci@if not "%_Py_Quiet%"=="1" @echo Installing Python via nuget... 667db96d56Sopenharmony_ci@"%_Py_NUGET%" install pythonx86 -ExcludeVersion -OutputDirectory "%_Py_EXTERNALS_DIR%" 677db96d56Sopenharmony_ci@rem Quote it here; it's not quoted later because "py -x.y" wouldn't work 687db96d56Sopenharmony_ci@if not errorlevel 1 (set PYTHON="%_Py_EXTERNALS_DIR%\pythonx86\tools\python.exe") & (set _Py_Python_Source=found on nuget.org) & goto :found 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci@set _Py_D= 727db96d56Sopenharmony_ci@set _Py_Quiet= 737db96d56Sopenharmony_ci@set _Py_Python_Source= 747db96d56Sopenharmony_ci@set _Py_EXTERNALS_DIR= 757db96d56Sopenharmony_ci@set _Py_NUGET= 767db96d56Sopenharmony_ci@set _Py_NUGET_URL= 777db96d56Sopenharmony_ci@set _Py_HOST_PYTHON= 787db96d56Sopenharmony_ci@exit /b 1 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci:found 817db96d56Sopenharmony_ci@if "%_Py_Quiet%"=="1" (@echo %PYTHON%) else @echo Using %PYTHON% (%_Py_Python_Source%) 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci@set _Py_D= 847db96d56Sopenharmony_ci@set _Py_Quiet= 857db96d56Sopenharmony_ci@set _Py_Python_Source= 867db96d56Sopenharmony_ci@set _Py_EXTERNALS_DIR= 877db96d56Sopenharmony_ci@set _Py_NUGET= 887db96d56Sopenharmony_ci@set _Py_NUGET_URL= 897db96d56Sopenharmony_ci@set _Py_HOST_PYTHON= 90