[2] | 1 | #!/bin/ksh |
---|
| 2 | |
---|
| 3 | #************************************************************** |
---|
| 4 | # Author: Patrick Brockmann |
---|
[373] | 5 | # Contact: Patrick.Brockmann__at__cea.fr |
---|
| 6 | # $Revision:: $ Revision of last commit |
---|
| 7 | # $Author:: $ Author of last commit |
---|
| 8 | # $Date:: $ Date of last commit |
---|
[2] | 9 | # IPSL (2006) |
---|
| 10 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 11 | # |
---|
| 12 | #************************************************************** |
---|
| 13 | |
---|
| 14 | #================================================== |
---|
| 15 | # The documentation of this file can be automatically generated |
---|
[1077] | 16 | # if you use the prefix #D- for comments to be extracted. |
---|
[2] | 17 | # Extract with command: cat lib* | grep "^#D-" | cut -c "4-" |
---|
| 18 | #================================================== |
---|
[1077] | 19 | |
---|
[2] | 20 | #D-#================================================================== |
---|
| 21 | #D-libIGCM_card |
---|
| 22 | #D-This ksh library handles extraction of information from configuration file |
---|
| 23 | #D-called "card" file (en français fichier "carte"). |
---|
| 24 | #D-All function described bellow must be prefixed by IGCM_card. |
---|
| 25 | #D-A card file is organized as follows : |
---|
| 26 | #D- --------------------- |
---|
| 27 | #D-[Messages] |
---|
| 28 | #D-Option1= "Hello Earth" |
---|
| 29 | #D-Option2= "Hello Mars" |
---|
| 30 | #D- |
---|
[1077] | 31 | #D-# My comments |
---|
[2] | 32 | #D-[Recipes] |
---|
[1077] | 33 | #D-Cake= "file1.doc" |
---|
| 34 | #D-Starter= "file2.doc" |
---|
[2] | 35 | #D- |
---|
| 36 | #D-[ColorValues] |
---|
| 37 | #D-Red= 120 |
---|
[1077] | 38 | #D-Blue= 230 |
---|
| 39 | #D-Green= 178 |
---|
[2] | 40 | #D- |
---|
| 41 | #D-[Couples] |
---|
| 42 | #D-List1= (up, down), \ |
---|
| 43 | #D- (humid, dry), \ |
---|
| 44 | #D- (hot, cold), \ |
---|
| 45 | #D- (far, close) |
---|
| 46 | #D-List2= (ice, fire, air, water) |
---|
| 47 | #D- --------------------- |
---|
| 48 | #D- |
---|
| 49 | |
---|
| 50 | #D-#================================================================== |
---|
| 51 | #D-function IGCM_card_PrintOption |
---|
| 52 | #D-* Purpose: Print an option from a given file.card and section |
---|
| 53 | #D-* Usage: IGCM_card_PrintOption file.card section option |
---|
[475] | 54 | #D-* Only used by IGCM_card_Test.ksh |
---|
[2] | 55 | #D- |
---|
| 56 | function IGCM_card_PrintOption |
---|
| 57 | { |
---|
[1111] | 58 | IGCM_debug_PushStack "IGCM_card_PrintOption" $@ |
---|
| 59 | if ( [ -r "$1" ] && [ -f "$1" ] ) ; then |
---|
| 60 | gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk "$@" |
---|
| 61 | else |
---|
| 62 | echo |
---|
| 63 | IGCM_debug_Print 1 "--Error--> IGCM_card_PrintOption $@" |
---|
| 64 | IGCM_debug_Print 1 " $1 is not readable" |
---|
| 65 | IGCM_debug_Exit "IGCM_card_PrintOption" |
---|
| 66 | fi |
---|
| 67 | IGCM_debug_PopStack "IGCM_card_PrintOption" |
---|
[2] | 68 | } |
---|
| 69 | |
---|
| 70 | #D-#================================================================== |
---|
| 71 | #D-function IGCM_card_PrintSection |
---|
| 72 | #D-* Purpose: Print all options from a given file.card and section |
---|
| 73 | #D-* Usage: IGCM_card_PrintSection file.card section |
---|
[475] | 74 | #D-* Only used by IGCM_card_Test.ksh |
---|
[2] | 75 | #D- |
---|
| 76 | function IGCM_card_PrintSection |
---|
| 77 | { |
---|
[1111] | 78 | IGCM_debug_PushStack "IGCM_card_PrintSection" $@ |
---|
| 79 | if ( [ -r "$1" ] && [ -f "$1" ] ) ; then |
---|
| 80 | gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@" |
---|
| 81 | else |
---|
| 82 | IGCM_debug_Print 1 "--Error--> IGCM_card_PrintSection $@" |
---|
| 83 | IGCM_debug_Print 1 " $1 is not readable" |
---|
| 84 | IGCM_debug_Exit "IGCM_card_PrintSection" |
---|
| 85 | fi |
---|
| 86 | IGCM_debug_PopStack "IGCM_card_PrintSection" |
---|
[2] | 87 | } |
---|
| 88 | |
---|
| 89 | #D-#================================================================== |
---|
| 90 | #D-function IGCM_card_DefineVariableFromOption |
---|
| 91 | #D-* Purpose: Define a variable from a given file.card, section and option |
---|
| 92 | #D-* Variable name is automatically defined as file_section_option |
---|
| 93 | #D-* Usage: IGCM_card_DefineVariableFromOption file.card section option |
---|
| 94 | #D- |
---|
| 95 | function IGCM_card_DefineVariableFromOption |
---|
| 96 | { |
---|
[1111] | 97 | IGCM_debug_PushStack "IGCM_card_DefineVariableFromOption" $@ |
---|
| 98 | if ( [ -r "$1" ] && [ -f "$1" ] ) ; then |
---|
| 99 | # Get basename of card file ($1) |
---|
| 100 | typeset name1=${1##*/} |
---|
[786] | 101 | # Build name of variable as $1_$2_$3 (cardname_Section_Option) |
---|
| 102 | typeset name=${name1%%.*}_${2}_${3} |
---|
| 103 | typeset value=$( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" ) |
---|
[2] | 104 | |
---|
[1135] | 105 | # Only if a Section is missing we exit the job. |
---|
| 106 | # We must allow missing Option to keep backward compatibilty. |
---|
[786] | 107 | if [ "${value}" = "Error: Section not found" ] ; then |
---|
| 108 | echo |
---|
| 109 | IGCM_debug_Print 1 "Error with readding of ${name} variable in ${1}." |
---|
| 110 | IGCM_debug_Print 1 "Error: Section ${2} not found" |
---|
| 111 | IGCM_debug_Exit |
---|
| 112 | IGCM_debug_Verif_Exit |
---|
[1135] | 113 | elif [ "${value}" = "Error: Option not found" ] ; then |
---|
| 114 | eval ${name}=${NULL_STR} |
---|
| 115 | else |
---|
| 116 | eval ${name}=${value} |
---|
[786] | 117 | fi |
---|
[1111] | 118 | else |
---|
| 119 | echo |
---|
| 120 | IGCM_debug_Print 1 "--Error--> IGCM_card_DefineVariableFromOption" |
---|
| 121 | IGCM_debug_Print 1 "--Error--> $1 is not readable" |
---|
| 122 | IGCM_debug_Exit "IGCM_card_DefineVariableFromOption" |
---|
| 123 | IGCM_debug_Verif_Exit |
---|
| 124 | fi |
---|
| 125 | IGCM_debug_PopStack "IGCM_card_DefineVariableFromOption" |
---|
[2] | 126 | } |
---|
| 127 | |
---|
| 128 | #D-#================================================================== |
---|
| 129 | #D-function IGCM_card_DefineArrayFromOption |
---|
| 130 | #D-* Purpose: Define an array variable from a given file.card, section and option |
---|
| 131 | #D-* Array variable is automatically defined as file_section_option |
---|
| 132 | #D-* Usage: IGCM_card_DefineArrayFromOption file.card section option |
---|
| 133 | #D- |
---|
| 134 | function IGCM_card_DefineArrayFromOption |
---|
| 135 | { |
---|
[1111] | 136 | IGCM_debug_PushStack "IGCM_card_DefineArrayFromOption" $@ |
---|
| 137 | if ( [ -r "$1" ] && [ -f "$1" ] ) ; then |
---|
| 138 | # Get basename of card file ($1) |
---|
| 139 | typeset name1=${1##*/} |
---|
| 140 | # Build name of array as $1_$2_$3 (cardname_Section_Option) |
---|
| 141 | typeset name=${name1%%.*}_${2}_${3} |
---|
| 142 | eval unset ${name} |
---|
| 143 | eval ${name}[0]=${NULL_STR} |
---|
| 144 | set +A ${name} -- $( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" | gawk -- 'BEGIN {FS="[() ,]+"} {for (i=2; i <= NF-1; i++) printf("%s ",$i)}' ) |
---|
| 145 | else |
---|
| 146 | echo |
---|
| 147 | IGCM_debug_Print 1 "--Error--> IGCM_card_DefineArrayFromOption $@" |
---|
| 148 | IGCM_debug_Print 1 " $1 is not readable" |
---|
| 149 | IGCM_debug_Exit "IGCM_card_DefineArrayFromOption" |
---|
| 150 | fi |
---|
| 151 | IGCM_debug_PopStack "IGCM_card_DefineArrayFromOption" |
---|
[2] | 152 | } |
---|
| 153 | |
---|
| 154 | #D-#================================================================== |
---|
| 155 | #D-function IGCM_card_DefineArrayFromSection |
---|
| 156 | #D-* Purpose: Define an array variable from a given file.card and section |
---|
| 157 | #D-* Array variable is automatically defined as file_section |
---|
| 158 | #D-* Usage: IGCM_card_DefineArrayFromSection file.card section |
---|
| 159 | #D- |
---|
| 160 | function IGCM_card_DefineArrayFromSection |
---|
| 161 | { |
---|
[1111] | 162 | IGCM_debug_PushStack "IGCM_card_DefineArrayFromSection" $@ |
---|
| 163 | if ( [ -r "$1" ] && [ -f "$1" ] ) ; then |
---|
| 164 | # Get basename of card file ($1) |
---|
| 165 | typeset name1=${1##*/} |
---|
| 166 | # Build name of array as $1_$2 (cardname_Section) |
---|
| 167 | typeset name=${name1%%.*}_${2} |
---|
| 168 | eval unset ${name} |
---|
| 169 | eval ${name}[0]=${NULL_STR} |
---|
| 170 | set +A ${name} -- $( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@" ) |
---|
| 171 | #if [ "$( eval echo \${${name}[@]} )" = "Error: Section not found" ] ; then |
---|
| 172 | # echo |
---|
| 173 | # IGCM_debug_Print 1 "Error with readding of ${name} variable in ${1}." |
---|
| 174 | # IGCM_debug_Print 1 "Error: Section ${2} not found" |
---|
| 175 | # IGCM_debug_Exit |
---|
| 176 | # IGCM_debug_Verif_Exit |
---|
| 177 | #fi |
---|
| 178 | if [ "$( eval echo \${${name}[@]} )" = "Error: Section not found" ] ; then |
---|
| 179 | echo |
---|
| 180 | IGCM_debug_Print 1 "Warning with readding of ${name} variable in ${1}." |
---|
| 181 | IGCM_debug_Print 1 "Warning: Section ${2} not found" |
---|
[786] | 182 | eval unset ${name} |
---|
[2] | 183 | fi |
---|
[1111] | 184 | else |
---|
| 185 | IGCM_debug_Print 1 "--Error--> IGCM_card_DefineArrayFromSection $@" |
---|
| 186 | IGCM_debug_Print 1 " $1 is not readable" |
---|
| 187 | IGCM_debug_Exit "IGCM_card_DefineArrayFromSection" |
---|
| 188 | fi |
---|
| 189 | IGCM_debug_PopStack "IGCM_card_DefineArrayFromSection" |
---|
[2] | 190 | } |
---|
| 191 | |
---|
| 192 | #D-#================================================================== |
---|
| 193 | #D-function IGCM_card_WriteOption |
---|
[23] | 194 | #D-* Purpose: Write an option in a given file.card and section |
---|
[2] | 195 | #D-* Usage: IGCM_card_WriteOption file.card section newvalue |
---|
| 196 | #D-* Examples: IGCM_card_WriteOption file.card Recipes Red 150 |
---|
| 197 | #D- IGCM_card_WriteOption file.card Messages Option2 '"Hello Mercure"' |
---|
| 198 | #D- IGCM_card_WriteOption file.card Messages ListVal1 '( 1, 2, 3 )' |
---|
| 199 | #D- listname="(Sebastien, Martial, Patrick)" |
---|
| 200 | #D- IGCM_card_WriteOption NewTestFile.card Messages ListVal2 "${listname}" |
---|
| 201 | #D- |
---|
| 202 | function IGCM_card_WriteOption |
---|
| 203 | { |
---|
[1111] | 204 | IGCM_debug_PushStack "IGCM_card_WriteOption" $@ |
---|
| 205 | if ( [ -r "$1" ] && [ -w "$1" ] && [ -f "$1" ] ) ; then |
---|
[1152] | 206 | if [ $( IGCM_card_PrintOption "$1" "$2" "$3" | grep "not found" | wc -l ) -gt 0 ] ; then |
---|
[1111] | 207 | IGCM_debug_Print 1 "!!! Issue with IGCM_card_WriteOption !!!" |
---|
| 208 | IGCM_debug_Print 1 "We tried to write : $@" |
---|
| 209 | IGCM_debug_Exit "Must check that option $3 in section $2 exist in this file $1" |
---|
| 210 | IGCM_debug_Verif_Exit |
---|
| 211 | fi |
---|
[223] | 212 | |
---|
[1111] | 213 | # The tmpfile uses now the real path of the card to be modified, |
---|
| 214 | # not just a local tmpfile with PID. |
---|
| 215 | tmpfile=$1_mutex_$$ |
---|
[223] | 216 | |
---|
[1111] | 217 | IGCM_card_CheckConflict $1 |
---|
[223] | 218 | |
---|
[1111] | 219 | # Do the job |
---|
| 220 | ( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_WriteOption.awk -- "$@" 2> /dev/null ) > ${tmpfile} |
---|
[223] | 221 | |
---|
[1111] | 222 | cp $1 $1.bak |
---|
| 223 | mv ${tmpfile} $1 |
---|
[223] | 224 | |
---|
[1111] | 225 | else |
---|
| 226 | echo |
---|
| 227 | IGCM_debug_Print 1 "--Error--> IGCM_card_WriteOption $@" |
---|
| 228 | IGCM_debug_Print 1 " $1 is not readable or not writable" |
---|
| 229 | IGCM_debug_Exit "IGCM_card_WriteOption" |
---|
| 230 | fi |
---|
| 231 | IGCM_debug_PopStack "IGCM_card_WriteOption" |
---|
[2] | 232 | } |
---|
| 233 | |
---|
[1111] | 234 | #D-#================================================================== |
---|
| 235 | #D-function IGCM_card_CheckConflict |
---|
| 236 | #D-* Purpose: Check that a card is not in use by another process. If it is the case wait until it is not. |
---|
| 237 | #D-* Usage: IGCM_card_CheckConflict run.card |
---|
| 238 | #D-* Examples: |
---|
| 239 | #D- |
---|
[472] | 240 | function IGCM_card_CheckConflict |
---|
| 241 | { |
---|
[1111] | 242 | IGCM_debug_PushStack "IGCM_card_CheckConflict" $@ |
---|
| 243 | typeset isleep tmpfiles |
---|
[472] | 244 | |
---|
[1111] | 245 | # Watch for possible conflics : Check for other tmpfiles. |
---|
| 246 | set +A tmpfiles -- $( ls $1_mutex_[0-9]* 2>/dev/null ) |
---|
| 247 | ((isleep=0)) |
---|
| 248 | while [ ${#tmpfiles[@]} -gt 0 ] ; do |
---|
| 249 | echo "Conflict between two processes working on " $1 "!!!" ${tmpfiles[@]} |
---|
| 250 | sleep 1 |
---|
| 251 | ((isleep=isleep+1)) |
---|
| 252 | if [ ${isleep} -gt 20 ] ; then |
---|
| 253 | echo "Too many loops waiting for other process working on " $1 ". We continue." |
---|
| 254 | echo "You should see if one process of your run or post-treatment may have terminated suddenly." |
---|
| 255 | echo "Afer, you should erase this(those) file(s) : " ${tmpfiles[@]} |
---|
| 256 | # Send a mail to USER ?? |
---|
| 257 | break ; |
---|
| 258 | fi |
---|
| 259 | unset tmpfiles |
---|
[472] | 260 | set +A tmpfiles -- $( ls $1_mutex_[0-9]* 2>/dev/null ) |
---|
[1111] | 261 | done |
---|
[472] | 262 | |
---|
[1111] | 263 | IGCM_debug_PopStack "IGCM_card_CheckConflict" |
---|
[472] | 264 | } |
---|
| 265 | |
---|
[2] | 266 | #D-#================================================================== |
---|
[23] | 267 | #D-function IGCM_card_WriteArrayOption |
---|
| 268 | #D-* Purpose: Write an array option a given file.card and section |
---|
| 269 | #D-* Usage: IGCM_card_WriteArrayOption file.card section option newarray |
---|
| 270 | #D-* Examples: set -A MyArray -- 1 2 3 |
---|
| 271 | #D- IGCM_card_WriteArrayOption file.card Recipes List MyArray |
---|
| 272 | #D- |
---|
| 273 | function IGCM_card_WriteArrayOption |
---|
| 274 | { |
---|
[1111] | 275 | IGCM_debug_PushStack "IGCM_card_WriteArrayOption" $@ |
---|
[23] | 276 | |
---|
[1111] | 277 | if ( [ -r "$1" ] && [ -w "$1" ] && [ -f "$1" ] ) ; then |
---|
| 278 | if [ X"${4}" != X"" ]; then |
---|
| 279 | tab=$4 |
---|
| 280 | IGCM_card_WriteOption $1 $2 $3 '('$( eval echo \${${tab}[@]} | sed -e 's/ /,/g' )')' |
---|
[23] | 281 | else |
---|
[1111] | 282 | IGCM_card_WriteOption $1 $2 $3 '()' |
---|
[23] | 283 | fi |
---|
[1111] | 284 | else |
---|
| 285 | echo |
---|
| 286 | IGCM_debug_Print 1 "--Error--> IGCM_card_WriteArrayOption $@" |
---|
| 287 | IGCM_debug_Print 1 " $1 is not readable or not writable" |
---|
| 288 | IGCM_debug_Exit "IGCM_card_WriteArrayOption" |
---|
| 289 | fi |
---|
| 290 | IGCM_debug_PopStack "IGCM_card_WriteArrayOption" |
---|
[23] | 291 | } |
---|
| 292 | |
---|
| 293 | #D-#================================================================== |
---|
[2] | 294 | #D-function IGCM_card_Check |
---|
| 295 | #D-* Purpose: Check the present file by comparison with a reference file |
---|
| 296 | #D-* Usage: IGCM_card_Check |
---|
| 297 | #D- |
---|
| 298 | function IGCM_card_Check |
---|
| 299 | { |
---|
[1111] | 300 | #--------------------- |
---|
| 301 | if [ ! -n "${libIGCM}" ] ; then |
---|
| 302 | echo "Check libIGCM_card ...........................................[ FAILED ]" |
---|
| 303 | echo "--Error--> libIGCM variable is not defined" |
---|
| 304 | IGCM_debug_Exit "IGCM_card_Check" |
---|
| 305 | fi |
---|
[2] | 306 | |
---|
[1111] | 307 | #--------------------- |
---|
| 308 | whence -v gawk > /dev/null 2>&1 |
---|
| 309 | if [ ! $? -eq 0 ] ; then |
---|
| 310 | echo "Check libIGCM_card ...........................................[ FAILED ]" |
---|
| 311 | echo "--Error--> gawk command is not defined" |
---|
| 312 | IGCM_debug_Exit "IGCM_card_Check" |
---|
| 313 | fi |
---|
[2] | 314 | |
---|
[1111] | 315 | #--------------------- |
---|
[1118] | 316 | # No need to remove timestamps here |
---|
[1123] | 317 | diff ${libIGCM}/libIGCM_card/IGCM_card_Test.ref <(${libIGCM}/libIGCM_card/IGCM_card_Test.ksh) > /dev/null 2>&1 |
---|
[1111] | 318 | status=$? |
---|
[2] | 319 | |
---|
[1118] | 320 | if [ ${status} -eq 0 ] ; then |
---|
[1111] | 321 | echo "Check libIGCM_card ...............................................[ OK ]" |
---|
| 322 | else |
---|
| 323 | echo "Check libIGCM_card ...........................................[ FAILED ]" |
---|
| 324 | echo "--Error--> Execution of ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh" |
---|
[1578] | 325 | echo " has produced an error in the checking phase" |
---|
| 326 | echo " If you are working on TGCC computer : please verify if your login is in the groupe igcmg" |
---|
| 327 | echo " if not, ask to hotline to be add to this group" |
---|
| 328 | echo " If this is not the solution to your problem, verify you didn't made any modifications" |
---|
| 329 | echo " in libIGCM_sys, libIGCM_debug, libIGCM_card" |
---|
[1125] | 330 | diff ${libIGCM}/libIGCM_card/IGCM_card_Test.ref <(${libIGCM}/libIGCM_card/IGCM_card_Test.ksh) |
---|
[1111] | 331 | IGCM_debug_Exit "IGCM_card_Check" |
---|
| 332 | fi |
---|
[2] | 333 | } |
---|