@@ -4,7 +4,12 @@ setlocal enableDelayedExpansion
44REM Setup initial vars
55set " script_name = %~n0 .command"
66set " thisDir = %~dp0 "
7+ set /a tried = 0
8+ set " toask = yes"
79
10+ goto checkscript
11+
12+ :checkscript
813REM Check for our script first
914if not exist " !thisDir! \!script_name! " (
1015 echo Could not find !script_name! .
@@ -15,31 +20,162 @@ if not exist "!thisDir!\!script_name!" (
1520 pause > nul
1621 exit /b
1722)
23+ goto checkpy
1824
25+ :checkpy
1926REM Get python location
2027FOR /F " tokens=* USEBACKQ" %%F IN (`where python 2^ > nul `) DO (
2128 SET " python = %%F "
2229)
2330
2431REM Check for py and give helpful hints!
2532if /i " !python! " == " " (
26- echo Python is not installed or not found in your PATH var.
27- echo Please install it from https://www.python.org/downloads/windows/
28- echo .
29- echo Make sure you check the box labeled:
30- echo .
31- echo " Add Python X.X to PATH"
32- echo .
33- echo Where X.X is the py version you're installing.
34- echo .
35- echo Press [enter] to quit.
36- pause > nul
37- exit /b
33+ if %tried% lss 1 (
34+ if /i " !toask! " == " yes" (
35+ REM Better ask permission first
36+ goto askinstall
37+ ) else (
38+ goto installpy
39+ )
40+ ) else (
41+ cls
42+ echo ### ###
43+ echo # Warning #
44+ echo ### ###
45+ REM Couldn't install for whatever reason - give the error message
46+ echo Python is not installed or not found in your PATH var.
47+ echo Please install it from https://www.python.org/downloads/windows/
48+ echo .
49+ echo Make sure you check the box labeled:
50+ echo .
51+ echo " Add Python X.X to PATH"
52+ echo .
53+ echo Where X.X is the py version you're installing.
54+ echo .
55+ echo Press [enter] to quit.
56+ pause > nul
57+ exit /b
58+ )
59+ )
60+ goto runscript
61+
62+ :askinstall
63+ cls
64+ echo ### ###
65+ echo # Python Not Found #
66+ echo ### ###
67+ echo .
68+ echo Python was not found on the system or in the PATH var.
69+ echo .
70+ set /p " menu = Would you like to install it now? [y/n]: "
71+ if /i " !menu! " == " y" (
72+ REM We got the OK - install it
73+ goto installpy
74+ ) else if " !menu! " == " n" (
75+ REM No OK here...
76+ set /a tried = %tried%+ 1
77+ goto checkpy
78+ )
79+ REM Incorrect answer - go back
80+ goto askinstall
81+
82+ :installpy
83+ REM This will attempt to download and install python
84+ REM First we get the html for the python downloads page for Windows
85+ set /a tried = %tried%+ 1
86+ cls
87+ echo ### ###
88+ echo # Installing Python #
89+ echo ### ###
90+ echo .
91+ echo Gathering info from https://www.python.org/downloads/windows/...
92+ powershell -command " [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP% \pyurl.txt')"
93+ if not exist " %TEMP% \pyurl.txt" (
94+ goto checkpy
3895)
3996
97+ echo Parsing for latest...
98+ REM Got the file, let's parse it
99+ pushd " %TEMP% "
100+ set " release = "
101+ for /F " tokens=*" %%x in (pyurl.txt) do (
102+ set " t = %%x "
103+ if /i not " %%x " == " " (
104+ if /i not " !t:Latest Python 3 =! " == " !t! " (
105+ REM echo !t!
106+ set " release = !t! "
107+ )
108+ )
109+ )
110+ popd
111+
112+ REM Let's replace the " with ' and split the string by spaces
113+ REM to get the actual version number
114+ set " release = !release:" =' ! "
115+ for /F " tokens=8* delims= " %%x in (" !release! " ) do (
116+ set " release = %%x "
117+ )
118+ REM Once more - split at the < and get the first
119+ for /F " tokens=1* delims=<" %%x in (" !release! " ) do (
120+ set " release = %%x "
121+ )
122+
123+ echo Found Python !release! - Downloading...
124+ REM Let's delete our txt file now - we no longer need it
125+ del " %TEMP% \pyurl.txt"
126+
127+ REM At this point - we should have the version number.
128+ REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
129+ set " url = https://www.python.org/ftp/python/!release! /python-!release! -amd64.exe"
130+ REM Now we download it with our slick powershell command
131+ powershell -command " [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url! ','%TEMP% \pyinstall.exe')"
132+ REM If it doesn't exist - we bail
133+ if not exist " %TEMP% \pyinstall.exe" (
134+ goto checkpy
135+ )
136+ REM It should exist at this point - let's run it to install silently
137+ echo Installing...
138+ echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
139+ pushd " %TEMP% "
140+ pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
141+ popd
142+ echo Installer finsihed with %ERRORLEVEL% status.
143+ REM Now we should be able to delete the installer and check for py again
144+ del " %TEMP% \pyinstall.exe"
145+ REM If it worked, then we should have python in our PATH
146+ REM this does not get updated right away though - let's try
147+ REM manually updating the local PATH var
148+ set " spath = "
149+ set " upath = "
150+ for /f " tokens=2* delims= " %%i in ('reg.exe query " HKCU\Environment" /v " Path" 2^ > nul ') do (
151+ if NOT " %%j " == " " (
152+ set " upath = %%j "
153+ )
154+ )
155+ for /f " tokens=2* delims= " %%i in ('reg.exe query " HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v " Path" 2^ > nul ') do (
156+ if NOT " %%j " == " " (
157+ set " spath = %%j "
158+ )
159+ )
160+ if not " !spath! " == " " (
161+ REM We got something in the system path
162+ set " PATH = !spath! "
163+ if not " !upath! " == " " (
164+ REM We also have something in the user path
165+ set " PATH = !PATH! ;!upath! "
166+ )
167+ ) else if not " !upath! " == " " (
168+ set " PATH = !upath! "
169+ )
170+ goto checkpy
171+ exit /b
172+
173+ :runscript
40174REM Python found
175+ cls
41176if " %* " == " " (
42177 " !python! " " !thisDir!!script_name! "
43178) else (
44179 " !python! " " !thisDir!!script_name! " %*
45- )
180+ )
181+ goto :EOF
0 commit comments