[2280] | 1 | #!/bin/bash |
---|
| 2 | #------------------------------------------------ |
---|
[10797] | 3 | #$Id: chk_iomput.sh 8309 2017-07-10 13:44:10Z timgraham $ |
---|
[2280] | 4 | #------------------------------------------------ |
---|
| 5 | # |
---|
| 6 | set -u |
---|
| 7 | # |
---|
| 8 | # if not argument -> get the help |
---|
| 9 | [ $# -eq 0 ] && ./$0 --help && exit |
---|
| 10 | # |
---|
| 11 | inxml=0 |
---|
| 12 | insrc=0 |
---|
| 13 | while [ $# -gt 0 ] # Until you run out of parameters . . . |
---|
| 14 | do |
---|
| 15 | case "$1" in |
---|
| 16 | -h|--help) |
---|
| 17 | echo |
---|
| 18 | echo 'Description:' |
---|
| 19 | echo ' check that an xml file is coherant with the source code:' |
---|
| 20 | echo ' - all variable ids defined by "call iom_put" must have their counterpart' |
---|
| 21 | echo ' in the variable definition in xml file' |
---|
| 22 | echo ' - list variable ids defined in xml file without any corresponding call' |
---|
| 23 | echo ' to iom_put. This can be done but it is useless as iom will only ouput zeros' |
---|
| 24 | echo ' - all variable ids used in the files definition in xml file must have' |
---|
| 25 | echo ' their counterpart in the variable definition in xml file' |
---|
| 26 | echo 'Usage:' |
---|
| 27 | echo ' chk_iomput.sh [OPTION]' |
---|
| 28 | echo ' or chk_iomput.sh [OPTION] xmlfile DIRECTORIES' |
---|
| 29 | echo ' with:' |
---|
| 30 | echo ' xmlfile: the xml file to test' |
---|
| 31 | echo ' DIRECTORIES: a list of directories containing the source code' |
---|
| 32 | echo 'Options' |
---|
| 33 | echo ' -h, --help to get this help' |
---|
| 34 | echo ' --inxml only print all variable definitions found in the xml file' |
---|
| 35 | echo ' --insrc only print all variable definitions found in the source code' |
---|
| 36 | echo 'Examples' |
---|
[4153] | 37 | echo ' ./chk_iomput.sh' |
---|
| 38 | echo ' ./chk_iomput.sh --help' |
---|
[8309] | 39 | echo ' ./chk_iomput.sh ../../CONFIG/ORCA2_LIM/EXP00/context_nemo.xml "../../NEMO/OPA_SRC/ ../../NEMO/LIM_SRC_2/"' |
---|
[2280] | 40 | echo |
---|
| 41 | exit ;; |
---|
| 42 | --inxml) inxml=1 ;; |
---|
| 43 | --insrc) insrc=1 ;; |
---|
| 44 | -*) echo ; echo "illegal option" ; ./$0 --help && exit ;; |
---|
| 45 | *) [ $# -ne 2 ] && echo && echo "wrong number of arguments" && ./$0 --help && exit |
---|
| 46 | xmlfile=${1} |
---|
| 47 | srcdir=${2} |
---|
| 48 | shift |
---|
| 49 | esac |
---|
| 50 | shift # Check next set of parameters. |
---|
| 51 | done |
---|
| 52 | # |
---|
[8309] | 53 | echo $xmlfile |
---|
| 54 | echo $srcdir |
---|
| 55 | |
---|
| 56 | for i in $xmlfile |
---|
| 57 | do |
---|
| 58 | [ ! -f "$xmlfile" ] && echo "$xmlfile not found, we stop..." && exit |
---|
| 59 | done |
---|
[2280] | 60 | for i in $srcdir |
---|
| 61 | do |
---|
| 62 | [ ! -d $i ] && echo "$i is not a directory, we stop..." && exit |
---|
| 63 | done |
---|
| 64 | # |
---|
| 65 | #------------------------------------------------ |
---|
| 66 | # |
---|
[4162] | 67 | external=$( grep -c "<field_definition *\([^ ].* \)*src=" $xmlfile ) |
---|
[8309] | 68 | if [ $external -ge 1 ] |
---|
[4153] | 69 | then |
---|
[4162] | 70 | xmlfield_def=$( grep "<field_definition *\([^ ].* \)*src=" $xmlfile | sed -e 's/.*src="\([^"]*\)".*/\1/' ) |
---|
[8309] | 71 | tmp_def="" |
---|
| 72 | for fdef in $xmlfield_def ; do tmp_def="$tmp_def $( dirname $xmlfile )/$fdef" ; done |
---|
| 73 | xmlfield_def=$tmp_def |
---|
| 74 | echo $xmlfield_def |
---|
[4153] | 75 | else |
---|
| 76 | xmlfield_def=$xmlfile |
---|
| 77 | fi |
---|
[8309] | 78 | external=$( grep -c "<file_definition *\([^ ].* \)*src=" $xmlfile ) |
---|
| 79 | if [ $external -ge 1 ] |
---|
| 80 | then |
---|
| 81 | xmlfile_def=$( grep "<file_definition *\([^ ].* \)*src=" $xmlfile | sed -e 's/.*src="\([^"]*\)".*/\1/' ) |
---|
| 82 | tmp_def="" |
---|
| 83 | for fdef in $xmlfile_def ; do tmp_def="$tmp_def $( dirname $xmlfile )/$fdef" ; done |
---|
| 84 | xmlfile_def=$tmp_def |
---|
| 85 | echo $xmlfile_def |
---|
| 86 | else |
---|
| 87 | xmlfile_def=$xmlfile |
---|
| 88 | fi |
---|
| 89 | |
---|
[4162] | 90 | [ $inxml -eq 1 ] && grep "< *field *\([^ ].* \)*id *=" $xmlfield_def |
---|
[2280] | 91 | [ $insrc -eq 1 ] && find $srcdir -name "*.[Ffh]90" -exec grep -iH "^[^\!]*call *iom_put *(" {} \; |
---|
| 92 | [ $(( $insrc + $inxml )) -ge 1 ] && exit |
---|
| 93 | # |
---|
| 94 | #------------------------------------------------ |
---|
| 95 | # |
---|
| 96 | # list of file containing "CALL iom_put" in $srcdir |
---|
| 97 | # |
---|
| 98 | srclist=$( find $srcdir -name "*.[Ffh]90" -exec grep -il "^[^\!]*call *iom_put *(" {} \; ) |
---|
| 99 | # |
---|
| 100 | # list of variables used in "CALL iom_put" |
---|
| 101 | # |
---|
[4153] | 102 | badvarsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -ic iom_put ) |
---|
| 103 | if [ $badvarsrc -ne 0 ] |
---|
| 104 | then |
---|
| 105 | echo "The following call to iom_put cannot be checked" |
---|
| 106 | echo |
---|
| 107 | find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -i iom_put | sort -d |
---|
| 108 | echo |
---|
| 109 | fi |
---|
| 110 | varlistsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -vi iom_put | sort -d ) |
---|
[2280] | 111 | # |
---|
| 112 | # list of variables defined in the xml file |
---|
| 113 | # |
---|
[4162] | 114 | varlistxml=$( grep "< *field *\([^ ].* \)*id *=" $xmlfield_def | sed -e "s/^.*< *field .*id *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d ) |
---|
[2280] | 115 | # |
---|
| 116 | # list of variables to be outputed in the xml file |
---|
| 117 | # |
---|
[8309] | 118 | varlistout=$( grep "< *field *\([^ ].* \)*field_ref *=" $xmlfile_def | sed -e "s/^.*< *field .*field_ref *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d ) |
---|
[2280] | 119 | # |
---|
| 120 | echo "--------------------------------------------------" |
---|
| 121 | echo check if all iom_put found in $srcdir |
---|
[4153] | 122 | echo have a corresponding variable definition in $xmlfield_def |
---|
[2280] | 123 | echo "--------------------------------------------------" |
---|
| 124 | for var in $varlistsrc |
---|
| 125 | do |
---|
| 126 | tst=$( echo " "$varlistxml" " | grep -c " $var " ) |
---|
| 127 | if [ $tst -ne 1 ] |
---|
| 128 | then |
---|
[4153] | 129 | echo "problem with $var: $tst lines corresponding to its definition in $xmlfield_def, but defined in the code in" |
---|
[2280] | 130 | for f in $srclist |
---|
| 131 | do |
---|
| 132 | grep -iH "^[^\!]*call *iom_put *( *[\"\']${var}[\"\'] *," $f |
---|
| 133 | done |
---|
| 134 | echo |
---|
| 135 | fi |
---|
| 136 | done |
---|
| 137 | # |
---|
| 138 | echo "--------------------------------------------------" |
---|
| 139 | echo check if all variables defined in $xmlfile |
---|
| 140 | echo have a corresponding \"call iom_put\" in sources found in $srcdir |
---|
| 141 | echo "--------------------------------------------------" |
---|
| 142 | # |
---|
| 143 | for var in $varlistxml |
---|
| 144 | do |
---|
| 145 | found=$( echo " "$varlistsrc" " | grep -c " $var " ) |
---|
| 146 | if [ $found -eq 0 ] |
---|
| 147 | then |
---|
| 148 | echo \"call iom_put\" not found for variable $var |
---|
| 149 | grep "< *field * id *= *[\"\']${var}[\"\']" $xmlfile |
---|
| 150 | echo |
---|
| 151 | fi |
---|
| 152 | done |
---|
| 153 | # |
---|
| 154 | echo "--------------------------------------------------" |
---|
| 155 | echo ${xmlfile}: check if all variables to be outputed in files are really defined... |
---|
| 156 | echo "--------------------------------------------------" |
---|
| 157 | # |
---|
| 158 | # list of variables defined in the xml file |
---|
| 159 | for var in $varlistout |
---|
| 160 | do |
---|
| 161 | found=$( echo " "$varlistxml" " | grep -c " $var " ) |
---|
| 162 | [ $found -eq 0 ] && echo variable to be outputed but not defined: $var |
---|
| 163 | done |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | exit |
---|