forked from corpnewt/gibMacOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeInstall.bat
204 lines (188 loc) · 5.87 KB
/
MakeInstall.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
:::::::::::::::::::::::::::::::::::::::::
:: 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
set "script_name=%~n0.py"
set "thisDir=%~dp0"
set /a tried=0
set "toask=yes"
goto checkscript
:checkscript
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
)
goto checkpy
:checkpy
python -V > NUL 2>&1
if not "!errorlevel!" == "0" (
if %tried% lss 1 (
if /i "!toask!"=="yes" (
REM Better ask permission first
goto askinstall
) else (
goto installpy
)
) 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
: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
: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/...
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" (
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
powershell -command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object System.Net.WebClient).DownloadFile('!url!','%TEMP%\pyinstall.exe')"
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!"
)
goto checkpy
exit /b
:runscript
REM Python found
cls
set "args=%*"
set "args=!args:"=!"
if "!args!"=="" (
python "!thisDir!!script_name!"
) else (
python "!thisDir!!script_name!" %*
)
goto :EOF