Skip to content

Commit b44a482

Browse files
fix: Add support for 'vi' if 'ed' isn't installed (#53)
* As a full text editor is useful for writing the complicated multi-line patches into the 'activate' script, fall back to using 'vi' if 'ed' is not installed. * Note that 'ed' or 'vi' works in the README.
1 parent b098151 commit b44a482

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Required-by: cabinetry, coffea, servicex, uproot_browser
182182

183183
A full listing of all programs used outside of Bash shell builtins are:
184184
* `cat`
185-
* `ed`
185+
* `ed` or `vi`
186186
* `find`
187187
* `readlink`
188188
* `sed`

cvmfs-venv.sh

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ fi
157157
unset _setup_command
158158
unset _do_setup_atlas
159159

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+
160172
_venv_name="${1:-venv}"
161173
if [ ! -d "${_venv_name}" ]; then
162174
printf "# Creating new Python virtual environment '%s'\n" "${_venv_name}"
@@ -289,53 +301,118 @@ EOT
289301
# block and inject the PYTHONPATH if statement block directly after it
290302
# (2 lines later).
291303
_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}"
293316
${_RECOVER_OLD_PYTHONPATH_LINE}i
294317
${_RECOVER_OLD_PYTHONPATH}
295318
.
296319
wq
297320
EOF
321+
vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}"
322+
fi
298323

299324
# Find the line number of the last line in deactivate's PYTHONHOME reset
300325
# if statement block and inject the PYTHONPATH reset if statement block directly
301326
# after it (2 lines later).
302327
_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
304331
${_SET_PYTHONPATH_INSERT_LINE}i
305332
${_SET_PYTHONPATH}
306333
.
307334
wq
308335
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
309347

310348
# Find the line number of the deactivate function and inject the cvmfs-venv-rebase directly after it
311349
# (1 line later).
312350
_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
314354
${_RUN_REBASE_LINE}i
315355
${_RUN_REBASE}
316356
.
317357
wq
318358
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
319370

320371
# Find the line number of the unset -f deactivate line in deactivate's destructive unset
321372
# and inject the cvmfs-venv-rebase reset directly after it (1 line later).
322373
_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}"
324386
${_DESCTRUCTIVE_UNSET_LINE}i
325387
${_DESCTRUCTIVE_UNSET}
326388
.
327389
wq
328390
EOF
391+
vi -es "${_venv_full_path}/bin/activate" < "${_vi_script}"
392+
fi
329393

330394
# Find the line number of the unset -f cvmfs-venv-rebase line in deactivate's destructive unset
331395
# and inject the cvmfs-venv-rebase function directly after it (4 lines later).
332396
_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
334400
${_CVMFS_VENV_REBASE_LINE}i
335401
${_CVMFS_VENV_REBASE}
336402
.
337403
wq
338404
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
339416

340417
unset _venv_full_path
341418

@@ -380,3 +457,4 @@ unset _return_break
380457
unset _no_system_site_packages
381458
unset _no_update
382459
unset _no_uv
460+
unset _text_editor

0 commit comments

Comments
 (0)