-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_environment.bat
More file actions
374 lines (327 loc) · 13.5 KB
/
setup_environment.bat
File metadata and controls
374 lines (327 loc) · 13.5 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
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:: ============================================================================
:: SimOR Environment Setup Script
:: ============================================================================
:: This script sets up the computing environment for the SimOR project.
:: It installs UV (if needed), clones and builds ActivitySim, optionally
:: installs the sandag_parking repo, and configures Visum Python packages.
::
:: All cloned repos and virtual environments are placed in the
:: ext_dependencies subfolder within this SimOR repo.
:: ============================================================================
:: ---------------------------------------------------------------------------
:: User-configurable settings
:: ---------------------------------------------------------------------------
:: Set this to the folder containing Visum's bundled Python interpreter.
:: IMPORTANT: Do not include a trailing backslash at the end of this path.
:: Example: C:\Program Files\PTV Vision\PTV Visum 2026\Exe\Python
SET "VISUM_PYTHON_DIR=C:\Program Files\PTV Vision\PTV Visum 2026\Exe\Python"
:: Set to Y to clone and install sandag_parking (oregon_metro branch), N to skip
:: (optional dependency, only needed if you want to regenerate parking cost data)
SET "INSTALL_PARKING=N"
:: ---------------------------------------------------------------------------
:: ---------------------------------------------------------------------------
:: Resolve the base directory (the root of this SimOR repo)
:: ---------------------------------------------------------------------------
SET "BASE_DIR=%~dp0"
:: Remove trailing backslash
IF "%BASE_DIR:~-1%"=="\" SET "BASE_DIR=%BASE_DIR:~0,-1%"
ECHO ============================================================
ECHO SimOR Environment Setup
ECHO ============================================================
ECHO.
ECHO Base directory: %BASE_DIR%
ECHO.
:: ---------------------------------------------------------------------------
:: Create ext_dependencies folder for cloned repos and environments
:: ---------------------------------------------------------------------------
SET "EXT_DIR=%BASE_DIR%\ext_dependencies"
IF NOT EXIST "%EXT_DIR%" (
MKDIR "%EXT_DIR%"
ECHO Created ext_dependencies folder.
)
:: ============================================================================
:: STEP 1 – Ensure UV is installed
:: ============================================================================
ECHO [1/5] Checking for UV package manager...
where uv >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO UV not found. Installing UV...
powershell -ExecutionPolicy Bypass -Command "irm https://astral.sh/uv/install.ps1 | iex"
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: Failed to install UV.
GOTO :ERROR_EXIT
)
:: Refresh PATH so uv is available in this session
FOR /F "tokens=*" %%A IN ('powershell -Command "[System.Environment]::GetEnvironmentVariable('Path','User')"') DO SET "PATH=%%A;%PATH%"
where uv >nul 2>&1
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: UV installed but not found on PATH. Please restart your terminal and re-run this script.
GOTO :ERROR_EXIT
)
ECHO UV installed successfully.
) ELSE (
ECHO UV is already installed.
)
ECHO.
:: ============================================================================
:: STEP 1b – Ensure Git is available
:: ============================================================================
ECHO Checking for Git...
where git >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO Git not found on PATH. Searching common install locations...
SET "GIT_FOUND="
FOR %%G IN (
"%ProgramFiles%\Git\cmd"
"%ProgramFiles(x86)%\Git\cmd"
"%LOCALAPPDATA%\Programs\Git\cmd"
"%ProgramFiles%\Git\bin"
) DO (
IF EXIST "%%~G\git.exe" (
SET "PATH=%%~G;!PATH!"
SET "GIT_FOUND=1"
)
)
IF NOT DEFINED GIT_FOUND (
ECHO ERROR: Git is not installed or not found.
ECHO Please install Git from https://git-scm.com/ and re-run this script.
GOTO :ERROR_EXIT
)
ECHO Found Git and added to PATH for this session.
) ELSE (
ECHO Git is available.
)
ECHO.
:: ============================================================================
:: STEP 2 – Clone / update ActivitySim and create its virtual environment
:: ============================================================================
ECHO [2/5] Setting up ActivitySim (SimOR_pnr branch)...
SET "ACTIVITYSIM_DIR=%EXT_DIR%\activitysim"
IF EXIST "%ACTIVITYSIM_DIR%\.git" (
ECHO ActivitySim repo already exists. Pulling latest changes...
pushd "%ACTIVITYSIM_DIR%"
git checkout SimOR_pnr
git pull
popd
) ELSE (
ECHO Cloning ActivitySim ^(SimOR_pnr branch^)...
pushd "%EXT_DIR%"
git clone --branch SimOR_pnr https://github.com/RSGInc/activitysim.git
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: Failed to clone ActivitySim.
popd
GOTO :ERROR_EXIT
)
popd
)
:: Build the environment using the uv lock file
ECHO Installing ActivitySim dependencies via UV...
pushd "%ACTIVITYSIM_DIR%"
uv sync --frozen --link-mode copy
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: uv sync failed for ActivitySim.
popd
GOTO :ERROR_EXIT
)
uv pip install -e . --no-deps --link-mode copy
popd
:: Resolve the absolute path to the venv Python
FOR /F "delims=" %%P IN ('powershell -Command "(Resolve-Path '%ACTIVITYSIM_DIR%\.venv\Scripts\python.exe').Path"') DO SET "PYTHON_ACTIVITYSIM=%%P"
IF NOT EXIST "%PYTHON_ACTIVITYSIM%" (
ECHO ERROR: Could not find Python at %ACTIVITYSIM_DIR%\.venv\Scripts\python.exe
GOTO :ERROR_EXIT
)
ECHO PYTHON_ACTIVITYSIM = %PYTHON_ACTIVITYSIM%
ECHO.
:: ============================================================================
:: STEP 3 – Visum Python packages
:: ============================================================================
ECHO [3/5] Setting up Visum Python packages...
SET "VISUM_PACKAGE_STATUS=not checked"
SET "VISUM_PYTHON_EXE=%VISUM_PYTHON_DIR%\python.exe"
IF NOT EXIST "%VISUM_PYTHON_EXE%" (
ECHO WARNING: Visum Python not found at:
ECHO %VISUM_PYTHON_DIR%
ECHO Please edit VISUM_PYTHON_DIR in this script to point to your Visum 2026 Python folder.
SET "VISUM_PACKAGE_STATUS=skipped (Visum Python not found)"
GOTO :VISUM_DONE
)
:: Resolve site-packages path via temp file (handles spaces in paths)
SET "VISUM_SITE_PACKAGES="
SET "VISUM_SITE_FILE=%TEMP%\simor_visum_site_packages.txt"
"%VISUM_PYTHON_EXE%" -c "import sysconfig; print(sysconfig.get_paths().get('purelib',''))" > "%VISUM_SITE_FILE%" 2>nul
IF EXIST "%VISUM_SITE_FILE%" (
SET /P VISUM_SITE_PACKAGES=<"%VISUM_SITE_FILE%"
DEL /Q "%VISUM_SITE_FILE%" >nul 2>&1
)
IF NOT DEFINED VISUM_SITE_PACKAGES (
ECHO WARNING: Could not resolve Visum site-packages path.
SET "VISUM_PACKAGE_STATUS=skipped (could not resolve site-packages)"
GOTO :VISUM_DONE
)
ECHO Resolved Visum site-packages path:
ECHO !VISUM_SITE_PACKAGES!
:: Check if packages are already importable
SET "PYTHONNOUSERSITE=1"
"%VISUM_PYTHON_EXE%" -c "import tables,openmatrix,yaml,pandas,scipy; assert pandas.__version__.startswith('2.3'); assert scipy.__version__.startswith('1.16')" >nul 2>&1
IF !ERRORLEVEL! EQU 0 (
ECHO Required Visum packages are already available. Skipping install.
SET "VISUM_PACKAGE_STATUS=already available"
GOTO :VISUM_DONE
)
:: Ensure site-packages directory exists
IF NOT EXIST "!VISUM_SITE_PACKAGES!" (
MKDIR "!VISUM_SITE_PACKAGES!" >nul 2>&1
IF !ERRORLEVEL! NEQ 0 (
ECHO WARNING: Cannot create directory:
ECHO !VISUM_SITE_PACKAGES!
ECHO Re-run as Administrator or ask IT to install these packages.
SET "VISUM_PACKAGE_STATUS=missing packages, no write access"
GOTO :VISUM_DONE
)
)
:: Test write access
>"!VISUM_SITE_PACKAGES!\.__simor_write_test__.tmp" ECHO write-test 2>nul
IF NOT EXIST "!VISUM_SITE_PACKAGES!\.__simor_write_test__.tmp" (
ECHO WARNING: Cannot write to:
ECHO !VISUM_SITE_PACKAGES!
ECHO Re-run as Administrator or ask IT to install these packages.
SET "VISUM_PACKAGE_STATUS=missing packages, no write access"
GOTO :VISUM_DONE
)
DEL /Q "!VISUM_SITE_PACKAGES!\.__simor_write_test__.tmp" >nul 2>&1
:: Install packages (--isolated avoids user-level config redirecting to user site-packages)
ECHO Installing tables, openmatrix, pyyaml, pandas 2.3.x, and scipy 1.16.x into:
ECHO !VISUM_SITE_PACKAGES!
"%VISUM_PYTHON_EXE%" -m pip install --isolated --upgrade tables openmatrix pyyaml pandas==2.3.* scipy==1.16.* --target "!VISUM_SITE_PACKAGES!"
IF !ERRORLEVEL! NEQ 0 (
ECHO WARNING: Failed to install one or more Visum Python packages.
SET "VISUM_PACKAGE_STATUS=install failed"
) ELSE (
ECHO Visum packages installed successfully.
SET "VISUM_PACKAGE_STATUS=installed/updated"
)
:VISUM_DONE
ECHO.
:: ============================================================================
:: STEP 4 – MAZ skimming Python environment
:: ============================================================================
ECHO [4/5] Setting up MAZ skimming Python environment...
SET "MAZ_SKIM_DIR=%EXT_DIR%\maz_skimming"
ECHO Installing MAZ skimming dependencies via UV...
pushd "%MAZ_SKIM_DIR%"
uv sync --frozen --link-mode copy
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: uv sync failed for MAZ skimming environment.
popd
GOTO :ERROR_EXIT
)
popd
:: Resolve the absolute path to the venv Python
FOR /F "delims=" %%P IN ('powershell -Command "(Resolve-Path '%MAZ_SKIM_DIR%\.venv\Scripts\python.exe').Path"') DO SET "PYTHON_MAZ_SKIMMING=%%P"
IF NOT EXIST "%PYTHON_MAZ_SKIMMING%" (
ECHO ERROR: Could not find Python at %MAZ_SKIM_DIR%\.venv\Scripts\python.exe
GOTO :ERROR_EXIT
)
ECHO PYTHON_MAZ_SKIMMING = %PYTHON_MAZ_SKIMMING%
ECHO.
:: ============================================================================
:: STEP 5 – (Optional) Clone / update sandag_parking
:: ============================================================================
IF /I "%INSTALL_PARKING%"=="Y" (
ECHO Setting up sandag_parking ^(oregon_metro branch^)...
SET "PARKING_DIR=%EXT_DIR%\sandag_parking"
IF EXIST "!PARKING_DIR!\.git" (
ECHO sandag_parking repo already exists. Pulling latest changes...
pushd "!PARKING_DIR!"
git checkout oregon_metro
git pull
popd
) ELSE (
ECHO Cloning sandag_parking ^(oregon_metro branch^)...
pushd "%EXT_DIR%"
git clone --branch oregon_metro https://github.com/RSGInc/sandag_parking.git
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: Failed to clone sandag_parking.
popd
GOTO :ERROR_EXIT
)
popd
)
:: Build the environment using the uv lock file
ECHO Installing sandag_parking dependencies via UV...
pushd "!PARKING_DIR!"
uv sync --frozen --link-mode copy
IF !ERRORLEVEL! NEQ 0 (
ECHO ERROR: uv sync failed for sandag_parking.
popd
GOTO :ERROR_EXIT
)
popd
:: Resolve the absolute path to the venv Python
FOR /F "delims=" %%P IN ('powershell -Command "(Resolve-Path '!PARKING_DIR!\.venv\Scripts\python.exe').Path"') DO SET "PYTHON_PARKING=%%P"
IF NOT EXIST "!PYTHON_PARKING!" (
ECHO ERROR: Could not find Python at !PARKING_DIR!\.venv\Scripts\python.exe
GOTO :ERROR_EXIT
)
ECHO PYTHON_PARKING = !PYTHON_PARKING!
) ELSE (
:: Even if not cloning, set the variable if the repo already exists
SET "PARKING_DIR=%EXT_DIR%\sandag_parking"
IF EXIST "!PARKING_DIR!\.venv\Scripts\python.exe" (
FOR /F "delims=" %%P IN ('powershell -Command "(Resolve-Path '!PARKING_DIR!\.venv\Scripts\python.exe').Path"') DO SET "PYTHON_PARKING=%%P"
ECHO Existing sandag_parking environment found.
ECHO PYTHON_PARKING = !PYTHON_PARKING!
) ELSE (
SET "PYTHON_PARKING="
ECHO sandag_parking skipped.
)
)
ECHO.
:: Set PYTHON_VISUM as the full path to the Visum Python executable
IF EXIST "%VISUM_PYTHON_DIR%\python.exe" (
SET "PYTHON_VISUM=%VISUM_PYTHON_DIR%\python.exe"
) ELSE (
SET "PYTHON_VISUM="
)
:: ============================================================================
:: Summary
:: ============================================================================
ECHO ============================================================
ECHO Environment Setup Complete
ECHO ============================================================
ECHO.
ECHO PYTHON_ACTIVITYSIM = %PYTHON_ACTIVITYSIM%
ECHO PYTHON_MAZ_SKIMMING = %PYTHON_MAZ_SKIMMING%
ECHO PYTHON_VISUM = %PYTHON_VISUM%
ECHO VISUM_PACKAGES = %VISUM_PACKAGE_STATUS%
IF DEFINED PYTHON_PARKING (
ECHO PYTHON_PARKING = !PYTHON_PARKING!
) ELSE (
ECHO PYTHON_PARKING = ^(not set^)
)
ECHO.
ECHO ============================================================
:: ---------------------------------------------------------------------------
:: Export variables to the caller's scope (survives ENDLOCAL)
:: ---------------------------------------------------------------------------
ENDLOCAL & (
SET "PYTHON_ACTIVITYSIM=%PYTHON_ACTIVITYSIM%"
SET "PYTHON_MAZ_SKIMMING=%PYTHON_MAZ_SKIMMING%"
SET "PYTHON_VISUM=%PYTHON_VISUM%"
SET "PYTHON_PARKING=%PYTHON_PARKING%"
SET "VISUM_PACKAGE_STATUS=%VISUM_PACKAGE_STATUS%"
SET "EXT_DIR=%EXT_DIR%"
SET "BASE_DIR=%BASE_DIR%"
SET "PATH=%PATH%"
)
GOTO :EOF
:ERROR_EXIT
ECHO.
ECHO ============================================================
ECHO Setup failed. See errors above.
ECHO ============================================================
ENDLOCAL
EXIT /B 1