Changeset 1074 for trunk/libIGCM


Ignore:
Timestamp:
09/25/14 14:53:25 (10 years ago)
Author:
jgipsl
Message:

Added function IGCM_comp_modifyXmlFile

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh

    r1069 r1074  
    10561056 
    10571057#======================================================================= 
     1058function IGCM_comp_modifyXmlFile 
     1059{ 
     1060# 
     1061# syntax:     IGCM_comp_modifyXmlFile  type  filein  keyid  keyattrib  value 
     1062# 
     1063# For example : IGCM_comp_modifyXmlFile force file_def_orchidee.xml sechiba2 enabled .TRUE. 
     1064#          or   IGCM_comp_modifyXmlFile blocker iodef.xml using_server NONE false 
     1065# 
     1066# This function is used to modify the value for a specific attribute and variable id. 
     1067# The file must be a of xml syntax. 
     1068# This function can be used in the comp.driver files for the components.  
     1069# 
     1070# Arguments:  
     1071# - type      : first argument must be blocker, nonblocker or force.  
     1072#               For "blocker" case, the variable must be attributed the keyworld AUTO  
     1073#               otherwise this function will exit. 
     1074#               For "nonblocker" case, the user can remove or modify the variable. For  
     1075#               this case, as long as AUTO is not set, no modification will be done. 
     1076#               For "force" case, the variable will be modified even if it is not set to AUTO  
     1077# - filein    : the file in run directory of .xml type in which the variable should be set 
     1078# - keyid     : the variable to modify 
     1079# - keyattrib : the attribute name to modify. If NONE, then the variable itself will be modified 
     1080# - value     : the value to set in the filein 
     1081# 
     1082    IGCM_debug_PushStack "IGCM_comp_modifyXmlFile" 
     1083     
     1084    # Set local variables and test the arguments 
     1085    if [ $# = 5 ] ; then 
     1086        # Normal case with 4 arguments 
     1087        type=$1 ; filein=$2 ; keyid=$3 ; keyattrib=$4 ; value=$5 
     1088    else 
     1089        IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Bad number of arguments." 
     1090        return 
     1091    fi 
     1092    IGCM_debug_Print 2 "Entering IGCM_comp_modifyXmlFile with arguments: type=$type file=$filein, id=$keyid attribute=$keyattrib, value=$value" 
     1093 
     1094    # Test if first argument is correct 
     1095    if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then 
     1096        IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Error in first argument must be blocker, nonblocker or force" 
     1097        return 
     1098    fi 
     1099 
     1100    # Test if the file exist 
     1101    if [ ! -f ${filein} ] ; then 
     1102        IGCM_debug_Exit "IGCM_comp_modifyXmlFile: $filein does not exist." 
     1103        return 
     1104    fi 
     1105 
     1106    # Test if keyid is set in filein, otherwise exit 
     1107    if [ $( grep -w $keyid $filein | wc -l ) = 0 ] ; then 
     1108        # Variable key is not set in filein, stop.  
     1109        IGCM_debug_Exit "IGCM_comp_modifyXmlFile : $keyid is not set in the file. Bad syntax of $filein file." 
     1110        return 
     1111    fi 
     1112 
     1113    # Check if AUTO is set on the same line as keyid and keyattrib 
     1114    if [  $( grep -w $keyid $filein | grep AUTO | wc -l ) = 1 ] ; then 
     1115        # Modifification will be done 
     1116        modify=yes 
     1117    else 
     1118        if [ $type = blocker ] ; then 
     1119            # Exit, the variable must be set to AUTO 
     1120            IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The $keyattrib for $keyid must be set to AUTO in $filein." 
     1121            return 
     1122        elif [ $type = nonblocker ] ; then 
     1123            # Nothing will be done 
     1124            IGCM_debug_Print 1 "Nonblocker nothing is done for ${filein}, id=${keyid} and attribute ${keyattrib}" 
     1125            modify=no 
     1126        elif [ $type = force ] ; then 
     1127            # Force modification 
     1128            IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=$keyattrib for id=$keyid was not set to AUTO. Modification will be forced." 
     1129            modify=yes 
     1130        fi 
     1131    fi 
     1132 
     1133    # Do the modifcation now 
     1134    if [ $modify = yes ] ; then 
     1135        if [ $keyattrib = NONE ] ; then 
     1136            # Case to modify the variable itself 
     1137            IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting the variable=${value}" 
     1138            sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" $filein > $filein.tmp 
     1139        else 
     1140            # Case to modify the attribute value 
     1141            IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting attribute to ${keyattrib}=${value}" 
     1142            sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" $filein > $filein.tmp 
     1143        fi 
     1144        \mv $filein.tmp $filein  
     1145    fi 
     1146    IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" 
     1147} 
     1148 
     1149#======================================================================= 
    10581150function IGCM_comp_Update 
    10591151{ 
Note: See TracChangeset for help on using the changeset viewer.