Compare commits

...

7 Commits

Author SHA1 Message Date
Rootspring e7ae9f68f2
Merge c84871453c into 0749880ad1 2024-03-31 18:17:01 +02:00
CorpNewt 0749880ad1
Update gibMacOS.command 2024-03-23 12:34:48 -05:00
CorpNewt f0db386238
Update BuildmacOSInstallApp.command 2024-03-23 12:34:28 -05:00
CorpNewt a86022adb7 Line ending woes 2024-03-13 09:28:01 -05:00
CorpNewt 12de8f78a3
Updates for python.org gzip compression 2024-03-13 04:18:50 -05:00
CorpNewt 2a60cda8dd
Updates for python.org gzip compression 2024-03-13 04:18:42 -05:00
cheesycod c84871453c
Temporary file upload 2020-06-03 11:40:26 +05:30
5 changed files with 796 additions and 782 deletions

View File

@ -3,7 +3,7 @@
# Get the curent directory, the script name # Get the curent directory, the script name
# and the script name with "py" substituted for the extension. # and the script name with "py" substituted for the extension.
args=( "$@" ) args=( "$@" )
dir="${0%/*}" dir="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
script="${0##*/}" script="${0##*/}"
target="${script%.*}.py" target="${script%.*}.py"

View File

@ -1,390 +1,397 @@
@echo off @echo off
REM Get our local path before delayed expansion - allows ! in path REM Get our local path before delayed expansion - allows ! in path
set "thisDir=%~dp0" set "thisDir=%~dp0"
setlocal enableDelayedExpansion setlocal enableDelayedExpansion
REM Setup initial vars REM Setup initial vars
set "script_name=" set "script_name="
set /a tried=0 set /a tried=0
set "toask=yes" set "toask=yes"
set "pause_on_error=yes" set "pause_on_error=yes"
set "py2v=" set "py2v="
set "py2path=" set "py2path="
set "py3v=" set "py3v="
set "py3path=" set "py3path="
set "pypath=" set "pypath="
set "targetpy=3" set "targetpy=3"
REM use_py3: REM use_py3:
REM TRUE = Use if found, use py2 otherwise REM TRUE = Use if found, use py2 otherwise
REM FALSE = Use py2 REM FALSE = Use py2
REM FORCE = Use py3 REM FORCE = Use py3
set "use_py3=TRUE" set "use_py3=TRUE"
REM We'll parse if the first argument passed is REM We'll parse if the first argument passed is
REM --install-python and if so, we'll just install REM --install-python and if so, we'll just install
set "just_installing=FALSE" set "just_installing=FALSE"
REM Get the system32 (or equivalent) path REM Get the system32 (or equivalent) path
call :getsyspath "syspath" call :getsyspath "syspath"
REM Make sure the syspath exists REM Make sure the syspath exists
if "!syspath!" == "" ( if "!syspath!" == "" (
if exist "%SYSTEMROOT%\system32\cmd.exe" ( if exist "%SYSTEMROOT%\system32\cmd.exe" (
if exist "%SYSTEMROOT%\system32\reg.exe" ( if exist "%SYSTEMROOT%\system32\reg.exe" (
if exist "%SYSTEMROOT%\system32\where.exe" ( if exist "%SYSTEMROOT%\system32\where.exe" (
REM Fall back on the default path if it exists REM Fall back on the default path if it exists
set "ComSpec=%SYSTEMROOT%\system32\cmd.exe" set "ComSpec=%SYSTEMROOT%\system32\cmd.exe"
set "syspath=%SYSTEMROOT%\system32\" set "syspath=%SYSTEMROOT%\system32\"
) )
) )
) )
if "!syspath!" == "" ( if "!syspath!" == "" (
cls cls
echo ### ### echo ### ###
echo # Warning # echo # Warning #
echo ### ### echo ### ###
echo. echo.
echo Could not locate cmd.exe, reg.exe, or where.exe echo Could not locate cmd.exe, reg.exe, or where.exe
echo. echo.
echo Please ensure your ComSpec environment variable is properly configured and echo Please ensure your ComSpec environment variable is properly configured and
echo points directly to cmd.exe, then try again. echo points directly to cmd.exe, then try again.
echo. echo.
echo Current CompSpec Value: "%ComSpec%" echo Current CompSpec Value: "%ComSpec%"
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
) )
if "%~1" == "--install-python" ( if "%~1" == "--install-python" (
set "just_installing=TRUE" set "just_installing=TRUE"
goto installpy goto installpy
) )
goto checkscript goto checkscript
:checkscript :checkscript
REM Check for our script first REM Check for our script first
set "looking_for=!script_name!" set "looking_for=!script_name!"
if "!script_name!" == "" ( if "!script_name!" == "" (
set "looking_for=%~n0.py or %~n0.command" set "looking_for=%~n0.py or %~n0.command"
set "script_name=%~n0.py" set "script_name=%~n0.py"
if not exist "!thisDir!\!script_name!" ( if not exist "!thisDir!\!script_name!" (
set "script_name=%~n0.command" set "script_name=%~n0.command"
) )
) )
if not exist "!thisDir!\!script_name!" ( if not exist "!thisDir!\!script_name!" (
echo Could not find !looking_for!. echo Could not find !looking_for!.
echo Please make sure to run this script from the same directory echo Please make sure to run this script from the same directory
echo as !looking_for!. echo as !looking_for!.
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
goto checkpy goto checkpy
:checkpy :checkpy
call :updatepath call :updatepath
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" )
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python3 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python3 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" )
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe py 2^> nul`) do ( call :checkpylauncher "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe py 2^> nul`) do ( call :checkpylauncher "%%x" "py2v" "py2path" "py3v" "py3path" )
REM Walk our returns to see if we need to install REM Walk our returns to see if we need to install
if /i "!use_py3!" == "FALSE" ( if /i "!use_py3!" == "FALSE" (
set "targetpy=2" set "targetpy=2"
set "pypath=!py2path!" set "pypath=!py2path!"
) else if /i "!use_py3!" == "FORCE" ( ) else if /i "!use_py3!" == "FORCE" (
set "pypath=!py3path!" set "pypath=!py3path!"
) else if /i "!use_py3!" == "TRUE" ( ) else if /i "!use_py3!" == "TRUE" (
set "pypath=!py3path!" set "pypath=!py3path!"
if "!pypath!" == "" set "pypath=!py2path!" if "!pypath!" == "" set "pypath=!py2path!"
) )
if not "!pypath!" == "" ( if not "!pypath!" == "" (
goto runscript goto runscript
) )
if !tried! lss 1 ( if !tried! lss 1 (
if /i "!toask!"=="yes" ( if /i "!toask!"=="yes" (
REM Better ask permission first REM Better ask permission first
goto askinstall goto askinstall
) else ( ) else (
goto installpy goto installpy
) )
) else ( ) else (
cls cls
echo ### ### echo ### ###
echo # Warning # echo # Warning #
echo ### ### echo ### ###
echo. echo.
REM Couldn't install for whatever reason - give the error message REM Couldn't install for whatever reason - give the error message
echo Python is not installed or not found in your PATH var. echo Python is not installed or not found in your PATH var.
echo Please install it from https://www.python.org/downloads/windows/ echo Please install it from https://www.python.org/downloads/windows/
echo. echo.
echo Make sure you check the box labeled: echo Make sure you check the box labeled:
echo. echo.
echo "Add Python X.X to PATH" echo "Add Python X.X to PATH"
echo. echo.
echo Where X.X is the py version you're installing. echo Where X.X is the py version you're installing.
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
goto runscript goto runscript
:checkpylauncher <path> <py2v> <py2path> <py3v> <py3path> :checkpylauncher <path> <py2v> <py2path> <py3v> <py3path>
REM Attempt to check the latest python 2 and 3 versions via the py launcher REM Attempt to check the latest python 2 and 3 versions via the py launcher
for /f "USEBACKQ tokens=*" %%x in (`%~1 -2 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" ) for /f "USEBACKQ tokens=*" %%x in (`%~1 -2 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" )
for /f "USEBACKQ tokens=*" %%x in (`%~1 -3 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" ) for /f "USEBACKQ tokens=*" %%x in (`%~1 -3 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" )
goto :EOF goto :EOF
:checkpyversion <path> <py2v> <py2path> <py3v> <py3path> :checkpyversion <path> <py2v> <py2path> <py3v> <py3path>
set "version="&for /f "tokens=2* USEBACKQ delims= " %%a in (`"%~1" -V 2^>^&1`) do ( set "version="&for /f "tokens=2* USEBACKQ delims= " %%a in (`"%~1" -V 2^>^&1`) do (
REM Ensure we have a version number REM Ensure we have a version number
call :isnumber "%%a" call :isnumber "%%a"
if not "!errorlevel!" == "0" goto :EOF if not "!errorlevel!" == "0" goto :EOF
set "version=%%a" set "version=%%a"
) )
if not defined version goto :EOF if not defined version goto :EOF
if "!version:~0,1!" == "2" ( if "!version:~0,1!" == "2" (
REM Python 2 REM Python 2
call :comparepyversion "!version!" "!%~2!" call :comparepyversion "!version!" "!%~2!"
if "!errorlevel!" == "1" ( if "!errorlevel!" == "1" (
set "%~2=!version!" set "%~2=!version!"
set "%~3=%~1" set "%~3=%~1"
) )
) else ( ) else (
REM Python 3 REM Python 3
call :comparepyversion "!version!" "!%~4!" call :comparepyversion "!version!" "!%~4!"
if "!errorlevel!" == "1" ( if "!errorlevel!" == "1" (
set "%~4=!version!" set "%~4=!version!"
set "%~5=%~1" set "%~5=%~1"
) )
) )
goto :EOF goto :EOF
:isnumber <check_value> :isnumber <check_value>
set "var="&for /f "delims=0123456789." %%i in ("%~1") do set var=%%i set "var="&for /f "delims=0123456789." %%i in ("%~1") do set var=%%i
if defined var (exit /b 1) if defined var (exit /b 1)
exit /b 0 exit /b 0
:comparepyversion <version1> <version2> <return> :comparepyversion <version1> <version2> <return>
REM Exits with status 0 if equal, 1 if v1 gtr v2, 2 if v1 lss v2 REM Exits with status 0 if equal, 1 if v1 gtr v2, 2 if v1 lss v2
for /f "tokens=1,2,3 delims=." %%a in ("%~1") do ( for /f "tokens=1,2,3 delims=." %%a in ("%~1") do (
set a1=%%a set a1=%%a
set a2=%%b set a2=%%b
set a3=%%c set a3=%%c
) )
for /f "tokens=1,2,3 delims=." %%a in ("%~2") do ( for /f "tokens=1,2,3 delims=." %%a in ("%~2") do (
set b1=%%a set b1=%%a
set b2=%%b set b2=%%b
set b3=%%c set b3=%%c
) )
if not defined a1 set a1=0 if not defined a1 set a1=0
if not defined a2 set a2=0 if not defined a2 set a2=0
if not defined a3 set a3=0 if not defined a3 set a3=0
if not defined b1 set b1=0 if not defined b1 set b1=0
if not defined b2 set b2=0 if not defined b2 set b2=0
if not defined b3 set b3=0 if not defined b3 set b3=0
if %a1% gtr %b1% exit /b 1 if %a1% gtr %b1% exit /b 1
if %a1% lss %b1% exit /b 2 if %a1% lss %b1% exit /b 2
if %a2% gtr %b2% exit /b 1 if %a2% gtr %b2% exit /b 1
if %a2% lss %b2% exit /b 2 if %a2% lss %b2% exit /b 2
if %a3% gtr %b3% exit /b 1 if %a3% gtr %b3% exit /b 1
if %a3% lss %b3% exit /b 2 if %a3% lss %b3% exit /b 2
exit /b 0 exit /b 0
:askinstall :askinstall
cls cls
echo ### ### echo ### ###
echo # Python Not Found # echo # Python Not Found #
echo ### ### echo ### ###
echo. echo.
echo Python !targetpy! was not found on the system or in the PATH var. echo Python !targetpy! was not found on the system or in the PATH var.
echo. echo.
set /p "menu=Would you like to install it now? [y/n]: " set /p "menu=Would you like to install it now? [y/n]: "
if /i "!menu!"=="y" ( if /i "!menu!"=="y" (
REM We got the OK - install it REM We got the OK - install it
goto installpy goto installpy
) else if "!menu!"=="n" ( ) else if "!menu!"=="n" (
REM No OK here... REM No OK here...
set /a tried=!tried!+1 set /a tried=!tried!+1
goto checkpy goto checkpy
) )
REM Incorrect answer - go back REM Incorrect answer - go back
goto askinstall goto askinstall
:installpy :installpy
REM This will attempt to download and install python REM This will attempt to download and install python
REM First we get the html for the python downloads page for Windows REM First we get the html for the python downloads page for Windows
set /a tried=!tried!+1 set /a tried=!tried!+1
cls cls
echo ### ### echo ### ###
echo # Installing Python # echo # Installing Python #
echo ### ### echo ### ###
echo. echo.
echo Gathering info from https://www.python.org/downloads/windows/... echo Gathering info from https://www.python.org/downloads/windows/...
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')" powershell -command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;(new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')"
if not exist "%TEMP%\pyurl.txt" ( REM Extract it if it's gzip compressed
if /i "!just_installing!" == "TRUE" ( powershell -command "$infile='%TEMP%\pyurl.txt';$outfile='%TEMP%\pyurl.temp';try{$input=New-Object System.IO.FileStream $infile,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read);$output=New-Object System.IO.FileStream $outfile,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None);$gzipStream=New-Object System.IO.Compression.GzipStream $input,([IO.Compression.CompressionMode]::Decompress);$buffer=New-Object byte[](1024);while($true){$read=$gzipstream.Read($buffer,0,1024);if($read -le 0){break};$output.Write($buffer,0,$read)};$gzipStream.Close();$output.Close();$input.Close();Move-Item -Path $outfile -Destination $infile -Force}catch{}"
echo Failed to get info if not exist "%TEMP%\pyurl.txt" (
exit /b 1 if /i "!just_installing!" == "TRUE" (
) else ( echo Failed to get info
goto checkpy exit /b 1
) ) else (
) goto checkpy
)
echo Parsing for latest... )
pushd "%TEMP%" echo Parsing for latest...
:: Version detection code slimmed by LussacZheng (https://github.com/corpnewt/gibMacOS/issues/20) pushd "%TEMP%"
for /f "tokens=9 delims=< " %%x in ('findstr /i /c:"Latest Python !targetpy! Release" pyurl.txt') do ( set "release=%%x" ) :: Version detection code slimmed by LussacZheng (https://github.com/corpnewt/gibMacOS/issues/20)
popd for /f "tokens=9 delims=< " %%x in ('findstr /i /c:"Latest Python !targetpy! Release" pyurl.txt') do ( set "release=%%x" )
popd
echo Found Python !release! - Downloading... if "!release!" == "" (
REM Let's delete our txt file now - we no longer need it if /i "!just_installing!" == "TRUE" (
del "%TEMP%\pyurl.txt" echo Failed to get python version
exit /b 1
REM At this point - we should have the version number. ) else (
REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe" goto checkpy
set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe" )
set "pytype=exe" )
if "!targetpy!" == "2" ( echo Found Python !release! - Downloading...
set "url=https://www.python.org/ftp/python/!release!/python-!release!.amd64.msi" REM Let's delete our txt file now - we no longer need it
set "pytype=msi" del "%TEMP%\pyurl.txt"
) REM At this point - we should have the version number.
REM Now we download it with our slick powershell command REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.!pytype!')" set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe"
REM If it doesn't exist - we bail set "pytype=exe"
if not exist "%TEMP%\pyinstall.!pytype!" ( if "!targetpy!" == "2" (
if /i "!just_installing!" == "TRUE" ( set "url=https://www.python.org/ftp/python/!release!/python-!release!.amd64.msi"
echo Failed to download installer set "pytype=msi"
exit /b 1 )
) else ( REM Now we download it with our slick powershell command
goto checkpy powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.!pytype!')"
) REM If it doesn't exist - we bail
) if not exist "%TEMP%\pyinstall.!pytype!" (
REM It should exist at this point - let's run it to install silently if /i "!just_installing!" == "TRUE" (
echo Installing... echo Failed to download installer
pushd "%TEMP%" exit /b 1
if /i "!pytype!" == "exe" ( ) else (
echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 goto checkpy
pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 )
) else ( )
set "foldername=!release:.=!" REM It should exist at this point - let's run it to install silently
echo msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" echo Installing...
msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" pushd "%TEMP%"
) if /i "!pytype!" == "exe" (
popd echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
echo Installer finished with %ERRORLEVEL% status. pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
REM Now we should be able to delete the installer and check for py again ) else (
del "%TEMP%\pyinstall.!pytype!" set "foldername=!release:.=!"
REM If it worked, then we should have python in our PATH echo msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!"
REM this does not get updated right away though - let's try msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!"
REM manually updating the local PATH var )
call :updatepath popd
if /i "!just_installing!" == "TRUE" ( echo Installer finished with %ERRORLEVEL% status.
echo. REM Now we should be able to delete the installer and check for py again
echo Done. del "%TEMP%\pyinstall.!pytype!"
) else ( REM If it worked, then we should have python in our PATH
goto checkpy REM this does not get updated right away though - let's try
) REM manually updating the local PATH var
exit /b call :updatepath
if /i "!just_installing!" == "TRUE" (
:runscript echo.
REM Python found echo Done.
cls ) else (
set "args=%*" goto checkpy
set "args=!args:"=!" )
if "!args!"=="" ( exit /b
"!pypath!" "!thisDir!!script_name!"
) else ( :runscript
"!pypath!" "!thisDir!!script_name!" %* REM Python found
) cls
if /i "!pause_on_error!" == "yes" ( set "args=%*"
if not "%ERRORLEVEL%" == "0" ( set "args=!args:"=!"
echo. if "!args!"=="" (
echo Script exited with error code: %ERRORLEVEL% "!pypath!" "!thisDir!!script_name!"
echo. ) else (
echo Press [enter] to exit... "!pypath!" "!thisDir!!script_name!" %*
pause > nul )
) if /i "!pause_on_error!" == "yes" (
) if not "%ERRORLEVEL%" == "0" (
goto :EOF echo.
echo Script exited with error code: %ERRORLEVEL%
:undouble <string_name> <string_value> <character> echo.
REM Helper function to strip doubles of a single character out of a string recursively echo Press [enter] to exit...
set "string_value=%~2" pause > nul
:undouble_continue )
set "check=!string_value:%~3%~3=%~3!" )
if not "!check!" == "!string_value!" ( goto :EOF
set "string_value=!check!"
goto :undouble_continue :undouble <string_name> <string_value> <character>
) REM Helper function to strip doubles of a single character out of a string recursively
set "%~1=!check!" set "string_value=%~2"
goto :EOF :undouble_continue
set "check=!string_value:%~3%~3=%~3!"
:updatepath if not "!check!" == "!string_value!" (
set "spath=" set "string_value=!check!"
set "upath=" goto :undouble_continue
for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKCU\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "upath=%%j" ) )
for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "spath=%%j" ) set "%~1=!check!"
if not "%spath%" == "" ( goto :EOF
REM We got something in the system path
set "PATH=%spath%" :updatepath
if not "%upath%" == "" ( set "spath="
REM We also have something in the user path set "upath="
set "PATH=%PATH%;%upath%" for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKCU\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "upath=%%j" )
) for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "spath=%%j" )
) else if not "%upath%" == "" ( if not "%spath%" == "" (
set "PATH=%upath%" REM We got something in the system path
) set "PATH=%spath%"
REM Remove double semicolons from the adjusted PATH if not "%upath%" == "" (
call :undouble "PATH" "%PATH%" ";" REM We also have something in the user path
goto :EOF set "PATH=%PATH%;%upath%"
)
:getsyspath <variable_name> ) else if not "%upath%" == "" (
REM Helper method to return a valid path to cmd.exe, reg.exe, and where.exe by set "PATH=%upath%"
REM walking the ComSpec var - will also repair it in memory if need be )
REM Strip double semi-colons REM Remove double semicolons from the adjusted PATH
call :undouble "temppath" "%ComSpec%" ";" call :undouble "PATH" "%PATH%" ";"
goto :EOF
REM Dirty hack to leverage the "line feed" approach - there are some odd side
REM effects with this. Do not use this variable name in comments near this :getsyspath <variable_name>
REM line - as it seems to behave erradically. REM Helper method to return a valid path to cmd.exe, reg.exe, and where.exe by
(set LF=^ REM walking the ComSpec var - will also repair it in memory if need be
%=this line is empty=% REM Strip double semi-colons
) call :undouble "temppath" "%ComSpec%" ";"
REM Replace instances of semi-colons with a line feed and wrap
REM in parenthesis to work around some strange batch behavior REM Dirty hack to leverage the "line feed" approach - there are some odd side
set "testpath=%temppath:;=!LF!%" REM effects with this. Do not use this variable name in comments near this
REM line - as it seems to behave erradically.
REM Let's walk each path and test if cmd.exe, reg.exe, and where.exe exist there (set LF=^
set /a found=0 %=this line is empty=%
for /f "tokens=* delims=" %%i in ("!testpath!") do ( )
REM Only continue if we haven't found it yet REM Replace instances of semi-colons with a line feed and wrap
if not "%%i" == "" ( REM in parenthesis to work around some strange batch behavior
if !found! lss 1 ( set "testpath=%temppath:;=!LF!%"
set "checkpath=%%i"
REM Remove "cmd.exe" from the end if it exists REM Let's walk each path and test if cmd.exe, reg.exe, and where.exe exist there
if /i "!checkpath:~-7!" == "cmd.exe" ( set /a found=0
set "checkpath=!checkpath:~0,-7!" for /f "tokens=* delims=" %%i in ("!testpath!") do (
) REM Only continue if we haven't found it yet
REM Pad the end with a backslash if needed if not "%%i" == "" (
if not "!checkpath:~-1!" == "\" ( if !found! lss 1 (
set "checkpath=!checkpath!\" set "checkpath=%%i"
) REM Remove "cmd.exe" from the end if it exists
REM Let's see if cmd, reg, and where exist there - and set it if so if /i "!checkpath:~-7!" == "cmd.exe" (
if EXIST "!checkpath!cmd.exe" ( set "checkpath=!checkpath:~0,-7!"
if EXIST "!checkpath!reg.exe" ( )
if EXIST "!checkpath!where.exe" ( REM Pad the end with a backslash if needed
set /a found=1 if not "!checkpath:~-1!" == "\" (
set "ComSpec=!checkpath!cmd.exe" set "checkpath=!checkpath!\"
set "%~1=!checkpath!" )
) REM Let's see if cmd, reg, and where exist there - and set it if so
) if EXIST "!checkpath!cmd.exe" (
) if EXIST "!checkpath!reg.exe" (
) if EXIST "!checkpath!where.exe" (
) set /a found=1
) set "ComSpec=!checkpath!cmd.exe"
goto :EOF set "%~1=!checkpath!"
)
)
)
)
)
)
goto :EOF

BIN
gibMacOS-master 2.zip Normal file

Binary file not shown.

View File

@ -1,390 +1,397 @@
@echo off @echo off
REM Get our local path before delayed expansion - allows ! in path REM Get our local path before delayed expansion - allows ! in path
set "thisDir=%~dp0" set "thisDir=%~dp0"
setlocal enableDelayedExpansion setlocal enableDelayedExpansion
REM Setup initial vars REM Setup initial vars
set "script_name=" set "script_name="
set /a tried=0 set /a tried=0
set "toask=yes" set "toask=yes"
set "pause_on_error=yes" set "pause_on_error=yes"
set "py2v=" set "py2v="
set "py2path=" set "py2path="
set "py3v=" set "py3v="
set "py3path=" set "py3path="
set "pypath=" set "pypath="
set "targetpy=3" set "targetpy=3"
REM use_py3: REM use_py3:
REM TRUE = Use if found, use py2 otherwise REM TRUE = Use if found, use py2 otherwise
REM FALSE = Use py2 REM FALSE = Use py2
REM FORCE = Use py3 REM FORCE = Use py3
set "use_py3=TRUE" set "use_py3=TRUE"
REM We'll parse if the first argument passed is REM We'll parse if the first argument passed is
REM --install-python and if so, we'll just install REM --install-python and if so, we'll just install
set "just_installing=FALSE" set "just_installing=FALSE"
REM Get the system32 (or equivalent) path REM Get the system32 (or equivalent) path
call :getsyspath "syspath" call :getsyspath "syspath"
REM Make sure the syspath exists REM Make sure the syspath exists
if "!syspath!" == "" ( if "!syspath!" == "" (
if exist "%SYSTEMROOT%\system32\cmd.exe" ( if exist "%SYSTEMROOT%\system32\cmd.exe" (
if exist "%SYSTEMROOT%\system32\reg.exe" ( if exist "%SYSTEMROOT%\system32\reg.exe" (
if exist "%SYSTEMROOT%\system32\where.exe" ( if exist "%SYSTEMROOT%\system32\where.exe" (
REM Fall back on the default path if it exists REM Fall back on the default path if it exists
set "ComSpec=%SYSTEMROOT%\system32\cmd.exe" set "ComSpec=%SYSTEMROOT%\system32\cmd.exe"
set "syspath=%SYSTEMROOT%\system32\" set "syspath=%SYSTEMROOT%\system32\"
) )
) )
) )
if "!syspath!" == "" ( if "!syspath!" == "" (
cls cls
echo ### ### echo ### ###
echo # Warning # echo # Warning #
echo ### ### echo ### ###
echo. echo.
echo Could not locate cmd.exe, reg.exe, or where.exe echo Could not locate cmd.exe, reg.exe, or where.exe
echo. echo.
echo Please ensure your ComSpec environment variable is properly configured and echo Please ensure your ComSpec environment variable is properly configured and
echo points directly to cmd.exe, then try again. echo points directly to cmd.exe, then try again.
echo. echo.
echo Current CompSpec Value: "%ComSpec%" echo Current CompSpec Value: "%ComSpec%"
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
) )
if "%~1" == "--install-python" ( if "%~1" == "--install-python" (
set "just_installing=TRUE" set "just_installing=TRUE"
goto installpy goto installpy
) )
goto checkscript goto checkscript
:checkscript :checkscript
REM Check for our script first REM Check for our script first
set "looking_for=!script_name!" set "looking_for=!script_name!"
if "!script_name!" == "" ( if "!script_name!" == "" (
set "looking_for=%~n0.py or %~n0.command" set "looking_for=%~n0.py or %~n0.command"
set "script_name=%~n0.py" set "script_name=%~n0.py"
if not exist "!thisDir!\!script_name!" ( if not exist "!thisDir!\!script_name!" (
set "script_name=%~n0.command" set "script_name=%~n0.command"
) )
) )
if not exist "!thisDir!\!script_name!" ( if not exist "!thisDir!\!script_name!" (
echo Could not find !looking_for!. echo Could not find !looking_for!.
echo Please make sure to run this script from the same directory echo Please make sure to run this script from the same directory
echo as !looking_for!. echo as !looking_for!.
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
goto checkpy goto checkpy
:checkpy :checkpy
call :updatepath call :updatepath
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" )
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python3 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe python3 2^> nul`) do ( call :checkpyversion "%%x" "py2v" "py2path" "py3v" "py3path" )
for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe py 2^> nul`) do ( call :checkpylauncher "%%x" "py2v" "py2path" "py3v" "py3path" ) for /f "USEBACKQ tokens=*" %%x in (`!syspath!where.exe py 2^> nul`) do ( call :checkpylauncher "%%x" "py2v" "py2path" "py3v" "py3path" )
REM Walk our returns to see if we need to install REM Walk our returns to see if we need to install
if /i "!use_py3!" == "FALSE" ( if /i "!use_py3!" == "FALSE" (
set "targetpy=2" set "targetpy=2"
set "pypath=!py2path!" set "pypath=!py2path!"
) else if /i "!use_py3!" == "FORCE" ( ) else if /i "!use_py3!" == "FORCE" (
set "pypath=!py3path!" set "pypath=!py3path!"
) else if /i "!use_py3!" == "TRUE" ( ) else if /i "!use_py3!" == "TRUE" (
set "pypath=!py3path!" set "pypath=!py3path!"
if "!pypath!" == "" set "pypath=!py2path!" if "!pypath!" == "" set "pypath=!py2path!"
) )
if not "!pypath!" == "" ( if not "!pypath!" == "" (
goto runscript goto runscript
) )
if !tried! lss 1 ( if !tried! lss 1 (
if /i "!toask!"=="yes" ( if /i "!toask!"=="yes" (
REM Better ask permission first REM Better ask permission first
goto askinstall goto askinstall
) else ( ) else (
goto installpy goto installpy
) )
) else ( ) else (
cls cls
echo ### ### echo ### ###
echo # Warning # echo # Warning #
echo ### ### echo ### ###
echo. echo.
REM Couldn't install for whatever reason - give the error message REM Couldn't install for whatever reason - give the error message
echo Python is not installed or not found in your PATH var. echo Python is not installed or not found in your PATH var.
echo Please install it from https://www.python.org/downloads/windows/ echo Please install it from https://www.python.org/downloads/windows/
echo. echo.
echo Make sure you check the box labeled: echo Make sure you check the box labeled:
echo. echo.
echo "Add Python X.X to PATH" echo "Add Python X.X to PATH"
echo. echo.
echo Where X.X is the py version you're installing. echo Where X.X is the py version you're installing.
echo. echo.
echo Press [enter] to quit. echo Press [enter] to quit.
pause > nul pause > nul
exit /b 1 exit /b 1
) )
goto runscript goto runscript
:checkpylauncher <path> <py2v> <py2path> <py3v> <py3path> :checkpylauncher <path> <py2v> <py2path> <py3v> <py3path>
REM Attempt to check the latest python 2 and 3 versions via the py launcher REM Attempt to check the latest python 2 and 3 versions via the py launcher
for /f "USEBACKQ tokens=*" %%x in (`%~1 -2 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" ) for /f "USEBACKQ tokens=*" %%x in (`%~1 -2 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" )
for /f "USEBACKQ tokens=*" %%x in (`%~1 -3 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" ) for /f "USEBACKQ tokens=*" %%x in (`%~1 -3 -c "import sys; print(sys.executable)" 2^> nul`) do ( call :checkpyversion "%%x" "%~2" "%~3" "%~4" "%~5" )
goto :EOF goto :EOF
:checkpyversion <path> <py2v> <py2path> <py3v> <py3path> :checkpyversion <path> <py2v> <py2path> <py3v> <py3path>
set "version="&for /f "tokens=2* USEBACKQ delims= " %%a in (`"%~1" -V 2^>^&1`) do ( set "version="&for /f "tokens=2* USEBACKQ delims= " %%a in (`"%~1" -V 2^>^&1`) do (
REM Ensure we have a version number REM Ensure we have a version number
call :isnumber "%%a" call :isnumber "%%a"
if not "!errorlevel!" == "0" goto :EOF if not "!errorlevel!" == "0" goto :EOF
set "version=%%a" set "version=%%a"
) )
if not defined version goto :EOF if not defined version goto :EOF
if "!version:~0,1!" == "2" ( if "!version:~0,1!" == "2" (
REM Python 2 REM Python 2
call :comparepyversion "!version!" "!%~2!" call :comparepyversion "!version!" "!%~2!"
if "!errorlevel!" == "1" ( if "!errorlevel!" == "1" (
set "%~2=!version!" set "%~2=!version!"
set "%~3=%~1" set "%~3=%~1"
) )
) else ( ) else (
REM Python 3 REM Python 3
call :comparepyversion "!version!" "!%~4!" call :comparepyversion "!version!" "!%~4!"
if "!errorlevel!" == "1" ( if "!errorlevel!" == "1" (
set "%~4=!version!" set "%~4=!version!"
set "%~5=%~1" set "%~5=%~1"
) )
) )
goto :EOF goto :EOF
:isnumber <check_value> :isnumber <check_value>
set "var="&for /f "delims=0123456789." %%i in ("%~1") do set var=%%i set "var="&for /f "delims=0123456789." %%i in ("%~1") do set var=%%i
if defined var (exit /b 1) if defined var (exit /b 1)
exit /b 0 exit /b 0
:comparepyversion <version1> <version2> <return> :comparepyversion <version1> <version2> <return>
REM Exits with status 0 if equal, 1 if v1 gtr v2, 2 if v1 lss v2 REM Exits with status 0 if equal, 1 if v1 gtr v2, 2 if v1 lss v2
for /f "tokens=1,2,3 delims=." %%a in ("%~1") do ( for /f "tokens=1,2,3 delims=." %%a in ("%~1") do (
set a1=%%a set a1=%%a
set a2=%%b set a2=%%b
set a3=%%c set a3=%%c
) )
for /f "tokens=1,2,3 delims=." %%a in ("%~2") do ( for /f "tokens=1,2,3 delims=." %%a in ("%~2") do (
set b1=%%a set b1=%%a
set b2=%%b set b2=%%b
set b3=%%c set b3=%%c
) )
if not defined a1 set a1=0 if not defined a1 set a1=0
if not defined a2 set a2=0 if not defined a2 set a2=0
if not defined a3 set a3=0 if not defined a3 set a3=0
if not defined b1 set b1=0 if not defined b1 set b1=0
if not defined b2 set b2=0 if not defined b2 set b2=0
if not defined b3 set b3=0 if not defined b3 set b3=0
if %a1% gtr %b1% exit /b 1 if %a1% gtr %b1% exit /b 1
if %a1% lss %b1% exit /b 2 if %a1% lss %b1% exit /b 2
if %a2% gtr %b2% exit /b 1 if %a2% gtr %b2% exit /b 1
if %a2% lss %b2% exit /b 2 if %a2% lss %b2% exit /b 2
if %a3% gtr %b3% exit /b 1 if %a3% gtr %b3% exit /b 1
if %a3% lss %b3% exit /b 2 if %a3% lss %b3% exit /b 2
exit /b 0 exit /b 0
:askinstall :askinstall
cls cls
echo ### ### echo ### ###
echo # Python Not Found # echo # Python Not Found #
echo ### ### echo ### ###
echo. echo.
echo Python !targetpy! was not found on the system or in the PATH var. echo Python !targetpy! was not found on the system or in the PATH var.
echo. echo.
set /p "menu=Would you like to install it now? [y/n]: " set /p "menu=Would you like to install it now? [y/n]: "
if /i "!menu!"=="y" ( if /i "!menu!"=="y" (
REM We got the OK - install it REM We got the OK - install it
goto installpy goto installpy
) else if "!menu!"=="n" ( ) else if "!menu!"=="n" (
REM No OK here... REM No OK here...
set /a tried=!tried!+1 set /a tried=!tried!+1
goto checkpy goto checkpy
) )
REM Incorrect answer - go back REM Incorrect answer - go back
goto askinstall goto askinstall
:installpy :installpy
REM This will attempt to download and install python REM This will attempt to download and install python
REM First we get the html for the python downloads page for Windows REM First we get the html for the python downloads page for Windows
set /a tried=!tried!+1 set /a tried=!tried!+1
cls cls
echo ### ### echo ### ###
echo # Installing Python # echo # Installing Python #
echo ### ### echo ### ###
echo. echo.
echo Gathering info from https://www.python.org/downloads/windows/... echo Gathering info from https://www.python.org/downloads/windows/...
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')" powershell -command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;(new-object System.Net.WebClient).DownloadFile('https://www.python.org/downloads/windows/','%TEMP%\pyurl.txt')"
if not exist "%TEMP%\pyurl.txt" ( REM Extract it if it's gzip compressed
if /i "!just_installing!" == "TRUE" ( powershell -command "$infile='%TEMP%\pyurl.txt';$outfile='%TEMP%\pyurl.temp';try{$input=New-Object System.IO.FileStream $infile,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read);$output=New-Object System.IO.FileStream $outfile,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None);$gzipStream=New-Object System.IO.Compression.GzipStream $input,([IO.Compression.CompressionMode]::Decompress);$buffer=New-Object byte[](1024);while($true){$read=$gzipstream.Read($buffer,0,1024);if($read -le 0){break};$output.Write($buffer,0,$read)};$gzipStream.Close();$output.Close();$input.Close();Move-Item -Path $outfile -Destination $infile -Force}catch{}"
echo Failed to get info if not exist "%TEMP%\pyurl.txt" (
exit /b 1 if /i "!just_installing!" == "TRUE" (
) else ( echo Failed to get info
goto checkpy exit /b 1
) ) else (
) goto checkpy
)
echo Parsing for latest... )
pushd "%TEMP%" echo Parsing for latest...
:: Version detection code slimmed by LussacZheng (https://github.com/corpnewt/gibMacOS/issues/20) pushd "%TEMP%"
for /f "tokens=9 delims=< " %%x in ('findstr /i /c:"Latest Python !targetpy! Release" pyurl.txt') do ( set "release=%%x" ) :: Version detection code slimmed by LussacZheng (https://github.com/corpnewt/gibMacOS/issues/20)
popd for /f "tokens=9 delims=< " %%x in ('findstr /i /c:"Latest Python !targetpy! Release" pyurl.txt') do ( set "release=%%x" )
popd
echo Found Python !release! - Downloading... if "!release!" == "" (
REM Let's delete our txt file now - we no longer need it if /i "!just_installing!" == "TRUE" (
del "%TEMP%\pyurl.txt" echo Failed to get python version
exit /b 1
REM At this point - we should have the version number. ) else (
REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe" goto checkpy
set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe" )
set "pytype=exe" )
if "!targetpy!" == "2" ( echo Found Python !release! - Downloading...
set "url=https://www.python.org/ftp/python/!release!/python-!release!.amd64.msi" REM Let's delete our txt file now - we no longer need it
set "pytype=msi" del "%TEMP%\pyurl.txt"
) REM At this point - we should have the version number.
REM Now we download it with our slick powershell command REM We can build the url like so: "https://www.python.org/ftp/python/[version]/python-[version]-amd64.exe"
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.!pytype!')" set "url=https://www.python.org/ftp/python/!release!/python-!release!-amd64.exe"
REM If it doesn't exist - we bail set "pytype=exe"
if not exist "%TEMP%\pyinstall.!pytype!" ( if "!targetpy!" == "2" (
if /i "!just_installing!" == "TRUE" ( set "url=https://www.python.org/ftp/python/!release!/python-!release!.amd64.msi"
echo Failed to download installer set "pytype=msi"
exit /b 1 )
) else ( REM Now we download it with our slick powershell command
goto checkpy powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.!pytype!')"
) REM If it doesn't exist - we bail
) if not exist "%TEMP%\pyinstall.!pytype!" (
REM It should exist at this point - let's run it to install silently if /i "!just_installing!" == "TRUE" (
echo Installing... echo Failed to download installer
pushd "%TEMP%" exit /b 1
if /i "!pytype!" == "exe" ( ) else (
echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 goto checkpy
pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0 )
) else ( )
set "foldername=!release:.=!" REM It should exist at this point - let's run it to install silently
echo msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" echo Installing...
msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!" pushd "%TEMP%"
) if /i "!pytype!" == "exe" (
popd echo pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
echo Installer finished with %ERRORLEVEL% status. pyinstall.exe /quiet PrependPath=1 Include_test=0 Shortcuts=0 Include_launcher=0
REM Now we should be able to delete the installer and check for py again ) else (
del "%TEMP%\pyinstall.!pytype!" set "foldername=!release:.=!"
REM If it worked, then we should have python in our PATH echo msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!"
REM this does not get updated right away though - let's try msiexec /i pyinstall.msi /qb ADDLOCAL=ALL TARGETDIR="%LocalAppData%\Programs\Python\Python!foldername:~0,2!"
REM manually updating the local PATH var )
call :updatepath popd
if /i "!just_installing!" == "TRUE" ( echo Installer finished with %ERRORLEVEL% status.
echo. REM Now we should be able to delete the installer and check for py again
echo Done. del "%TEMP%\pyinstall.!pytype!"
) else ( REM If it worked, then we should have python in our PATH
goto checkpy REM this does not get updated right away though - let's try
) REM manually updating the local PATH var
exit /b call :updatepath
if /i "!just_installing!" == "TRUE" (
:runscript echo.
REM Python found echo Done.
cls ) else (
set "args=%*" goto checkpy
set "args=!args:"=!" )
if "!args!"=="" ( exit /b
"!pypath!" "!thisDir!!script_name!"
) else ( :runscript
"!pypath!" "!thisDir!!script_name!" %* REM Python found
) cls
if /i "!pause_on_error!" == "yes" ( set "args=%*"
if not "%ERRORLEVEL%" == "0" ( set "args=!args:"=!"
echo. if "!args!"=="" (
echo Script exited with error code: %ERRORLEVEL% "!pypath!" "!thisDir!!script_name!"
echo. ) else (
echo Press [enter] to exit... "!pypath!" "!thisDir!!script_name!" %*
pause > nul )
) if /i "!pause_on_error!" == "yes" (
) if not "%ERRORLEVEL%" == "0" (
goto :EOF echo.
echo Script exited with error code: %ERRORLEVEL%
:undouble <string_name> <string_value> <character> echo.
REM Helper function to strip doubles of a single character out of a string recursively echo Press [enter] to exit...
set "string_value=%~2" pause > nul
:undouble_continue )
set "check=!string_value:%~3%~3=%~3!" )
if not "!check!" == "!string_value!" ( goto :EOF
set "string_value=!check!"
goto :undouble_continue :undouble <string_name> <string_value> <character>
) REM Helper function to strip doubles of a single character out of a string recursively
set "%~1=!check!" set "string_value=%~2"
goto :EOF :undouble_continue
set "check=!string_value:%~3%~3=%~3!"
:updatepath if not "!check!" == "!string_value!" (
set "spath=" set "string_value=!check!"
set "upath=" goto :undouble_continue
for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKCU\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "upath=%%j" ) )
for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "spath=%%j" ) set "%~1=!check!"
if not "%spath%" == "" ( goto :EOF
REM We got something in the system path
set "PATH=%spath%" :updatepath
if not "%upath%" == "" ( set "spath="
REM We also have something in the user path set "upath="
set "PATH=%PATH%;%upath%" for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKCU\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "upath=%%j" )
) for /f "USEBACKQ tokens=2* delims= " %%i in (`!syspath!reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "Path" 2^> nul`) do ( if not "%%j" == "" set "spath=%%j" )
) else if not "%upath%" == "" ( if not "%spath%" == "" (
set "PATH=%upath%" REM We got something in the system path
) set "PATH=%spath%"
REM Remove double semicolons from the adjusted PATH if not "%upath%" == "" (
call :undouble "PATH" "%PATH%" ";" REM We also have something in the user path
goto :EOF set "PATH=%PATH%;%upath%"
)
:getsyspath <variable_name> ) else if not "%upath%" == "" (
REM Helper method to return a valid path to cmd.exe, reg.exe, and where.exe by set "PATH=%upath%"
REM walking the ComSpec var - will also repair it in memory if need be )
REM Strip double semi-colons REM Remove double semicolons from the adjusted PATH
call :undouble "temppath" "%ComSpec%" ";" call :undouble "PATH" "%PATH%" ";"
goto :EOF
REM Dirty hack to leverage the "line feed" approach - there are some odd side
REM effects with this. Do not use this variable name in comments near this :getsyspath <variable_name>
REM line - as it seems to behave erradically. REM Helper method to return a valid path to cmd.exe, reg.exe, and where.exe by
(set LF=^ REM walking the ComSpec var - will also repair it in memory if need be
%=this line is empty=% REM Strip double semi-colons
) call :undouble "temppath" "%ComSpec%" ";"
REM Replace instances of semi-colons with a line feed and wrap
REM in parenthesis to work around some strange batch behavior REM Dirty hack to leverage the "line feed" approach - there are some odd side
set "testpath=%temppath:;=!LF!%" REM effects with this. Do not use this variable name in comments near this
REM line - as it seems to behave erradically.
REM Let's walk each path and test if cmd.exe, reg.exe, and where.exe exist there (set LF=^
set /a found=0 %=this line is empty=%
for /f "tokens=* delims=" %%i in ("!testpath!") do ( )
REM Only continue if we haven't found it yet REM Replace instances of semi-colons with a line feed and wrap
if not "%%i" == "" ( REM in parenthesis to work around some strange batch behavior
if !found! lss 1 ( set "testpath=%temppath:;=!LF!%"
set "checkpath=%%i"
REM Remove "cmd.exe" from the end if it exists REM Let's walk each path and test if cmd.exe, reg.exe, and where.exe exist there
if /i "!checkpath:~-7!" == "cmd.exe" ( set /a found=0
set "checkpath=!checkpath:~0,-7!" for /f "tokens=* delims=" %%i in ("!testpath!") do (
) REM Only continue if we haven't found it yet
REM Pad the end with a backslash if needed if not "%%i" == "" (
if not "!checkpath:~-1!" == "\" ( if !found! lss 1 (
set "checkpath=!checkpath!\" set "checkpath=%%i"
) REM Remove "cmd.exe" from the end if it exists
REM Let's see if cmd, reg, and where exist there - and set it if so if /i "!checkpath:~-7!" == "cmd.exe" (
if EXIST "!checkpath!cmd.exe" ( set "checkpath=!checkpath:~0,-7!"
if EXIST "!checkpath!reg.exe" ( )
if EXIST "!checkpath!where.exe" ( REM Pad the end with a backslash if needed
set /a found=1 if not "!checkpath:~-1!" == "\" (
set "ComSpec=!checkpath!cmd.exe" set "checkpath=!checkpath!\"
set "%~1=!checkpath!" )
) REM Let's see if cmd, reg, and where exist there - and set it if so
) if EXIST "!checkpath!cmd.exe" (
) if EXIST "!checkpath!reg.exe" (
) if EXIST "!checkpath!where.exe" (
) set /a found=1
) set "ComSpec=!checkpath!cmd.exe"
goto :EOF set "%~1=!checkpath!"
)
)
)
)
)
)
goto :EOF

View File

@ -3,7 +3,7 @@
# Get the curent directory, the script name # Get the curent directory, the script name
# and the script name with "py" substituted for the extension. # and the script name with "py" substituted for the extension.
args=( "$@" ) args=( "$@" )
dir="${0%/*}" dir="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
script="${0##*/}" script="${0##*/}"
target="${script%.*}.py" target="${script%.*}.py"