1
1
@ IF NOT DEFINED DEBUG_HELPER @ ECHO OFF
2
2
3
- echo Looking for Python 2.x
3
+ echo Looking for Python
4
4
setlocal enabledelayedexpansion
5
5
6
- :: If python.exe is in %Path%, just validate
6
+ :: To remove the preference for Python 2, but still support it, just remove
7
+ :: the 5 blocks marked with "Python 2:" and the support warnings
8
+
9
+ :: Python 2: If python.exe is in %Path%, use if it's Python 2
7
10
FOR /F " delims=" %%a IN ('where python.exe 2^ > NUL ') DO (
8
11
SET need_path = 0
9
12
SET p = %%~dpa
10
- IF NOT ERRORLEVEL 1 GOTO :validate
13
+ CALL :validate-v2
14
+ IF NOT ERRORLEVEL 1 GOTO :found-python2
15
+ GOTO :done-path-v2
11
16
)
17
+ :done-path-v2
12
18
13
- :: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
19
+ :: Python 2: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
14
20
FOR %%K IN ( " HKCU\Software" , " HKLM\SOFTWARE" , " HKLM\Software\Wow6432Node" ) DO (
15
21
SET need_path = 1
16
22
CALL :find-versions-v2 %%K
17
- :: If validate returns 0 just jump to the end
18
- IF NOT ERRORLEVEL 1 GOTO :validate
23
+ IF NOT ERRORLEVEL 1 CALL :validate-v2
24
+ IF NOT ERRORLEVEL 1 GOTO :found-python2
25
+ )
26
+
27
+ :: Use python.exe if in %PATH%
28
+ set need_path = 0
29
+ for /f " delims=" %%a in ('where python.exe 2^ > nul ') do (
30
+ set p = %%~dpa
31
+ goto :found-python
32
+ )
33
+
34
+ :: Query the 3 locations mentioned in PEP 514 for a Python InstallPath
35
+ set need_path = 1
36
+ for %%k in ( " HKCU\Software" , " HKLM\SOFTWARE" , " HKLM\Software\Wow6432Node" ) do (
37
+ call :find-versions %%k
38
+ if not errorlevel 1 goto :found-python
19
39
)
20
40
21
41
goto :no-python
22
42
23
43
24
- :: Find Python 2 installations in a registry location
44
+ :: Python 2: Find Python 2 installations in a registry location
25
45
:find-versions-v2
26
46
for /f " delims=" %%a in ('reg query " %~1 \Python\PythonCore" /f * /k 2^ > nul ^ | findstr /r ^^ HK ^ | findstr " \\2\." ') do (
27
47
call :read-installpath %%a
28
48
if not errorlevel 1 exit /b 0
29
49
)
30
50
exit /b 1
31
51
52
+ :: Find Python installations in a registry location
53
+ :find-versions
54
+ for /f " delims=" %%a in ('reg query " %~1 \Python\PythonCore" /f * /k 2^ > nul ^ | findstr /r ^^ HK') do (
55
+ call :read-installpath %%a
56
+ if not errorlevel 1 exit /b 0
57
+ )
58
+ exit /b 1
59
+
32
60
:: Read the InstallPath of a given Environment Key to %p%
33
61
:: https://www.python.org/dev/peps/pep-0514/#installpath
34
62
:read-installpath
@@ -44,22 +72,37 @@ for /f "skip=2 tokens=3*" %%a in ('reg query "%1\InstallPath" /ve /t REG_SZ 2^>
44
72
exit /b 1
45
73
46
74
47
- :: Check if %p% holds a path to a real python2 executable
48
- :validate
49
- IF NOT EXIST " %p% python.exe" goto : no-python
75
+ :: Python 2: Check if %p% holds a path to a real python2 executable
76
+ :validate-v2
77
+ IF NOT EXIST " %p% \ python.exe" EXIT /B 1
50
78
:: Check if %p% is python2
51
- " %p% python.exe" -V 2 >& 1 | findstr /R " ^Python.2.*" > NUL
52
- IF ERRORLEVEL 1 goto :no-python2
53
- :: We can wrap it up
54
- ENDLOCAL & SET pt = %p% & SET need_path_ext = %need_path%
55
- SET VCBUILD_PYTHON_LOCATION = %pt% python.exe
56
- IF %need_path_ext% == 1 SET Path = %Path% ;%pt%
57
- SET need_path_ext =
79
+ " %p% \python.exe" -V 2 >& 1 | findstr /R " ^Python.2.*" > NUL
58
80
EXIT /B %ERRORLEVEL%
59
81
60
- :no-python2
61
- echo Python found in %p% , but it is not v2.x.
62
- exit /B 1
82
+
83
+ :: Python 2:
84
+ :found-python2
85
+ echo Python 2 found in %p% \python.exe
86
+ set pyver = 2
87
+ goto :done
88
+
89
+ :found-python
90
+ echo Python found in %p% \python.exe
91
+ echo WARNING: Python 3 is not yet fully supported, to avoid issues Python 2 should be installed.
92
+ set pyver = 3
93
+ goto :done
94
+
95
+ :done
96
+ endlocal ^
97
+ & set " pt = %p% " ^
98
+ & set " need_path_ext = %need_path% " ^
99
+ & set " VCBUILD_PYTHON_VERSION = %pyver% "
100
+ set " VCBUILD_PYTHON_LOCATION = %pt% \python.exe"
101
+ if %need_path_ext% == 1 set " PATH = %pt% ;%PATH% "
102
+ set " pt = "
103
+ set " need_path_ext = "
104
+ exit /b 0
105
+
63
106
:no-python
64
107
echo Could not find Python.
65
- exit /B 1
108
+ exit /b 1
0 commit comments