11cb0ef41Sopenharmony_ci@IF NOT DEFINED DEBUG_HELPER @ECHO OFF
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciecho Looking for Python
41cb0ef41Sopenharmony_cisetlocal enabledelayedexpansion
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci:: Use python.exe if in %PATH%
71cb0ef41Sopenharmony_ciset need_path=0
81cb0ef41Sopenharmony_cifor /f "delims=" %%a in ('where python.exe 2^> nul') do (
91cb0ef41Sopenharmony_ci  set p=%%~dpa
101cb0ef41Sopenharmony_ci  goto :found-python
111cb0ef41Sopenharmony_ci)
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci:: Query the 3 locations mentioned in PEP 514 for a Python InstallPath
141cb0ef41Sopenharmony_ciset need_path=1
151cb0ef41Sopenharmony_cifor %%k in ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") do (
161cb0ef41Sopenharmony_ci  call :find-versions %%k
171cb0ef41Sopenharmony_ci  if not errorlevel 1 goto :found-python
181cb0ef41Sopenharmony_ci)
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cigoto :no-python
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci:: Find Python installations in a registry location
241cb0ef41Sopenharmony_ci:find-versions
251cb0ef41Sopenharmony_cifor /f "delims=" %%a in ('reg query "%~1\Python\PythonCore" /f * /k 2^> nul ^| findstr /r ^^HK') do (
261cb0ef41Sopenharmony_ci  call :read-installpath %%a
271cb0ef41Sopenharmony_ci  if not errorlevel 1 exit /b 0
281cb0ef41Sopenharmony_ci)
291cb0ef41Sopenharmony_ciexit /b 1
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci:: Read the InstallPath of a given Environment Key to %p%
321cb0ef41Sopenharmony_ci:: https://www.python.org/dev/peps/pep-0514/#installpath
331cb0ef41Sopenharmony_ci:read-installpath
341cb0ef41Sopenharmony_ci:: %%a will receive everything before ), might have spaces depending on language
351cb0ef41Sopenharmony_ci:: %%b will receive *, corresponding to everything after )
361cb0ef41Sopenharmony_ci:: %%c will receive REG_SZ
371cb0ef41Sopenharmony_ci:: %%d will receive the path, including spaces
381cb0ef41Sopenharmony_cifor /f "skip=2 tokens=1* delims=)" %%a in ('reg query "%1\InstallPath" /ve /t REG_SZ 2^> nul') do (
391cb0ef41Sopenharmony_ci  for /f "tokens=1*" %%c in ("%%b") do (
401cb0ef41Sopenharmony_ci    if not "%%c"=="REG_SZ" exit /b 1
411cb0ef41Sopenharmony_ci    set "p=%%d"
421cb0ef41Sopenharmony_ci    exit /b 0
431cb0ef41Sopenharmony_ci  )
441cb0ef41Sopenharmony_ci)
451cb0ef41Sopenharmony_ciexit /b 1
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci:found-python
481cb0ef41Sopenharmony_ciecho Python found in %p%\python.exe
491cb0ef41Sopenharmony_cicall :check-python "%p%\python.exe"
501cb0ef41Sopenharmony_ciif errorlevel 1 goto :no-python
511cb0ef41Sopenharmony_ciendlocal ^
521cb0ef41Sopenharmony_ci  & set "pt=%p%" ^
531cb0ef41Sopenharmony_ci  & set "need_path_ext=%need_path%"
541cb0ef41Sopenharmony_ciif %need_path_ext%==1 set "PATH=%pt%;%PATH%"
551cb0ef41Sopenharmony_ciset "pt="
561cb0ef41Sopenharmony_ciset "need_path_ext="
571cb0ef41Sopenharmony_ciexit /b 0
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci:check-python
601cb0ef41Sopenharmony_ci%1 -V
611cb0ef41Sopenharmony_ci:: 9009 means error file not found
621cb0ef41Sopenharmony_ciif %errorlevel% equ 9009 (
631cb0ef41Sopenharmony_ci  echo Not an executable Python program
641cb0ef41Sopenharmony_ci  exit /b 1
651cb0ef41Sopenharmony_ci)
661cb0ef41Sopenharmony_ciexit /b 0
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci:no-python
691cb0ef41Sopenharmony_ciecho Could not find Python.
701cb0ef41Sopenharmony_ciexit /b 1
71