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
26
27
28
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#------------------------------------------------
#$Id: chk_iomput.sh 9598 2018-05-15 22:47:16Z nicolasmartin $
#------------------------------------------------
#
set -u
#
# if not argument -> get the help
[ $# -eq 0 ] && ./$0 --help && exit
#
inxml=0
insrc=0
while [ $# -gt 0 ] # Until you run out of parameters . . .
do
case "$1" in
-h|--help)
echo
echo 'Description:'
echo ' check that an xml file is coherant with the source code:'
echo ' - all variable ids defined by "call iom_put" must have their counterpart'
echo ' in the variable definition in xml file'
echo ' - list variable ids defined in xml file without any corresponding call'
echo ' to iom_put. This can be done but it is useless as iom will only ouput zeros'
echo ' - all variable ids used in the files definition in xml file must have'
echo ' their counterpart in the variable definition in xml file'
echo 'Usage:'
echo ' chk_iomput.sh [OPTION]'
echo ' or chk_iomput.sh [OPTION] xmlfile DIRECTORIES'
echo ' with:'
echo ' xmlfile: the xml file to test'
echo ' DIRECTORIES: a list of directories containing the source code'
echo 'Options'
echo ' -h, --help to get this help'
echo ' --inxml only print all variable definitions found in the xml file'
echo ' --insrc only print all variable definitions found in the source code'
echo 'Examples'
echo ' ./chk_iomput.sh'
echo ' ./chk_iomput.sh --help'
echo ' ./chk_iomput.sh ../../cfgs/ORCA2_LIM/EXP00/context_nemo.xml "../../src/OCE/ ../../src/ICE/"'
echo
exit ;;
--inxml) inxml=1 ;;
--insrc) insrc=1 ;;
-*) echo ; echo "illegal option" ; ./$0 --help && exit ;;
*) [ $# -ne 2 ] && echo && echo "wrong number of arguments" && ./$0 --help && exit
xmlfile=${1}
srcdir=${2}
shift
esac
shift # Check next set of parameters.
done
#
echo $xmlfile
echo $srcdir
for i in $xmlfile
do
[ ! -f "$xmlfile" ] && echo "$xmlfile not found, we stop..." && exit
done
for i in $srcdir
do
[ ! -d $i ] && echo "$i is not a directory, we stop..." && exit
done
#
#------------------------------------------------
#
external=$( grep -c "<field_definition *\([^ ].* \)*src=" $xmlfile )
if [ $external -ge 1 ]
then
xmlfield_def=$( grep "<field_definition *\([^ ].* \)*src=" $xmlfile | sed -e 's/.*src="\([^"]*\)".*/\1/' )
tmp_def=""
for fdef in $xmlfield_def ; do tmp_def="$tmp_def $( dirname $xmlfile )/$fdef" ; done
xmlfield_def=$tmp_def
echo $xmlfield_def
else
xmlfield_def=$xmlfile
fi
external=$( grep -c "<file_definition *\([^ ].* \)*src=" $xmlfile )
if [ $external -ge 1 ]
then
xmlfile_def=$( grep "<file_definition *\([^ ].* \)*src=" $xmlfile | sed -e 's/.*src="\([^"]*\)".*/\1/' )
tmp_def=""
for fdef in $xmlfile_def ; do tmp_def="$tmp_def $( dirname $xmlfile )/$fdef" ; done
xmlfile_def=$tmp_def
echo $xmlfile_def
else
xmlfile_def=$xmlfile
fi
[ $inxml -eq 1 ] && grep "< *field *\([^ ].* \)*id *=" $xmlfield_def
[ $insrc -eq 1 ] && find $srcdir -name "*.[Ffh]90" -exec grep -iH "^[^\!]*call *iom_put *(" {} \;
[ $(( $insrc + $inxml )) -ge 1 ] && exit
#
#------------------------------------------------
#
# list of file containing "CALL iom_put" in $srcdir
#
srclist=$( find $srcdir -name "*.[Ffh]90" -exec grep -il "^[^\!]*call *iom_put *(" {} \; )
#
# list of variables used in "CALL iom_put"
#
badvarsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -ic iom_put )
if [ $badvarsrc -ne 0 ]
then
echo "The following call to iom_put cannot be checked"
echo
find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -i iom_put | sort -d
echo
fi
varlistsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -vi iom_put | sort -d )
#
# list of variables defined in the xml file
#
varlistxml=$( grep "< *field *\([^ ].* \)*id *=" $xmlfield_def | sed -e "s/^.*< *field .*id *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d )
#
# list of variables to be outputed in the xml file
#
varlistout=$( grep "< *field *\([^ ].* \)*field_ref *=" $xmlfile_def | sed -e "s/^.*< *field .*field_ref *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d )
#
echo "--------------------------------------------------"
echo check if all iom_put found in $srcdir
echo have a corresponding variable definition in $xmlfield_def
echo "--------------------------------------------------"
for var in $varlistsrc
do
tst=$( echo " "$varlistxml" " | grep -c " $var " )
if [ $tst -ne 1 ]
then
echo "problem with $var: $tst lines corresponding to its definition in $xmlfield_def, but defined in the code in"
for f in $srclist
do
grep -iH "^[^\!]*call *iom_put *( *[\"\']${var}[\"\'] *," $f
done
echo
fi
done
#
echo "--------------------------------------------------"
echo check if all variables defined in $xmlfile
echo have a corresponding \"call iom_put\" in sources found in $srcdir
echo "--------------------------------------------------"
#
for var in $varlistxml
do
found=$( echo " "$varlistsrc" " | grep -c " $var " )
if [ $found -eq 0 ]
then
echo \"call iom_put\" not found for variable $var
grep "< *field * id *= *[\"\']${var}[\"\']" $xmlfile
echo
fi
done
#
echo "--------------------------------------------------"
echo ${xmlfile}: check if all variables to be outputed in files are really defined...
echo "--------------------------------------------------"
#
# list of variables defined in the xml file
for var in $varlistout
do
found=$( echo " "$varlistxml" " | grep -c " $var " )
[ $found -eq 0 ] && echo variable to be outputed but not defined: $var
done
exit