...
 
Commits (6)
......@@ -12,20 +12,9 @@
include logging
logdebug "Load atexit package"
include stacks
newstack __LECABASHLIB_ATEXITSTACK__
function __atexit__manager__() {
loginfo "Cleanning the resources"
while popvalue __LECABASHLIB_ATEXITSTACK__ cmd ; do
eval "$cmd"
done
loginfo "Done."
}
include signal
function registeratexit() {
pushvalue __LECABASHLIB_ATEXITSTACK__ "${*}"
registertrap EXIT "${*}"
}
trap "__atexit__manager__" EXIT ERR
......@@ -19,7 +19,7 @@ if [[ -z "${LECALIB_UTIL_SH}" ]]; then
THIS_DIR="$(dirname ${BASH_SOURCE[0]})"
source "${THIS_DIR}/utils.sh"
source "${THIS_DIR}/lecabashlib.sh"
function abspath() {
local P=$1
......
......@@ -83,7 +83,7 @@ function logwarning() {
function setloglevel () {
LOG_LEVEL=$(indirect "LOG_${1}_LEVEL")
loginfo "Logging level set to : ${1}"
loginfo "Logging level set to : ${1} ($LOG_LEVEL)"
}
function openlogfile() {
......
#
# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#
include logging
logdebug "Load trap package"
include stacks
__LECABASH__trap_signal_list__() {
echo $(kill -l \
| sed -E $'s/[ \t]+/@/g' \
| tr '@' '\n' \
| grep '^SIG' \
| sed 's/SIG//') EXIT ERR
}
__LECABASH__trap_signal_code__() {
local signal=$1
echo $(kill -l \
| sed -E $'s/[ \t]+/@/g' \
| sed -E 's/\)@/ /g' \
| tr '@' '\n' \
| grep "SIG${signal}" \
| awk '{print $1}') \
|| echo 0
}
__LECABASH__signal_trap_stack_name__() {
local signal="$1"
local stackname="__LECABASH__signal_${signal}_trap_stack__"
echo $stackname
}
__LECABASH__trap_function__() {
local exitcode="$?"
local signal="$1"
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
local code="$(__LECABASH__trap_signal_code__ $signal)"
loginfo "Signal ${signal} trapped"
if [[ $(stacksize $stackname) > 0 ]]; then
while popvalue "$stackname" cmd ; do
eval "$cmd"
done
loginfo "Commands registered on signal $signal run."
fi
if [[ "$signal" != "EXIT" ]]; then
exit "$((128 + code))"
else
exit "$exitcode"
fi
}
__LECABASH__contains__() {
local e match="$1"
shift
for e in "$@"; do
[[ "$e" == "$match" ]] && return 0
done
return 1
}
__LECABASH__istrapregistred__() {
local signal="$1"
local registred=$(trap -p "$signal" \
| grep '__LECABASH__trap_function__' \
| wc -l)
if (( registred == 0 )) ; then
return 1
else
return 0
fi
}
__LECABASH__registered_trap__() {
local signal="$1"
local command=$(trap -p "$signal" \
| awk '{$NF="";\
split($0,parts,"--");\
print parts[2]}')
eval "echo $command"
}
function registertrap() {
local signal=$1
shift
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
if ! __LECABASH__istrapregistred__ "$signal" ; then
local stackname="$(__LECABASH__signal_trap_stack_name__ ${signal})"
newstack $stackname
local oldcmd=$(__LECABASH__registered_trap__ "$signal")
if [[ oldcmd != "" ]] ; then
pushvalue "$stackname" "${oldcmd}"
fi
logdebug "Register trap for signal $signal"
trap "__LECABASH__trap_function__ $signal" "$signal"
fi
loginfo "Register new command on $signal : ${*}"
pushvalue "$stackname" "${*}"
}
......@@ -33,6 +33,7 @@ logdebug "Load stacks package"
function newstack() {
logdebug "New stack ${1} created"
eval declare -a "${1}"
}
......@@ -41,6 +42,7 @@ function stacksize() {
}
function pushvalue() {
logdebug "New value pushed on stack ${1}"
p="${1}[$(stacksize ${1})]=\"${2}\""
eval "$p"
}
......@@ -48,6 +50,7 @@ function pushvalue() {
function popvalue() {
local __popvalue_STACK="${1}"
shift
logdebug "New value poped from stack ${__popvalue_STACK}"
if [[ ! -z "${1}" ]]; then
local __popvalue_COMMAND="${1}="
......