-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.bat
More file actions
427 lines (357 loc) · 11.8 KB
/
setup.bat
File metadata and controls
427 lines (357 loc) · 11.8 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
@echo off
setlocal EnableExtensions
cd /d "%~dp0"
set "PYTHON_CMD=python"
set "REQUIREMENTS_OK=0"
set "IS_ADMIN=0"
set "SETUP_FAILED=0"
call :print_header
call :is_admin
if errorlevel 1 (
echo [INFO] Administrator privileges are required for reliable setup.
call :relaunch_elevated
if errorlevel 1 (
echo [WARN] Continuing in non-admin mode. Admin-only actions will be skipped or attempted per-user.
set "IS_ADMIN=0"
) else (
echo [INFO] Elevated installer launched. Closing this window.
exit /b 0
)
set "IS_ADMIN=0"
) else (
set "IS_ADMIN=1"
)
call :refresh_path
call :ensure_cloudflared
if errorlevel 1 (
echo [WARN] cloudflared step did not complete successfully.
set "SETUP_FAILED=1"
)
call :ensure_python
if errorlevel 1 (
echo [WARN] Python step did not complete successfully.
set "SETUP_FAILED=1"
)
call :install_requirements
if errorlevel 1 (
echo [WARN] requirements step did not complete successfully.
set "SETUP_FAILED=1"
)
call :final_validation
if errorlevel 1 set "SETUP_FAILED=1"
if "%SETUP_FAILED%"=="1" goto :fatal
echo.
echo ================================
echo Setup completed successfully.
echo Environment is ready.
echo ================================
pause
exit /b 0
:fatal
echo.
echo ================================
echo Setup failed. See errors above.
echo ================================
pause
exit /b 1
:print_header
echo.
echo ================================
echo Robust environment installer
echo ================================
echo.
exit /b 0
:is_admin
net session >nul 2>&1
if errorlevel 1 exit /b 1
exit /b 0
:relaunch_elevated
powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%ComSpec%' -ArgumentList '/c ""%~f0""' -Verb RunAs" >nul 2>&1
if errorlevel 1 (
echo [ERROR] UAC elevation request was denied or failed.
exit /b 1
)
exit /b 0
:refresh_path
set "MACHINE_PATH="
set "USER_PATH="
for /f "usebackq delims=" %%A in (`powershell -NoProfile -ExecutionPolicy Bypass -Command "[Environment]::GetEnvironmentVariable('Path','Machine')"`) do set "MACHINE_PATH=%%A"
for /f "usebackq delims=" %%A in (`powershell -NoProfile -ExecutionPolicy Bypass -Command "[Environment]::GetEnvironmentVariable('Path','User')"`) do set "USER_PATH=%%A"
if defined MACHINE_PATH (
if defined USER_PATH (
set "PATH=%MACHINE_PATH%;%USER_PATH%"
) else (
set "PATH=%MACHINE_PATH%"
)
)
exit /b 0
:ensure_cloudflared
echo.
echo ================================
echo Ensuring cloudflared is installed
echo ================================
call :is_cloudflared_available
if not errorlevel 1 (
echo [INFO] cloudflared is already available.
exit /b 0
)
echo [INFO] cloudflared not detected. Trying method 1/3: winget.
call :install_cloudflared_winget
if not errorlevel 1 exit /b 0
echo [WARN] Method 1 failed. Trying method 2/3: Chocolatey.
call :install_cloudflared_choco
if not errorlevel 1 exit /b 0
echo [WARN] Method 2 failed. Trying method 3/3: direct download.
call :install_cloudflared_direct
if not errorlevel 1 exit /b 0
echo [ERROR] All cloudflared installation methods failed.
exit /b 1
:is_cloudflared_available
where cloudflared >nul 2>&1 || exit /b 1
cloudflared --version >nul 2>&1 || exit /b 1
exit /b 0
:install_cloudflared_winget
where winget >nul 2>&1 || exit /b 1
winget install -e --id Cloudflare.cloudflared --accept-source-agreements --accept-package-agreements --silent --disable-interactivity >nul 2>&1
if errorlevel 1 exit /b 1
call :refresh_path
call :is_cloudflared_available
exit /b %errorlevel%
:install_cloudflared_choco
where choco >nul 2>&1 || exit /b 1
choco install cloudflared -y --no-progress >nul 2>&1
if errorlevel 1 exit /b 1
call :refresh_path
call :is_cloudflared_available
exit /b %errorlevel%
:install_cloudflared_direct
set "CF_ASSET=cloudflared-windows-amd64.exe"
if /i "%PROCESSOR_ARCHITECTURE%"=="ARM64" set "CF_ASSET=cloudflared-windows-arm64.exe"
if /i "%PROCESSOR_ARCHITECTURE%"=="x86" set "CF_ASSET=cloudflared-windows-386.exe"
set "CF_DIR=%ProgramFiles%\cloudflared"
if "%IS_ADMIN%"=="0" set "CF_DIR=%LocalAppData%\Programs\cloudflared"
set "CF_EXE=%CF_DIR%\cloudflared.exe"
if not exist "%CF_DIR%" mkdir "%CF_DIR%" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Invoke-WebRequest -Uri 'https://github.com/cloudflare/cloudflared/releases/latest/download/%CF_ASSET%' -OutFile '%CF_EXE%' -UseBasicParsing -TimeoutSec 180; exit 0 } catch { exit 1 }" >nul 2>&1
if errorlevel 1 exit /b 1
if not exist "%CF_EXE%" exit /b 1
call :add_path "%CF_DIR%"
if errorlevel 1 exit /b 1
call :refresh_path
call :is_cloudflared_available
exit /b %errorlevel%
:ensure_python
echo.
echo ================================
echo Ensuring Python and pip are installed
echo ================================
call :is_python_available
if not errorlevel 1 (
echo [INFO] Python is already available.
set "PYTHON_CMD=python"
call :ensure_pip
exit /b %errorlevel%
)
echo [INFO] Python not detected. Trying method 1/3: winget.
call :install_python_winget
if not errorlevel 1 (
call :ensure_pip
exit /b %errorlevel%
)
echo [WARN] Method 1 failed. Trying method 2/3: Chocolatey.
call :install_python_choco
if not errorlevel 1 (
call :ensure_pip
exit /b %errorlevel%
)
echo [WARN] Method 2 failed. Trying method 3/3: official installer.
call :install_python_direct
if not errorlevel 1 (
call :ensure_pip
exit /b %errorlevel%
)
echo [ERROR] All Python installation methods failed.
exit /b 1
:is_python_available
python -c "import sys; print(sys.version)" >nul 2>&1 || exit /b 1
exit /b 0
:install_python_winget
where winget >nul 2>&1 || exit /b 1
set "PY_WINGET_SCOPE=machine"
if "%IS_ADMIN%"=="0" set "PY_WINGET_SCOPE=user"
winget install -e --id Python.Python.3.12 --scope %PY_WINGET_SCOPE% --accept-source-agreements --accept-package-agreements --silent --disable-interactivity >nul 2>&1
if errorlevel 1 exit /b 1
call :refresh_path
call :repair_python_path_if_needed
call :is_python_available
exit /b %errorlevel%
:install_python_choco
where choco >nul 2>&1 || exit /b 1
choco install python -y --no-progress >nul 2>&1
if errorlevel 1 exit /b 1
call :refresh_path
call :repair_python_path_if_needed
call :is_python_available
exit /b %errorlevel%
:install_python_direct
set "PY_VERSION=3.12.8"
set "PY_ARCH=amd64"
if /i "%PROCESSOR_ARCHITECTURE%"=="x86" set "PY_ARCH="
set "PY_INSTALLER=%TEMP%\python-%PY_VERSION%-%PY_ARCH%.exe"
if "%PY_ARCH%"=="" set "PY_INSTALLER=%TEMP%\python-%PY_VERSION%.exe"
set "PY_URL=https://www.python.org/ftp/python/%PY_VERSION%/python-%PY_VERSION%-%PY_ARCH%.exe"
if "%PY_ARCH%"=="" set "PY_URL=https://www.python.org/ftp/python/%PY_VERSION%/python-%PY_VERSION%.exe"
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Invoke-WebRequest -Uri '%PY_URL%' -OutFile '%PY_INSTALLER%' -UseBasicParsing -TimeoutSec 240; exit 0 } catch { exit 1 }" >nul 2>&1
if errorlevel 1 exit /b 1
if not exist "%PY_INSTALLER%" exit /b 1
set "PY_INSTALL_ARGS=/quiet InstallAllUsers=1 PrependPath=1 Include_pip=1 Include_test=0 Shortcuts=0"
if "%IS_ADMIN%"=="0" set "PY_INSTALL_ARGS=/quiet InstallAllUsers=0 PrependPath=1 Include_pip=1 Include_test=0 Shortcuts=0"
"%PY_INSTALLER%" %PY_INSTALL_ARGS% >nul 2>&1
if errorlevel 1 exit /b 1
call :refresh_path
call :repair_python_path_if_needed
call :is_python_available
exit /b %errorlevel%
:repair_python_path_if_needed
call :is_python_available
if not errorlevel 1 exit /b 0
for %%P in (
"%ProgramFiles%\Python312"
"%ProgramFiles%\Python311"
"%ProgramFiles%\Python310"
"%LocalAppData%\Programs\Python\Python312"
"%LocalAppData%\Programs\Python\Python311"
"%LocalAppData%\Programs\Python\Python310"
) do (
if exist "%%~P\python.exe" (
call :add_path "%%~P"
call :add_path "%%~P\Scripts"
)
)
call :refresh_path
call :is_python_available
if not errorlevel 1 exit /b 0
where py >nul 2>&1 || exit /b 1
for /f "usebackq delims=" %%X in (`py -3 -c "import sys; print(sys.executable)" 2^>nul`) do set "PY_FROM_LAUNCHER=%%X"
if not defined PY_FROM_LAUNCHER exit /b 1
for %%D in ("%PY_FROM_LAUNCHER%") do set "PY_DIR=%%~dpD"
if not exist "%PY_FROM_LAUNCHER%" exit /b 1
call :add_path "%PY_DIR%"
call :add_path "%PY_DIR%Scripts"
call :refresh_path
call :is_python_available
exit /b %errorlevel%
:add_path
if "%IS_ADMIN%"=="1" (
call :add_machine_path "%~1"
exit /b %errorlevel%
)
call :add_user_path "%~1"
exit /b %errorlevel%
:ensure_pip
python -m pip --version >nul 2>&1
if not errorlevel 1 exit /b 0
python -m ensurepip --upgrade >nul 2>&1
if errorlevel 1 (
echo [ERROR] pip is unavailable and ensurepip failed.
exit /b 1
)
python -m pip --version >nul 2>&1 || exit /b 1
exit /b 0
:add_machine_path
set "TARGET_PATH=%~1"
if "%TARGET_PATH%"=="" exit /b 1
if not exist "%TARGET_PATH%" exit /b 1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$d='%TARGET_PATH%'; $p=[Environment]::GetEnvironmentVariable('Path','Machine'); if([string]::IsNullOrWhiteSpace($p)){ $p=$d } else { $exists=$p.Split(';') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ieq $d }; if(-not $exists){ $p=($p.TrimEnd(';') + ';' + $d) } }; [Environment]::SetEnvironmentVariable('Path',$p,'Machine')" >nul 2>&1
if errorlevel 1 exit /b 1
exit /b 0
:add_user_path
set "TARGET_PATH=%~1"
if "%TARGET_PATH%"=="" exit /b 1
if not exist "%TARGET_PATH%" exit /b 1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$d='%TARGET_PATH%'; $p=[Environment]::GetEnvironmentVariable('Path','User'); if([string]::IsNullOrWhiteSpace($p)){ $p=$d } else { $exists=$p.Split(';') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ieq $d }; if(-not $exists){ $p=($p.TrimEnd(';') + ';' + $d) } }; [Environment]::SetEnvironmentVariable('Path',$p,'User')" >nul 2>&1
if errorlevel 1 exit /b 1
exit /b 0
:install_requirements
echo.
echo ================================
echo Installing Python requirements
echo ================================
if not exist requirements.txt (
echo [ERROR] requirements.txt not found in: %CD%
exit /b 1
)
set "TRY=1"
:retry_pip_upgrade
python -m pip install --upgrade pip >nul 2>&1
if not errorlevel 1 goto pip_upgrade_ok
if %TRY% GEQ 3 (
echo [ERROR] Failed to upgrade pip after 3 attempts.
exit /b 1
)
set /a TRY+=1
echo [WARN] pip upgrade failed. Retrying (%TRY%/3)...
timeout /t 3 /nobreak >nul
goto retry_pip_upgrade
:pip_upgrade_ok
set "TRY=1"
:retry_requirements
python -m pip install -r requirements.txt >nul 2>&1
if not errorlevel 1 goto requirements_ok
if %TRY% GEQ 3 (
echo [ERROR] Failed to install requirements after 3 attempts.
exit /b 1
)
set /a TRY+=1
echo [WARN] requirements install failed. Retrying (%TRY%/3)...
timeout /t 3 /nobreak >nul
goto retry_requirements
:requirements_ok
set "REQUIREMENTS_OK=1"
exit /b 0
:final_validation
echo.
echo ================================
echo Final validation
echo ================================
set "VALIDATION_FAILED=0"
call :is_cloudflared_available
if errorlevel 1 (
echo [ERROR] cloudflared is not callable.
set "VALIDATION_FAILED=1"
) else (
echo [OK] cloudflared is callable.
)
call :is_python_available
if errorlevel 1 (
echo [ERROR] python is not callable.
set "VALIDATION_FAILED=1"
) else (
echo [OK] python is callable.
)
python -m pip --version >nul 2>&1
if errorlevel 1 (
echo [ERROR] pip is not available.
set "VALIDATION_FAILED=1"
) else (
echo [OK] pip is available.
)
where python >nul 2>&1
if errorlevel 1 (
echo [ERROR] python is not visible on PATH.
set "VALIDATION_FAILED=1"
) else (
echo [OK] python is visible on PATH.
)
if "%REQUIREMENTS_OK%"=="1" (
echo [OK] requirements.txt dependencies installed.
) else (
echo [ERROR] requirements.txt dependencies were not confirmed as installed.
set "VALIDATION_FAILED=1"
)
if "%VALIDATION_FAILED%"=="1" (
echo [ERROR] Environment validation failed.
exit /b 1
)
echo [OK] Environment validation passed.
exit /b 0