gibMacOS/MakeInstall.bat

212 lines
6.1 KiB
Batchfile
Raw Normal View History

2018-10-07 03:07:20 +00:00
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%~1'=='ELEV' (shift & goto main)
ECHO.
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
@echo off
setlocal enableDelayedExpansion
REM Setup initial vars
2019-03-14 17:48:18 +00:00
set "script_name=%~n0.py"
2018-10-07 03:07:20 +00:00
set "thisDir=%~dp0"
2019-03-14 17:47:17 +00:00
set /a tried=0
2019-03-14 18:09:20 +00:00
set "toask=yes"
2019-06-07 15:52:18 +00:00
set "python="
2018-10-07 03:07:20 +00:00
2019-03-14 17:47:17 +00:00
goto checkscript
:checkscript
2019-02-21 19:30:04 +00:00
REM Check for our script first
if not exist "!thisDir!\!script_name!" (
echo Could not find !script_name!.
echo Please make sure to run this script from the same directory
echo as !script_name!.
echo.
echo Press [enter] to quit.
pause > nul
exit /b
)
2019-03-14 17:47:17 +00:00
goto checkpy
2019-02-21 19:30:04 +00:00
2019-03-14 17:47:17 +00:00
:checkpy
2018-10-07 03:07:20 +00:00
REM Get python location
2019-06-07 15:52:18 +00:00
set "dummypath=%USERPROFILE%\AppData\Local\Microsoft\WindowsApps\python.exe"
FOR /F "tokens=* USEBACKQ" %%F IN (`where python`) DO (
if /i not "%%F" == "!dummypath!" (
set "python=%%F"
2019-05-15 20:47:30 +00:00
)
2018-10-07 03:07:20 +00:00
)
REM Check for py and give helpful hints!
if /i "!python!"=="" (
2019-03-14 17:47:17 +00:00
if %tried% lss 1 (
2019-03-14 18:09:20 +00:00
if /i "!toask!"=="yes" (
REM Better ask permission first
goto askinstall
) else (
goto installpy
)
2019-03-14 17:47:17 +00:00
) else (
cls
echo ### ###
echo # Warning #
echo ### ###
REM Couldn't install for whatever reason - give the error message
echo Python is not installed or not found in your PATH var.
echo Please install it from https://www.python.org/downloads/windows/
echo.
echo Make sure you check the box labeled:
echo.
echo "Add Python X.X to PATH"
echo.
echo Where X.X is the py version you're installing.
echo.
echo Press [enter] to quit.
pause > nul
exit /b
)
)
goto runscript
2019-03-14 18:09:20 +00:00
:askinstall
cls
echo ### ###
echo # Python Not Found #
echo ### ###
echo.
echo Python was not found on the system or in the PATH var.
echo.
set /p "menu=Would you like to install it now? [y/n]: "
if /i "!menu!"=="y" (
REM We got the OK - install it
goto installpy
) else if "!menu!"=="n" (
REM No OK here...
set /a tried=%tried%+1
goto checkpy
)
REM Incorrect answer - go back
goto askinstall
2019-03-14 17:47:17 +00:00
:installpy
REM This will attempt to download and install python
REM First we get the html for the python downloads page for Windows
set /a tried=%tried%+1
cls
echo ### ###
echo # Installing Python #
echo ### ###
echo.
echo Gathering info from https://www.python.org/downloads/windows/...
2019-03-30 03:54:08 +00:00
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')"
2019-03-14 17:47:17 +00:00
if not exist "%TEMP%\pyurl.txt" (
goto checkpy
)
echo Parsing for latest...
REM Got the file, let's parse it
pushd "%TEMP%"
set "release="
for /F "tokens=*" %%x in (pyurl.txt) do (
set "t=%%x"
if /i not "%%x" == "" (
if /i not "!t:Latest Python 3=!"=="!t!" (
REM echo !t!
set "release=!t!"
)
)
)
popd
REM Let's replace the " with ' and split the string by spaces
REM to get the actual version number
set "release=!release:"='!"
for /F "tokens=8* delims= " %%x in ("!release!") do (
set "release=%%x"
)
REM Once more - split at the < and get the first
for /F "tokens=1* delims=<" %%x in ("!release!") do (
set "release=%%x"
)
echo Found Python !release! - Downloading...
REM Let's delete our txt file now - we no longer need it
del "%TEMP%\pyurl.txt"
REM At this point - we should have the version number.
REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe"
REM Now we download it with our slick powershell command
2019-03-30 03:54:08 +00:00
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.exe')"
2019-03-14 17:47:17 +00:00
REM If it doesn't exist - we bail
if not exist "%TEMP%\pyinstall.exe" (
goto checkpy
)
REM It should exist at this point - let's run it to install silently
echo Installing...
echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
pushd "%TEMP%"
pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
popd
echo Installer finsihed with %ERRORLEVEL% status.
REM Now we should be able to delete the installer and check for py again
del "%TEMP%\pyinstall.exe"
REM If it worked, then we should have python in our PATH
REM this does not get updated right away though - let's try
REM manually updating the local PATH var
set "spath="
set "upath="
for /f "tokens=2* delims= " %%i in ('reg.exe query "HKCU\Environment" /v "Path" 2^> nul') do (
if NOT "%%j"=="" (
set "upath=%%j"
)
)
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"
)
)
if not "!spath!" == "" (
REM We got something in the system path
set "PATH=!spath!"
if not "!upath!" == "" (
REM We also have something in the user path
set "PATH=!PATH!;!upath!"
)
) else if not "!upath!" == "" (
set "PATH=!upath!"
2018-10-07 03:07:20 +00:00
)
2019-03-14 17:47:17 +00:00
goto checkpy
exit /b
2018-10-07 03:07:20 +00:00
2019-03-14 17:47:17 +00:00
:runscript
2018-10-07 03:07:20 +00:00
REM Python found
2019-03-14 18:15:43 +00:00
cls
2018-10-07 03:07:20 +00:00
if "%*"=="" (
2019-06-07 15:52:18 +00:00
"!python!" "!thisDir!!script_name!"
2018-10-07 03:07:20 +00:00
) else (
2019-06-07 15:52:18 +00:00
"!python!" "!thisDir!!script_name!" %*
2019-02-21 19:30:04 +00:00
)
2019-03-14 17:47:17 +00:00
goto :EOF