Commit 16f66e60 authored by Bogdan Vacaliuc's avatar Bogdan Vacaliuc
Browse files

refactor environment injection to simplify execution block

parent 0280d385
Loading
Loading
Loading
Loading
+24 −15
Original line number Diff line number Diff line
@@ -7,13 +7,31 @@ _usage() {
}

# overwrite these values with environment injection e.g.
# PIXI_PREFIX=path-to-parent-folder nsd-mantid-profile-wrap.sh mantid_dev --classic script.py
# PROFILE_ENV=mantid nsd-mantid-profile-wrap.sh mantid --classic script.py
# PROFILE_OPT="--outfile path-to-html" nsd-mantid-profile-wrap.sh mantid --classic script.py
# TMP=path-to-text-file			- default a file from mktemp
# PIXI_PREFIX=path-to-parent-folder	- default /usr/local/pixi
# PROFILE_ENV=mantid			- default mantid_dev (needs to have mantidprofiler module)
# PROFILE_OUT=path-to-html-file		- default ./profile.html
# -or-
# PROFILE_OPT="--outfile path-to-html --logfile path-to-txt ..."

TMP="${TMP:=$(mktemp)}"
PIXI_PREFIX="${PIXI_PREFIX:=/usr/local/pixi}"
PROFILE_ENV="${PROFILE_ENV:=mantid_dev}"
read -r -a PROFILE_OPT <<< "${PROFILE_OPT//[\(\)]/}"	# ensure PROFILE_OUT is an array
PROFILE_OUT="${PROFILE_OUT:=}"
# ensure PROFILE_OUT is a path-to-html-file, and PROFILE_OPT is an array
if [ -z "${PROFILE_OUT}" ] ; then
    read -r -a PROFILE_OPT <<< "${PROFILE_OPT//[\(\)]/}"
    # https://superuser.com/a/1732848
    # shellcheck disable=SC2199 # use implicit concatenation
    # shellcheck disable=SC2076 # rhs quoted syntax is correct
    if [[ "${PROFILE_OPT[@]}" =~ "--outfile ([^ ]+) " ]]; then
        PROFILE_OUT="${BASH_REMATCH[1]}"
    else
        PROFILE_OUT="$(pwd)/profile.html"
    fi
else
    PROFILE_OPT=("--outfile ${PROFILE_OUT}")
fi

# select the environment to use
ENV=
@@ -59,8 +77,8 @@ if [ -z "$CMD" ] ; then
fi

# execute main app in background
TMP=$(mktemp)
nsd-conda-wrap.sh "${ENV}" --classic "${CMD}" "$@" > "${TMP}" & pid=$!
echo "Writing script output to ${TMP}"

# deal with background app under CTRL-C and exit conditions
trap 'set -x ; kill -HUP $pid' SIGINT
@@ -71,13 +89,4 @@ args=("$PROFILE_ENV" "--classic" "-m" "mantidprofiler")
# shellcheck disable=SC2206 # do not break the array into a single string
args+=(${PROFILE_OPT[@]})
args+=("${pid}")
if nsd-conda-wrap.sh "${args[@]}" ; then
    # https://superuser.com/a/1732848
    # shellcheck disable=SC2199 # use implicit concatenation
    # shellcheck disable=SC2076 # rhs quoted syntax is correct
    if [[ "${PROFILE_OPT[@]}" =~ "--outfile ([^ ]+) " ]]; then
        echo "Wrote ${BASH_REMATCH[1]}"
    else
        echo "Wrote $(pwd)/profile.html"
    fi
fi
nsd-conda-wrap.sh "${args[@]}" && echo "Wrote ${PROFILE_OUT}"