tempdir.sh 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

# 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 tempdir package"


include ifs
include stacks
include atexit

newstack __LECABASHLIB_BASETMPDIRSTACK__
newstack __LECABASHLIB_TMPDIRSTACK__

__LECABASHLIB_TMPBASENAME__="__tmp__$(whoami).${$}__lecabashlib__.XXXXX"

function __remove_tmp__() {
26
	loginfo "Cleaning temporary directories"
27
	
28
	while popvalue __LECABASHLIB_TMPDIRSTACK__ directory ; do		
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
		loginfo "Removing tempdir : $directory" 
		rm -rf "$directory"
	done
}

registeratexit __remove_tmp__


function availabledisk() {
  	local FILENAME="${1}"


	pushifs $'\n'
  
	local lines=( $(df -k -P "${FILENAME}") )
	
	pushifs ' '
	local fields=(${lines[0]})
	local values=(${lines[1]})
	popifs
	popifs
	
	nfield=${#fields[@]}
	i=0
	while (( i < nfield )) && [[  "$(lower ${fields[$i]})" != "available" ]]  ; do
	 i=$(( i + 1 ))
	done
	
	
	if (( i < nfield )) ; then
		size=$(( values[i] / 1024 / 1024 ))
		echo $size
		return 0
	else
		return 1
	fi

}

function registertmpdir() {
	pushvalue __LECABASHLIB_BASETMPDIRSTACK__ "${1}"
}

72 73 74 75
function getbasetmpdir() {
	echo "${__LECABASHLIB_BASETMPDIRSTACK__[*]}"
}

76
function tempdirectory() {
77
	local __tempdirectory_minsize="0"
78 79

    if [[ ! -z "${1}" ]]; then
80
      local __tempdirectory_COMMAND="${1}="
81
    else
82
      local __tempdirectory_COMMAND="echo "
83 84 85 86 87 88 89 90 91 92 93 94 95
    fi
	shift
	
	if [[ "$(stacksize __LECABASHLIB_BASETMPDIRSTACK__)" == "0" ]]; then
		if [[ ! -z "$TMPDIR" ]] ; then
			registertmpdir "$TMPDIR"
		else
			mkdir -p "$HOME/tmp"
			registertmpdir "$HOME/tmp"
		fi
	fi

	if [[ ! -z "${1}" ]] ; then
96
		__tempdirectory_minsize="${1}"
97 98
	fi
	
99 100
	local __tempdirectory_nbase=$(stacksize __LECABASHLIB_BASETMPDIRSTACK__)
	local __tempdirectory_i=0
101
	
102 103 104
	while (( __tempdirectory_i < __tempdirectory_nbase )) && \
	      (( $(availabledisk "${__LECABASHLIB_BASETMPDIRSTACK__[$i]}") < __tempdirectory_minsize )) ; do
		__tempdirectory_i=$(( __tempdirectory_i + 1 ))
105 106
	done
	
107
	if (( __tempdirectory_i >= __tempdirectory_nbase )) ; then
108 109 110
		return 1
	fi

111 112 113
	local __tempdirectory_basedir="${__LECABASHLIB_BASETMPDIRSTACK__[$__tempdirectory_i]}"
	local __tempdirectory_directory=$(mktemp -d "${__tempdirectory_basedir%/}/${__LECABASHLIB_TMPBASENAME__}") 
	pushvalue __LECABASHLIB_TMPDIRSTACK__ "$__tempdirectory_directory" 
114
	
115 116
    local __tempdirectory_valcmd="${__tempdirectory_COMMAND}${__tempdirectory_directory}"
    eval $__tempdirectory_valcmd
117 118 119
	
}