|
| 1 | +@echo off |
| 2 | +setlocal enableDelayedExpansion |
| 3 | + |
| 4 | +REM Setup initial vars |
| 5 | +set "script_name=%~n0.command" |
| 6 | +set "thisDir=%~dp0" |
| 7 | +set /a tried=0 |
| 8 | +set "toask=yes" |
| 9 | +set "py2v=" |
| 10 | +set "py2path=" |
| 11 | +set "py3v=" |
| 12 | +set "py3path=" |
| 13 | +set "pypath=" |
| 14 | + |
| 15 | +REM use_py3: |
| 16 | +REM TRUE = Use if found, use py2 otherwise |
| 17 | +REM FALSE = Use py2 |
| 18 | +REM FORCE = Use py3 |
| 19 | +set "use_py3=TRUE" |
| 20 | + |
| 21 | +goto checkscript |
| 22 | + |
| 23 | +:checkscript |
| 24 | +REM Check for our script first |
| 25 | +if not exist "!thisDir!\!script_name!" ( |
| 26 | + echo Could not find !script_name!. |
| 27 | + echo Please make sure to run this script from the same directory |
| 28 | + echo as !script_name!. |
| 29 | + echo. |
| 30 | + echo Press [enter] to quit. |
| 31 | + pause > nul |
| 32 | + exit /b |
| 33 | +) |
| 34 | +goto checkpy |
| 35 | + |
| 36 | +:updatepath |
| 37 | +set "spath=" |
| 38 | +set "upath=" |
| 39 | +for /f "tokens=2* delims= " %%i in ('reg.exe query "HKCU\Environment" /v "Path" 2^> nul') do ( if not "%%j" == "" set "upath=%%j" ) |
| 40 | +for /f "tokens=2* delims= " %%i in ('reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul') do ( if not "%%j" == "" set "spath=%%j" ) |
| 41 | +if not "!spath!" == "" ( |
| 42 | + REM We got something in the system path |
| 43 | + set "PATH=!spath!" |
| 44 | + if not "!upath!" == "" ( |
| 45 | + REM We also have something in the user path |
| 46 | + set "PATH=!PATH!;!upath!" |
| 47 | + ) |
| 48 | +) else if not "!upath!" == "" ( |
| 49 | + set "PATH=!upath!" |
| 50 | +) |
| 51 | +goto :EOF |
| 52 | + |
| 53 | +:checkpy |
| 54 | +call :updatepath |
| 55 | +REM Get the system32 (or equivalent) path |
| 56 | +set syspath=%ComSpec:cmd.exe=% |
| 57 | +for /f "tokens=*" %%x in ('!syspath!where python') do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" ) |
| 58 | +set "targetpy=3" |
| 59 | +if /i "!use_py3!" == "FALSE" ( |
| 60 | + set "targetpy=2" |
| 61 | + set "pypath=!py2path!" |
| 62 | +) else if /i "!use_py3!" == "FORCE" ( |
| 63 | + set "pypath=!py3path!" |
| 64 | +) else if /i "!use_py3!" == "TRUE" ( |
| 65 | + set "pypath=!py3path!" |
| 66 | + if "!pypath!" == "" set "pypath=!py2path!" |
| 67 | +) |
| 68 | +if not "!pypath!" == "" ( |
| 69 | + goto runscript |
| 70 | +) |
| 71 | + |
| 72 | +if "!pypath!" == "" ( |
| 73 | + if %tried% lss 1 ( |
| 74 | + if /i "!toask!"=="yes" ( |
| 75 | + REM Better ask permission first |
| 76 | + goto askinstall |
| 77 | + ) else ( |
| 78 | + goto installpy |
| 79 | + ) |
| 80 | + ) else ( |
| 81 | + cls |
| 82 | + echo ### ### |
| 83 | + echo # Warning # |
| 84 | + echo ### ### |
| 85 | + echo. |
| 86 | + REM Couldn't install for whatever reason - give the error message |
| 87 | + echo Python is not installed or not found in your PATH var. |
| 88 | + echo Please install it from https://www.python.org/downloads/windows/ |
| 89 | + echo. |
| 90 | + echo Make sure you check the box labeled: |
| 91 | + echo. |
| 92 | + echo "Add Python X.X to PATH" |
| 93 | + echo. |
| 94 | + echo Where X.X is the py version you're installing. |
| 95 | + echo. |
| 96 | + echo Press [enter] to quit. |
| 97 | + pause > nul |
| 98 | + exit /b |
| 99 | + ) |
| 100 | +) |
| 101 | +goto runscript |
| 102 | + |
| 103 | +:checkpyversion <path> <py2v> <py2path> <py3v> <py3path> |
| 104 | +set "version="&for /f "tokens=2* USEBACKQ delims= " %%a in (`"%~1" -V 2^>^&1`) do ( |
| 105 | + REM Ensure we have a version number |
| 106 | + call :isnumber "%%a" |
| 107 | + if not "!errorlevel!" == "0" goto :EOF |
| 108 | + set "version=%%a" |
| 109 | +) |
| 110 | +if not defined version goto :EOF |
| 111 | +if "!version:~0,1!" == "2" ( |
| 112 | + REM Python 2 |
| 113 | + call :comparepyversion "!version!" "!%~2!" |
| 114 | + if "!errorlevel!" == "1" ( |
| 115 | + set "%~2=!version!" |
| 116 | + set "%~3=%~1" |
| 117 | + ) |
| 118 | +) else ( |
| 119 | + REM Python 3 |
| 120 | + call :comparepyversion "!version!" "!%~4!" |
| 121 | + if "!errorlevel!" == "1" ( |
| 122 | + set "%~4=!version!" |
| 123 | + set "%~5=%~1" |
| 124 | + ) |
| 125 | +) |
| 126 | +goto :EOF |
| 127 | + |
| 128 | +:isnumber <check> |
| 129 | +set "var="&for /f "delims=0123456789." %%i in ("%~1") do set var=%%i |
| 130 | +if defined var (exit /b 1) |
| 131 | +exit /b 0 |
| 132 | + |
| 133 | +:comparepyversion <version1> <version2> <return> |
| 134 | +REM Exits with status 0 if equal, 1 if v1 gtr v2, 2 if v1 lss v2 |
| 135 | +for /f "tokens=1,2,3 delims=." %%a in ("%~1") do ( |
| 136 | + set a1=%%a |
| 137 | + set a2=%%b |
| 138 | + set a3=%%c |
| 139 | +) |
| 140 | +for /f "tokens=1,2,3 delims=." %%a in ("%~2") do ( |
| 141 | + set b1=%%a |
| 142 | + set b2=%%b |
| 143 | + set b3=%%c |
| 144 | +) |
| 145 | +if not defined a1 set a1=0 |
| 146 | +if not defined a2 set a2=0 |
| 147 | +if not defined a3 set a3=0 |
| 148 | +if not defined b1 set b1=0 |
| 149 | +if not defined b2 set b2=0 |
| 150 | +if not defined b3 set b3=0 |
| 151 | +if %a1% gtr %b1% exit /b 1 |
| 152 | +if %a1% lss %b1% exit /b 2 |
| 153 | +if %a2% gtr %b2% exit /b 1 |
| 154 | +if %a2% lss %b2% exit /b 2 |
| 155 | +if %a3% gtr %b3% exit /b 1 |
| 156 | +if %a3% lss %b3% exit /b 2 |
| 157 | +exit /b 0 |
| 158 | + |
| 159 | +:askinstall |
| 160 | +cls |
| 161 | +echo ### ### |
| 162 | +echo # Python Not Found # |
| 163 | +echo ### ### |
| 164 | +echo. |
| 165 | +echo Python !targetpy! was not found on the system or in the PATH var. |
| 166 | +echo. |
| 167 | +set /p "menu=Would you like to install it now? [y/n]: " |
| 168 | +if /i "!menu!"=="y" ( |
| 169 | + REM We got the OK - install it |
| 170 | + goto installpy |
| 171 | +) else if "!menu!"=="n" ( |
| 172 | + REM No OK here... |
| 173 | + set /a tried=%tried%+1 |
| 174 | + goto checkpy |
| 175 | +) |
| 176 | +REM Incorrect answer - go back |
| 177 | +goto askinstall |
| 178 | + |
| 179 | +:installpy |
| 180 | +REM This will attempt to download and install python |
| 181 | +REM First we get the html for the python downloads page for Windows |
| 182 | +set /a tried=%tried%+1 |
| 183 | +cls |
| 184 | +echo ### ### |
| 185 | +echo # Installing Python # |
| 186 | +echo ### ### |
| 187 | +echo. |
| 188 | +echo Gathering info from https://www.python.org/downloads/windows/... |
| 189 | +powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')" |
| 190 | +if not exist "%TEMP%\pyurl.txt" ( |
| 191 | + goto checkpy |
| 192 | +) |
| 193 | + |
| 194 | +echo Parsing for latest... |
| 195 | +pushd "%TEMP%" |
| 196 | +:: Version detection code slimmed by LussacZheng (https://github.com/corpnewt/gibMacOS/issues/20) |
| 197 | +for /f "tokens=9 delims=< " %%x in ('findstr /i /c:"Latest Python !targetpy! Release" pyurl.txt') do ( set "release=%%x" ) |
| 198 | +popd |
| 199 | + |
| 200 | +echo Found Python !release! - Downloading... |
| 201 | +REM Let's delete our txt file now - we no longer need it |
| 202 | +del "%TEMP%\pyurl.txt" |
| 203 | + |
| 204 | +REM At this point - we should have the version number. |
| 205 | +REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe" |
| 206 | +set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe" |
| 207 | +set "pytype=exe" |
| 208 | +if "!targetpy!" == "2" ( |
| 209 | + set "url=https://www.python.org/ftp/python/!release!/python-!release!.amd64.msi" |
| 210 | + set "pytype=msi" |
| 211 | +) |
| 212 | +REM Now we download it with our slick powershell command |
| 213 | +powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.!pytype!')" |
| 214 | +REM If it doesn't exist - we bail |
| 215 | +if not exist "%TEMP%\pyinstall.!pytype!" ( |
| 216 | + goto checkpy |
| 217 | +) |
| 218 | +REM It should exist at this point - let's run it to install silently |
| 219 | +echo Installing... |
| 220 | +pushd "%TEMP%" |
| 221 | +if /i "!pytype!" == "exe" ( |
| 222 | + echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 |
| 223 | + pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 |
| 224 | +) else ( |
| 225 | + set "foldername=!release:.=!" |
| 226 | + echo msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" |
| 227 | + msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" |
| 228 | +) |
| 229 | +popd |
| 230 | +echo Installer finished with %ERRORLEVEL% status. |
| 231 | +REM Now we should be able to delete the installer and check for py again |
| 232 | +del "%TEMP%\pyinstall.!pytype!" |
| 233 | +REM If it worked, then we should have python in our PATH |
| 234 | +REM this does not get updated right away though - let's try |
| 235 | +REM manually updating the local PATH var |
| 236 | +call :updatepath |
| 237 | +goto checkpy |
| 238 | +exit /b |
| 239 | + |
| 240 | +:runscript |
| 241 | +REM Python found |
| 242 | +cls |
| 243 | +set "args=%*" |
| 244 | +set "args=!args:"=!" |
| 245 | +if "!args!"=="" ( |
| 246 | + "!pypath!" "!thisDir!!script_name!" |
| 247 | +) else ( |
| 248 | + "!pypath!" "!thisDir!!script_name!" %* |
| 249 | +) |
| 250 | +goto :EOF |
0 commit comments