lecabashlib.sh 2.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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
#
#


if [[ -z "${LECALIB_UTIL_SH}" ]]; then
	LECALIB_UTIL_SH=1
14
	LECABASHLIB_RELOADING=0
15 16 17 18
	
	if [[ -z "$LECABASHLIB_PATH" ]] ; then
		export LECABASHLIB_PATH=""
	fi
19 20 21

	THIS_DIR="$(dirname ${BASH_SOURCE[0]})"

22
    source "${THIS_DIR}/lecabashlib.sh"
23 24 25 26 27 28 29

	function abspath() {
		local P=$1
		echo "$(cd "$(dirname "${P}")" && pwd -P)/$(basename "${P}")"
	}

	LECALIB_DIR=$(abspath "${THIS_DIR}")
30 31 32 33 34 35

	function upper() {
		echo $1 | tr "a-z" "A-Z"
	}

	function lower() {
36
		echo $1 | tr "A-Z" "a-z"
37 38 39 40 41 42
	}

	function indirect() {
		eval echo \$${1}
	}

43
    function include() {
44 45 46
		local MODULE=$(upper $1)
		local LOADED_MODULE_VAR="LECALIB_${MODULE}_SH"
		local LOADED_MODULE=$(indirect $LOADED_MODULE_VAR)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		local found=""
		
		for path in $(echo $LECABASHLIB_PATH | sed 's/:/ /g') ; do
			if [[ -z "$found" && \
			      ( -z "${LOADED_MODULE}" || \
			       "$LECABASHLIB_RELOADING" == 1 ) && \
			       -f "${path}/${1}.sh" ]] ; then
				source "${path}/${1}.sh"
				eval $LOADED_MODULE_VAR=1
				found=1
			fi
		done
		if [[ -z "$found" && \
			      ( -z "${LOADED_MODULE}" || \
			       "$LECABASHLIB_RELOADING" == 1 ) && \
			       -f "${LECALIB_DIR}/${1}.sh" ]] ; then
63 64
			source "${LECALIB_DIR}/${1}.sh"
			eval $LOADED_MODULE_VAR=1
65 66 67 68
			found=1
		fi

		if (( found == 1 )) ; then
69
			if (( LECABASHLIB_RELOADING == 1 )) ; then
70 71 72
				logdebug "Module $1 reloaded..."
		    else
				logdebug "Module $1 loaded..."
73
			fi
74 75 76 77
	    else
	    		if [[ -z "${LOADED_MODULE}" ]] ; then
	    			logerror "Cannot load module $1"
	    		fi
78 79 80
		fi
	}

81 82 83 84 85 86 87
	function reload() {
		LECABASHLIB_RELOADING=1
		include "${1}"
		LECABASHLIB_RELOADING=0
	}


88
	function onluke() {
89
		[[ $(hostname) =~ ^luke[0-9]* ]]
90 91 92
	}

	function onfroggy() {
93
		[[ $(hostname) =~ ^frog ]]
94
	}
95
	
Eric Coissac committed
96
	function ondahu() {
97
		[[ $(hostname) =~ ^dahu[0-9] ]] || [[ $(hostname) =~ ^bigfoot[0-9] ]]
Eric Coissac committed
98 99
	}

100
	function isinteractiveshell() {
101 102 103 104 105 106
	  case "$-" in
	    *i*) (( 1 == 1 )) ;;
	    *)   (( 1 == 0 )) ;;
	  esac
	}	

107 108 109 110 111 112 113 114
	function pushd () {
    	command pushd "$@" > /dev/null
	}

	function popd () {
    	command popd "$@" > /dev/null
	}
	
115
	include logging
116 117

fi
118