1
1
@ IF NOT DEFINED DEBUG_HELPER @ ECHO OFF
2
+
2
3
echo Looking for Python 2.x
3
- SETLOCAL
4
+ setlocal enabledelayedexpansion
5
+
4
6
:: If python.exe is in %Path%, just validate
5
7
FOR /F " delims=" %%a IN ('where python.exe 2^ > NUL ') DO (
6
8
SET need_path = 0
@@ -11,32 +13,36 @@ FOR /F "delims=" %%a IN ('where python.exe 2^> NUL') DO (
11
13
:: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
12
14
FOR %%K IN ( " HKCU\Software" , " HKLM\SOFTWARE" , " HKLM\Software\Wow6432Node" ) DO (
13
15
SET need_path = 1
14
- CALL :find-main-branch %%K
16
+ CALL :find-versions-v2 %%K
15
17
:: If validate returns 0 just jump to the end
16
18
IF NOT ERRORLEVEL 1 GOTO :validate
17
19
)
20
+
18
21
goto :no-python
19
22
20
- :: Helper subroutine to handle quotes in %1
21
- :find-main-branch
22
- SET main_key = " %~1 \Python\PythonCore"
23
- REG QUERY %main_key% /s 2 > NUL | findstr " 2." | findstr InstallPath > NUL 2 > NUL
24
- IF NOT ERRORLEVEL 1 CALL :find-key %main_key%
25
- EXIT /B
26
-
27
- :: Query registry sub-tree for InstallPath
28
- :find-key
29
- FOR /F " delims=" %%a IN ('REG QUERY %1 /s 2^ > NUL ^ | findstr " 2." ^ | findstr InstallPath') DO IF NOT ERRORLEVEL 1 CALL :find-path %%a
30
- EXIT /B
31
-
32
- :: Parse the value of %1 as the path for python.exe
33
- :find-path
34
- FOR /F " tokens=3*" %%a IN ('REG QUERY %1 /ve') DO (
35
- SET pt = %%a
36
- IF NOT ERRORLEVEL 1 SET p = %pt%
37
- EXIT /B 0
23
+
24
+ :: Find Python 2 installations in a registry location
25
+ :find-versions-v2
26
+ for /f " delims=" %%a in ('reg query " %~1 \Python\PythonCore" /f * /k 2^ > nul ^ | findstr /r ^^ HK ^ | findstr " \\2\." ') do (
27
+ call :read-installpath %%a
28
+ if not errorlevel 1 exit /b 0
38
29
)
39
- EXIT /B 1
30
+ exit /b 1
31
+
32
+ :: Read the InstallPath of a given Environment Key to %p%
33
+ :: https://www.python.org/dev/peps/pep-0514/#installpath
34
+ :read-installpath
35
+ :: %%a will receive token 3
36
+ :: %%b will receive *, corresponding to token 4 and all after
37
+ for /f " skip=2 tokens=3*" %%a in ('reg query " %1 \InstallPath" /ve /t REG_SZ 2^ > nul ') do (
38
+ set " head = %%a "
39
+ set " tail = %%b "
40
+ set " p = !head! "
41
+ if not " !tail! " == " " set " p = !head! !tail! "
42
+ exit /b 0
43
+ )
44
+ exit /b 1
45
+
40
46
41
47
:: Check if %p% holds a path to a real python2 executable
42
48
:validate
0 commit comments