|
157 | 157 | unset _setup_command
|
158 | 158 | unset _do_setup_atlas
|
159 | 159 |
|
| 160 | +# determine text editor to use for complicated edits to the activate script |
| 161 | +if [ -x "$(command -v ed)" ]; then |
| 162 | + # default to using 'ed' |
| 163 | + _text_editor="ed" |
| 164 | +elif [ -x "$(command -v vi)" ]; then |
| 165 | + # fall back to 'vi' |
| 166 | + _text_editor="vi" |
| 167 | +else |
| 168 | + echo "ERROR: Neither 'ed' nor 'vi' is installed. Please install one of them." |
| 169 | + exit 1 |
| 170 | +fi |
| 171 | + |
160 | 172 | _venv_name="${1:-venv}"
|
161 | 173 | if [ ! -d "${_venv_name}" ]; then
|
162 | 174 | printf "# Creating new Python virtual environment '%s'\n" "${_venv_name}"
|
@@ -289,53 +301,118 @@ EOT
|
289 | 301 | # block and inject the PYTHONPATH if statement block directly after it
|
290 | 302 | # (2 lines later).
|
291 | 303 | _RECOVER_OLD_PYTHONPATH_LINE="$(($(sed -n '\|unset _OLD_VIRTUAL_PYTHONHOME|=' "${_venv_full_path}"/bin/activate) + 2))"
|
292 |
| - ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 304 | + # FIXME: Make a cleaner implimentation |
| 305 | + if [ "${_text_editor}" == "ed" ]; then |
| 306 | + ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 307 | +${_RECOVER_OLD_PYTHONPATH_LINE}i |
| 308 | +${_RECOVER_OLD_PYTHONPATH} |
| 309 | +. |
| 310 | +wq |
| 311 | +EOF |
| 312 | + else |
| 313 | + # only supporting vi so don't need to check |
| 314 | + _vi_script=$(mktemp) |
| 315 | + cat <<EOF > "${_vi_script}" |
293 | 316 | ${_RECOVER_OLD_PYTHONPATH_LINE}i
|
294 | 317 | ${_RECOVER_OLD_PYTHONPATH}
|
295 | 318 | .
|
296 | 319 | wq
|
297 | 320 | EOF
|
| 321 | + vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}" |
| 322 | + fi |
298 | 323 |
|
299 | 324 | # Find the line number of the last line in deactivate's PYTHONHOME reset
|
300 | 325 | # if statement block and inject the PYTHONPATH reset if statement block directly
|
301 | 326 | # after it (2 lines later).
|
302 | 327 | _SET_PYTHONPATH_INSERT_LINE="$(($(sed -n '\| unset PYTHONHOME|=' "${_venv_full_path}"/bin/activate) + 2))"
|
303 |
| - ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 328 | + # FIXME: Make a cleaner implimentation |
| 329 | + if [ "${_text_editor}" == "ed" ]; then |
| 330 | + ed --silent "${_venv_full_path}/bin/activate" <<EOF |
304 | 331 | ${_SET_PYTHONPATH_INSERT_LINE}i
|
305 | 332 | ${_SET_PYTHONPATH}
|
306 | 333 | .
|
307 | 334 | wq
|
308 | 335 | EOF
|
| 336 | + else |
| 337 | + # only supporting vi so don't need to check |
| 338 | + _vi_script=$(mktemp) |
| 339 | + cat <<EOF > "${_vi_script}" |
| 340 | +${_SET_PYTHONPATH_INSERT_LINE}i |
| 341 | +${_SET_PYTHONPATH} |
| 342 | +. |
| 343 | +wq |
| 344 | +EOF |
| 345 | + vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}" |
| 346 | + fi |
309 | 347 |
|
310 | 348 | # Find the line number of the deactivate function and inject the cvmfs-venv-rebase directly after it
|
311 | 349 | # (1 line later).
|
312 | 350 | _RUN_REBASE_LINE="$(($(sed -n '\|deactivate ()|=' "${_venv_full_path}"/bin/activate) + 1))"
|
313 |
| - ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 351 | + # FIXME: Make a cleaner implimentation |
| 352 | + if [ "${_text_editor}" == "ed" ]; then |
| 353 | + ed --silent "${_venv_full_path}/bin/activate" <<EOF |
314 | 354 | ${_RUN_REBASE_LINE}i
|
315 | 355 | ${_RUN_REBASE}
|
316 | 356 | .
|
317 | 357 | wq
|
318 | 358 | EOF
|
| 359 | + else |
| 360 | + # only supporting vi so don't need to check |
| 361 | + _vi_script=$(mktemp) |
| 362 | + cat <<EOF > "${_vi_script}" |
| 363 | +${_RUN_REBASE_LINE}i |
| 364 | +${_RUN_REBASE} |
| 365 | +. |
| 366 | +wq |
| 367 | +EOF |
| 368 | + vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}" |
| 369 | + fi |
319 | 370 |
|
320 | 371 | # Find the line number of the unset -f deactivate line in deactivate's destructive unset
|
321 | 372 | # and inject the cvmfs-venv-rebase reset directly after it (1 line later).
|
322 | 373 | _DESCTRUCTIVE_UNSET_LINE="$(($(sed -n '\|unset -f deactivate|=' "${_venv_full_path}"/bin/activate) + 1))"
|
323 |
| - ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 374 | + # FIXME: Make a cleaner implimentation |
| 375 | + if [ "${_text_editor}" == "ed" ]; then |
| 376 | + ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 377 | +${_DESCTRUCTIVE_UNSET_LINE}i |
| 378 | +${_DESCTRUCTIVE_UNSET} |
| 379 | +. |
| 380 | +wq |
| 381 | +EOF |
| 382 | + else |
| 383 | + # only supporting vi so don't need to check |
| 384 | + _vi_script=$(mktemp) |
| 385 | + cat <<EOF > "${_vi_script}" |
324 | 386 | ${_DESCTRUCTIVE_UNSET_LINE}i
|
325 | 387 | ${_DESCTRUCTIVE_UNSET}
|
326 | 388 | .
|
327 | 389 | wq
|
328 | 390 | EOF
|
| 391 | + vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}" |
| 392 | + fi |
329 | 393 |
|
330 | 394 | # Find the line number of the unset -f cvmfs-venv-rebase line in deactivate's destructive unset
|
331 | 395 | # and inject the cvmfs-venv-rebase function directly after it (4 lines later).
|
332 | 396 | _CVMFS_VENV_REBASE_LINE="$(($(sed -n '\|unset -f cvmfs-venv-rebase|=' "${_venv_full_path}"/bin/activate) + 4))"
|
333 |
| - ed --silent "${_venv_full_path}/bin/activate" <<EOF |
| 397 | + # FIXME: Make a cleaner implimentation |
| 398 | + if [ "${_text_editor}" == "ed" ]; then |
| 399 | + ed --silent "${_venv_full_path}/bin/activate" <<EOF |
334 | 400 | ${_CVMFS_VENV_REBASE_LINE}i
|
335 | 401 | ${_CVMFS_VENV_REBASE}
|
336 | 402 | .
|
337 | 403 | wq
|
338 | 404 | EOF
|
| 405 | + else |
| 406 | + # only supporting vi so don't need to check |
| 407 | + _vi_script=$(mktemp) |
| 408 | + cat <<EOF > "${_vi_script}" |
| 409 | +${_CVMFS_VENV_REBASE_LINE}i |
| 410 | +${_CVMFS_VENV_REBASE} |
| 411 | +. |
| 412 | +wq |
| 413 | +EOF |
| 414 | + vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}" |
| 415 | + fi |
339 | 416 |
|
340 | 417 | unset _venv_full_path
|
341 | 418 |
|
@@ -380,3 +457,4 @@ unset _return_break
|
380 | 457 | unset _no_system_site_packages
|
381 | 458 | unset _no_update
|
382 | 459 | unset _no_uv
|
| 460 | +unset _text_editor |
0 commit comments