-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinitial_setting.bat
More file actions
378 lines (312 loc) · 10.6 KB
/
Copy pathinitial_setting.bat
File metadata and controls
378 lines (312 loc) · 10.6 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
@echo off
setlocal EnableExtensions EnableDelayedExpansion
if "%~1"=="--help" goto :help
if "%~1"=="-h" goto :help
if "%~1"=="/?" goto :help
if not "%~1"=="" (
echo [error] Unknown option: %~1
goto :fail
)
cd /d "%~dp0" || goto :fail
set "PROJECT_ROOT=%CD%"
set "RUNTIME_DIR=%PROJECT_ROOT%\.runtime"
set "RUNTIME_TEMP_DIR=%RUNTIME_DIR%\temp"
set "MATPLOTLIB_CONFIG_DIR=%RUNTIME_DIR%\matplotlib"
set "UV_DIR=%RUNTIME_DIR%\uv"
set "UV_EXE=%UV_DIR%\uv.exe"
set "UV_DOWNLOAD_DIR=%RUNTIME_DIR%\download"
set "UV_CACHE_DIR=%RUNTIME_DIR%\uv-cache"
set "UV_PYTHON_INSTALL_DIR=%RUNTIME_DIR%\python"
set "UV_PROJECT_ENVIRONMENT=%PROJECT_ROOT%\.venv"
set "PYTHON_VERSION_FILE=%PROJECT_ROOT%\.python-version"
set "REQUIREMENTS_FILE=%PROJECT_ROOT%\requirements.txt"
set "PYTHON_EXE=%UV_PROJECT_ENVIRONMENT%\Scripts\python.exe"
set "CURL_EXE=%SystemRoot%\System32\curl.exe"
set "TAR_EXE=%SystemRoot%\System32\tar.exe"
set "TEMP=%RUNTIME_TEMP_DIR%"
set "TMP=%RUNTIME_TEMP_DIR%"
set "UV_STATUS=not checked"
set "PYTHON_STATUS=not checked"
set "VENV_STATUS=not checked"
set "PIP_STATUS=not checked"
set "PACKAGE_STATUS=not checked"
set "FOLDER_STATUS=not checked"
call :load_settings || goto :fail
call :prepare_dirs || goto :fail
call :ensure_uv || goto :fail
call :ensure_python_runtime || goto :fail
call :ensure_venv || goto :fail
call :ensure_pip || goto :fail
call :install_requirements || goto :fail
call :prepare_project_folders || goto :fail
echo.
echo Initial setting completed.
echo Run analysis with:
echo .venv\Scripts\python.exe run_analysis.py
call :print_summary
goto :finish_success
:load_settings
if not exist "%PYTHON_VERSION_FILE%" (
echo [error] .python-version was not found.
exit /b 1
)
for /f "usebackq tokens=* delims=" %%A in ("%PYTHON_VERSION_FILE%") do (
if not defined PYTHON_VERSION set "PYTHON_VERSION=%%A"
)
if not defined PYTHON_VERSION (
echo [error] .python-version is empty.
exit /b 1
)
if not exist "%REQUIREMENTS_FILE%" (
echo [error] requirements.txt was not found.
exit /b 1
)
exit /b 0
:prepare_dirs
call :ensure_dir "%RUNTIME_DIR%" || exit /b 1
call :ensure_dir "%RUNTIME_TEMP_DIR%" || exit /b 1
call :ensure_dir "%MATPLOTLIB_CONFIG_DIR%" || exit /b 1
call :ensure_dir "%UV_CACHE_DIR%" || exit /b 1
call :ensure_dir "%UV_PYTHON_INSTALL_DIR%" || exit /b 1
exit /b 0
:ensure_uv
echo [1/6] Checking uv
call :detect_arch || exit /b 1
if not exist "%UV_EXE%" (
call :download_uv || exit /b 1
call :get_uv_version UV_VERSION_AFTER
set "UV_STATUS=installed !UV_VERSION_AFTER!"
exit /b 0
)
call :get_uv_version UV_VERSION_BEFORE
call :get_latest_uv_version
if errorlevel 1 (
echo Could not check the latest uv version. Using installed uv.
set "UV_STATUS=using !UV_VERSION_BEFORE!; latest check unavailable"
exit /b 0
)
call :compare_versions "!UV_VERSION_BEFORE!" "!UV_LATEST_VERSION!"
if "!VERSION_COMPARE!"=="0" (
set "UV_STATUS=already up to date: !UV_VERSION_BEFORE!"
exit /b 0
)
if "!VERSION_COMPARE!"=="1" (
set "UV_STATUS=using !UV_VERSION_BEFORE!; newer than latest release !UV_LATEST_VERSION!"
exit /b 0
)
call :download_uv || exit /b 1
call :get_uv_version UV_VERSION_AFTER
set "UV_STATUS=updated from !UV_VERSION_BEFORE! to !UV_VERSION_AFTER!"
exit /b 0
:download_uv
set "UV_DOWNLOAD_URL=https://github.com/astral-sh/uv/releases/latest/download/uv-%UV_ARCH%.zip"
set "UV_ZIP=%UV_DOWNLOAD_DIR%\uv-%UV_ARCH%.zip"
set "UV_EXTRACT_DIR=%UV_DOWNLOAD_DIR%\uv-%UV_ARCH%-%RANDOM%%RANDOM%"
if not exist "%CURL_EXE%" (
echo [error] curl.exe was not found. Windows 10 or newer is recommended.
exit /b 1
)
if not exist "%TAR_EXE%" (
echo [error] tar.exe was not found. Windows 10 or newer is recommended.
exit /b 1
)
call :ensure_dir "%UV_DIR%" || exit /b 1
call :ensure_dir "%UV_DOWNLOAD_DIR%" || exit /b 1
call :ensure_dir "%UV_EXTRACT_DIR%" || exit /b 1
echo Downloading uv from GitHub
"%CURL_EXE%" --fail --location --retry 3 --output "%UV_ZIP%" "%UV_DOWNLOAD_URL%"
if errorlevel 1 exit /b 1
"%TAR_EXE%" -xf "%UV_ZIP%" -C "%UV_EXTRACT_DIR%"
if errorlevel 1 exit /b 1
set "UV_FOUND="
for /r "%UV_EXTRACT_DIR%" %%F in (uv.exe) do if not defined UV_FOUND (
copy /y "%%F" "%UV_EXE%" >nul
set "UV_FOUND=1"
)
set "UVX_FOUND="
for /r "%UV_EXTRACT_DIR%" %%F in (uvx.exe) do if not defined UVX_FOUND (
copy /y "%%F" "%UV_DIR%\uvx.exe" >nul
set "UVX_FOUND=1"
)
del /q "%UV_ZIP%" >nul 2>nul
rmdir /s /q "%UV_EXTRACT_DIR%" >nul 2>nul
if not exist "%UV_EXE%" (
echo [error] uv.exe was not installed.
exit /b 1
)
exit /b 0
:detect_arch
set "HOST_ARCH=%PROCESSOR_ARCHITECTURE%"
if defined PROCESSOR_ARCHITEW6432 set "HOST_ARCH=%PROCESSOR_ARCHITEW6432%"
set "UV_ARCH="
if /I "%HOST_ARCH%"=="AMD64" set "UV_ARCH=x86_64-pc-windows-msvc"
if /I "%HOST_ARCH%"=="ARM64" set "UV_ARCH=aarch64-pc-windows-msvc"
if not defined UV_ARCH (
echo [error] Unsupported Windows architecture: %HOST_ARCH%
exit /b 1
)
exit /b 0
:get_latest_uv_version
set "UV_LATEST_URL="
set "UV_LATEST_VERSION="
if not exist "%CURL_EXE%" exit /b 1
for /f "tokens=* delims=" %%U in ('"%CURL_EXE%" --silent --location --output NUL --write-out "%%{url_effective}" "https://github.com/astral-sh/uv/releases/latest" 2^>nul') do set "UV_LATEST_URL=%%U"
if not defined UV_LATEST_URL exit /b 1
set "UV_LATEST_VERSION=!UV_LATEST_URL:*tag/=!"
if "!UV_LATEST_VERSION!"=="!UV_LATEST_URL!" exit /b 1
if /I "!UV_LATEST_VERSION:~0,1!"=="v" set "UV_LATEST_VERSION=!UV_LATEST_VERSION:~1!"
exit /b 0
:ensure_python_runtime
echo [2/6] Checking Python %PYTHON_VERSION%
call :get_managed_python_version PYTHON_VERSION_BEFORE
"%UV_EXE%" python install "%PYTHON_VERSION%" --managed-python --upgrade --no-bin
if errorlevel 1 (
if defined PYTHON_VERSION_BEFORE (
echo [warning] Python update check failed. Continuing with installed Python !PYTHON_VERSION_BEFORE!.
set "PYTHON_STATUS=using !PYTHON_VERSION_BEFORE!; update check failed"
exit /b 0
)
exit /b 1
)
call :get_managed_python_version PYTHON_VERSION_AFTER
if not defined PYTHON_VERSION_BEFORE (
set "PYTHON_STATUS=installed !PYTHON_VERSION_AFTER!"
) else if "!PYTHON_VERSION_BEFORE!"=="!PYTHON_VERSION_AFTER!" (
set "PYTHON_STATUS=already up to date: !PYTHON_VERSION_AFTER!"
) else (
set "PYTHON_STATUS=updated from !PYTHON_VERSION_BEFORE! to !PYTHON_VERSION_AFTER!"
)
exit /b 0
:ensure_venv
echo [3/6] Checking virtual environment
if exist "%PYTHON_EXE%" (
call :get_python_exe_version "%PYTHON_EXE%" VENV_VERSION
set "VENV_STATUS=reused .venv with Python !VENV_VERSION!"
echo !VENV_VERSION! | findstr /b /c:"%PYTHON_VERSION%" >nul
if errorlevel 1 (
echo [warning] Existing .venv uses Python !VENV_VERSION!, but .python-version requests %PYTHON_VERSION%.
echo [warning] This script does not delete existing environments. Delete .venv and run again if needed.
)
exit /b 0
)
if exist "%UV_PROJECT_ENVIRONMENT%" (
echo Existing .venv is incomplete. Repairing without deleting it.
"%UV_EXE%" venv --seed --allow-existing --python "%PYTHON_VERSION%" --managed-python "%UV_PROJECT_ENVIRONMENT%"
) else (
"%UV_EXE%" venv --seed --python "%PYTHON_VERSION%" --managed-python "%UV_PROJECT_ENVIRONMENT%"
)
if errorlevel 1 exit /b 1
call :get_python_exe_version "%PYTHON_EXE%" VENV_VERSION
set "VENV_STATUS=created .venv with Python !VENV_VERSION!"
exit /b 0
:ensure_pip
echo [4/6] Checking pip
"%PYTHON_EXE%" -m pip --version >nul 2>nul
if not errorlevel 1 (
for /f "tokens=2" %%V in ('"%PYTHON_EXE%" -m pip --version 2^>nul') do set "PIP_VERSION=%%V"
set "PIP_STATUS=already installed: pip !PIP_VERSION!"
exit /b 0
)
"%PYTHON_EXE%" -m ensurepip --upgrade
if errorlevel 1 exit /b 1
for /f "tokens=2" %%V in ('"%PYTHON_EXE%" -m pip --version 2^>nul') do set "PIP_VERSION=%%V"
set "PIP_STATUS=installed pip !PIP_VERSION!"
exit /b 0
:install_requirements
echo [5/6] Checking packages from requirements.txt
"%UV_EXE%" pip install --strict --python "%PYTHON_EXE%" -r "%REQUIREMENTS_FILE%"
if errorlevel 1 exit /b 1
set "PACKAGE_STATUS=requirements.txt satisfied; uv output above shows any installs or updates"
exit /b 0
:prepare_project_folders
echo [6/6] Preparing project folders
call :ensure_dir "%PROJECT_ROOT%\input" || exit /b 1
call :ensure_dir "%PROJECT_ROOT%\output" || exit /b 1
set "FOLDER_STATUS=input and output folders are ready"
exit /b 0
:print_summary
echo.
echo Summary:
echo uv: %UV_STATUS%
echo Python runtime: %PYTHON_STATUS%
echo Virtual environment: %VENV_STATUS%
echo pip: %PIP_STATUS%
echo Packages: %PACKAGE_STATUS%
echo Project folders: %FOLDER_STATUS%
echo.
echo Requirements:
for /f "usebackq tokens=* delims=" %%R in ("%REQUIREMENTS_FILE%") do (
if not "%%R"=="" echo %%R
)
exit /b 0
:get_uv_version
set "%~1="
if exist "%UV_EXE%" (
for /f "tokens=2" %%V in ('"%UV_EXE%" self version 2^>nul') do set "%~1=%%V"
)
if not defined %~1 set "%~1=unknown"
exit /b 0
:get_managed_python_version
set "%~1="
for /d %%D in ("%UV_PYTHON_INSTALL_DIR%\cpython-%PYTHON_VERSION%*-windows-*-none") do (
if exist "%%~fD\python.exe" call :get_python_exe_version "%%~fD\python.exe" %~1
)
exit /b 0
:compare_versions
set "VERSION_COMPARE=0"
set "A1=0"
set "A2=0"
set "A3=0"
set "B1=0"
set "B2=0"
set "B3=0"
for /f "tokens=1-3 delims=.vV-" %%A in ("%~1") do (
if not "%%A"=="" set "A1=%%A"
if not "%%B"=="" set "A2=%%B"
if not "%%C"=="" set "A3=%%C"
)
for /f "tokens=1-3 delims=.vV-" %%A in ("%~2") do (
if not "%%A"=="" set "B1=%%A"
if not "%%B"=="" set "B2=%%B"
if not "%%C"=="" set "B3=%%C"
)
if !A1! LSS !B1! set "VERSION_COMPARE=-1" & exit /b 0
if !A1! GTR !B1! set "VERSION_COMPARE=1" & exit /b 0
if !A2! LSS !B2! set "VERSION_COMPARE=-1" & exit /b 0
if !A2! GTR !B2! set "VERSION_COMPARE=1" & exit /b 0
if !A3! LSS !B3! set "VERSION_COMPARE=-1" & exit /b 0
if !A3! GTR !B3! set "VERSION_COMPARE=1" & exit /b 0
exit /b 0
:get_python_exe_version
set "%~2="
if exist "%~1" (
for /f "tokens=2" %%V in ('"%~1" --version 2^>nul') do set "%~2=%%V"
)
if not defined %~2 set "%~2=unknown"
exit /b 0
:ensure_dir
if not exist "%~1" mkdir "%~1"
exit /b %ERRORLEVEL%
:help
echo Usage: initial_setting.bat
echo.
echo Creates or updates this project's local Windows runtime.
echo - Missing uv, Python, pip, and packages are installed.
echo - Existing uv and managed Python are updated when a newer version is available.
echo - Existing .venv is reused; it is never deleted by this script.
echo - Package versions are adjusted to satisfy requirements.txt.
echo - No administrator permission or global PATH change is required.
goto :finish_success
:fail
echo.
echo Initial setting failed.
call :print_summary
goto :finish_fail
:finish_success
echo.
pause
exit /b 0
:finish_fail
echo.
pause
exit /b 1