From b405c5e5c5eaef77d049f2920e41ff6efef868fc Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos <ankostis@gmail.com> Date: Tue, 17 May 2022 20:42:30 +0300 Subject: [PATCH] FEAT(SETUP) extract CHECK_BEFORE_ACTION funcs; > + FEAT: All messages explain how to re-run with effecting changes. + REFACT: x3 utility functions emulating GnuMake. + refact: split sub-actions into separate ones, now that checks are cheaper. --- setup-account.sh | 193 ++++++++++++++++++++++++++++------------------- 1 file changed, 115 insertions(+), 78 deletions(-) diff --git a/setup-account.sh b/setup-account.sh index 80f361b..a02d5a0 100755 --- a/setup-account.sh +++ b/setup-account.sh @@ -5,32 +5,65 @@ set -e +function _short { + ## Return vars relative to PWD, prefixed with "./" + # as conda env needs + local path=$(realpath -m --relative-base="$PWD" "$1") + ## Does result start with alpha-chars (eg dirname)?. + if [[ "/." != *"${path::1}"* ]]; then + echo "./$path" + else + echo "$path" + fi +} + mydir=$(realpath "${0%/*}") cd "$mydir" mycmd="${0##*/}" -conda_env_prefix="$mydir/env" -conda_env_prefix_rel="./env" -ipython_startup_ustore_vars="$HOME/.ipython/profile_default/startup/50-ustor-vars.py" +conda_env_prefix=$(_short "$mydir/env") function _mute { "${@}" &> /dev/null } function _log { - echo -n "${FUNCNAME[1]}: " - echo "${@}" -} -function _log2 { - echo -n "${FUNCNAME[2]}: " + echo -n "${FUNCNAME[${lognstack:-1}]}: " echo "${@}" } -function upd20_inputrc { - if _mute grep -v ':history-search-forward' ~/.inputrc; then - _log "already ok" +function _cmd_before_update { + local lognstack=$(( ${lognstack:-1} + 1)) + if "${@}"; then + _log -e "${notrun_msg:-already ok}" + return 1 else - _log -n "updating..." - cat >> ~/.inputrc <<- "EOF" + _log -en "${running_msg:-updating...}" + return 0 # true, to run if-body updating stuff. + fi +} + +function _exists_before_update { + local lognstack=$(( ${lognstack:-1} + 1)) + local check_file="$1" + local notrun_msg="${notrun_msg:-already ok; + 'rm $(_short $check_file)' and re-run, to effect change.}" + local running_msg="${running_msg:-updating '$check_file'...}" + _cmd_before_update [ -e "$check_file" ] +} + +function _grep_before_update { + local lognstack=$(( ${lognstack:-1} + 1)) + local check_file="$1" check_text="$2" + local notrun_msg="${notrun_msg:-already ok; + drop lines containing '$check_file::$check_text' and re-run, to effect change.}" + local running_msg="${running_msg:-updating '$check_file'...}" + _cmd_before_update _mute grep "$check_text" "$check_file" +} + +function upd10_inputrc { + local check_file="$HOME/.inputrc" + if _exists_before_update "$check_file"; then + cat >> "$check_file" <<- "EOF" $include /etc/inputrc ## arrow up @@ -45,13 +78,11 @@ EOF } function upd20_bashrc_git_completions { - if _mute grep "/usr/share/bash-completion/completions/git" ~/.bashrc ; then - _log "already ok" - else - _log -n "updating..." - cat >> ~/.bashrc <<- "EOF" + local check_file="$HOME/.bashrc" + if _grep_before_update "$check_file" "/usr/share/bash-completion/completions/git"; then + cat >> "$check_file" <<- "EOF" -## Enable git completions +## UDS: Enable git completions source /usr/share/bash-completion/completions/git EOF @@ -59,16 +90,14 @@ EOF fi } -function upd20_bashrc_history { - if _mute grep "PROMPT_COMMAND=\"history -a\"" ~/.bashrc ; then - _log "already ok" - else - _log -n "updating..." - cat >> ~/.bashrc <<- "EOF" +function upd20_bashrc_options { + local check_file="$HOME/.bashrc" + if _grep_before_update "$check_file" "## UDS SHELL OPTIONS"; then + cat >> "$check_file" <<- "EOF" #### -## HISTORY CONTROL: -## (more options may lie above)) +## UDS SHELL OPTIONS +## (contradicting options may lie above/below) ## ## Save all lines of a multiple-line command in the same history entry. shopt -s cmdhist @@ -91,13 +120,11 @@ EOF } function upd20_bashrc_ustore_vars { - if _mute grep "export USTORE=" ~/.bashrc; then - _log "already ok" - else - _log -n "updating..." - cat >> ~/.bashrc <<- EOF + local check_file="$HOME/.bashrc" + if _grep_before_update "$check_file" "## UDS: BDAP Storage locations"; then + cat >> "$check_file" <<- EOF -## BDAP Storage locations +## UDS: BDAP Storage locations export PSTORE="/eos/jeodpp/data/projects/LEGENT" export USTORE="/eos/jeodpp/home/users/${USER}" export UTRANS="/eos/jeodpp/home/users/${USER}/transfer" @@ -109,11 +136,9 @@ EOF } function upd20_bash_aliases { - if _mute grep 'alias lg=' ~/.bash_aliases; then - _log "already ok" - else - _log -n "updating..." - cat >> ~/.bash_aliases <<- "EOF" + local check_file="$HOME/.bash_aliases" + if _grep_before_update "$check_file" "alias lg="; then + cat >> "$check_file" <<- "EOF" ## GIT ALIASES alias lg='git lg' @@ -136,28 +161,27 @@ EOF fi } -function upd30_git_user { - if _mute git config --global --get user.name; then - _log "user.name already ok" - else - _log -n "" - read -e -p "+++Enter your git AUTHOR-NAME: " inp +function upd30_git_user_name { + local running_msg="+++Enter your git AUTHOR-NAME: " + if _cmd_before_update _mute git config --global --get user.name; then + + read -e inp git config --global user.name "inp" echo "UPDATED" fi +} - if _mute git config --global --get user.email; then - _log "user.email already ok" - else - _log -n "" - read -e -p "+++Enter your git AUTHOR-EMAIL: " inp +function upd30_git_user_email { + local running_msg="+++Enter your git AUTHOR-EMAIL: " + if _cmd_before_update _mute git config --global --get user.email; then + read -e inp git config --global user.email "inp" _log "UPDATED" fi } function upd20_git_aliases { - if _mute git config --global --get alias.lg; then + if _mute git config --global --get alias.lg; then _log "already ok" else _log -n "updating..." @@ -184,12 +208,10 @@ function upd20_git_aliases { } function upd20_ipython_ustore_vars { - if [ -f "$ipython_startup_ustore_vars" ]; then - _log "already ok" - else - _log -n "updating..." - mkdir -p $(dirname "$ipython_startup_ustore_vars") - cat > "$ipython_startup_ustore_vars" <<- EOF + local check_file="$HOME/.ipython/profile_default/startup/50-ustor-vars.py" + if _exists_before_update "$check_file"; then + mkdir -p $(dirname "$check_file") + cat > "$check_file" <<- EOF ## BDAP Storage locations PSTORE="/eos/jeodpp/data/projects/LEGENT" USTORE="/eos/jeodpp/home/users/${USER}" @@ -201,11 +223,15 @@ EOF fi } -function upd40_bashrc_conda_init { - if _mute grep ">>> conda initialize >>>" ~/.bashrc; then - _log 'already ok; delete ">>> conda initialize >>>" section from `~/.bashrc` to re-run.' - else - _log -n "updating..." +function upd30_bashrc_condarc { + local check_file="$HOME/.condarc" + local check_text="## UDS CONFIGS END" + if _grep_before_update "$check_file" "$check_text"; then + ## Header for new props appended (apart from those already there). + local check_text2="## UDS CONFIGS START" + if _mute grep -v "$check_text2" "$check_file"; then + echo -e "\n$check_text2" >> "$check_file" + fi # Strict-priority for when using conda-forge, to speedup # eg for `conda-bash-completion` package`, @@ -220,34 +246,42 @@ function upd40_bashrc_conda_init { # Stop "conda outdated" message (is `conda env update` faster?). conda config --set auto_update_conda False - conda init bash # Effective only if never used conda before. + echo "$check_text" >> "$check_file" + fi +} + +function upd40_bashrc_conda_init { + local check_file="$HOME/.bashrc" + if _grep_before_update "$check_file" ">>> conda initialize >>>"; then + ## Will print a message to logout & re-login. + conda init bash exit 0 fi } ## This function must be the 1st to run after all `upd_xxx` actions. # The last command of this action, activating conda-env, -# will fail if not re-logged in after `conda init bash`. +# will fail if not having re-logged in after `conda init bash`. function upd50_create_conda_env { - if [ -d "$conda_env_prefix" ]; then - _log -e "already ok: $conda_env_prefix - Reminder, how to (re-)activate your environment: conda activate $conda_env_prefix_rel" - else - _log "should take ~10' to create a conda-env@$conda_env_prefix..." - conda env create -p "$conda_env_prefix" -f environment.yml + local check_file="$conda_env_prefix" + local running_msg="should take ~5min to create a conda-env@$conda_env_prefix..." + local notrun_msg="already created; + DELETE it with 'conda env remove $conda_env_prefix' and re-run, to effect change. + Reminder for how to (re-)activate your environment: conda activate $conda_env_prefix" + if _exists_before_update "$check_file"; then + conda env create -p "$conda_env_prefix" -f environment.yml fi +} +function upd60_conda_env_vars { ## Setup any conda-env variables: # - make PATH & PYTHONPATH pointing to project-root, # to be able run & import scripts from anywhere. - if [ -f "$conda_env_prefix/etc/conda/activate.d/uds_vars.sh" ]; then - _log -e " OVERRIDE env_vars already ok: $conda_env_prefix/etc/conda/activate.d/uds_vars.sh - Delete this to re-run it to be effective." - else - _log "OVERRIDING env_vars..." - mkdir -p $conda_env_prefix/etc/conda/{,de}activate.d - cat > $conda_env_prefix/etc/conda/activate.d/uds_vars.sh <<- EOF + local check_file=$( _short "$conda_env_prefix/etc/conda/activate.d/uds_vars.sh") + if _exists_before_update "$check_file"; then + mkdir -p $(dirname "$check_file") + cat > "$check_file" <<- EOF #!/bin/sh export _UDS_OLD_PATH="\$PATH" @@ -255,7 +289,9 @@ export _UDS_OLD_PYTHONPATH="\$PYTHONPATH" export PATH="$mydir:\$PATH" export PYTHONPATH="$mydir:\$PYTHONPATH" EOF - cat > $conda_env_prefix/etc/conda/deactivate.d/uds_vars.sh <<- EOF + check_file=$( _short "$conda_env_prefix/etc/conda/deactivate.d/uds_vars.sh") + mkdir -p $(dirname "$check_file") + cat > "$check_file" <<- EOF #!/bin/sh export PATH="\$_UDS_OLD_PATH" @@ -263,7 +299,8 @@ export PYTHONPATH="\$_UDS_OLD_PYTHONPATH" unset _UDS_OLD_PATH unset _UDS_OLD_PYTHONPATH EOF - _log "ACTIVATE YOUR ENVIRONMENT with: conda activate $conda_env_prefix_rel" + echo "UPDATED + RE-ACTIVATE YOUR ENVIRONMENT with: conda activate $conda_env_prefix" fi } -- GitLab