Skip to content
Snippets Groups Projects
chk_iomput.sh 5.97 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed
#!/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