chk_iomput.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. #------------------------------------------------
  3. #$Id: chk_iomput.sh 4162 2013-11-07 10:19:49Z cetlod $
  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'
  37. echo ' ./chk_iomput.sh'
  38. echo ' ./chk_iomput.sh --help'
  39. echo ' ./chk_iomput.sh ../../CONFIG/ORCA2_LIM/EXP00/iodef.xml "../../NEMO/OPA_SRC/ ../../NEMO/LIM_SRC_2/"'
  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. #
  53. [ ! -f "$xmlfile" ] && echo "$xmlfile not found, we stop..." && exit
  54. for i in $srcdir
  55. do
  56. [ ! -d $i ] && echo "$i is not a directory, we stop..." && exit
  57. done
  58. #
  59. #------------------------------------------------
  60. #
  61. external=$( grep -c "<field_definition *\([^ ].* \)*src=" $xmlfile )
  62. if [ $external -eq 1 ]
  63. then
  64. xmlfield_def=$( grep "<field_definition *\([^ ].* \)*src=" $xmlfile | sed -e 's/.*src="\([^"]*\)".*/\1/' )
  65. xmlfield_def=$( dirname $xmlfile )/$xmlfield_def
  66. else
  67. xmlfield_def=$xmlfile
  68. fi
  69. [ $inxml -eq 1 ] && grep "< *field *\([^ ].* \)*id *=" $xmlfield_def
  70. [ $insrc -eq 1 ] && find $srcdir -name "*.[Ffh]90" -exec grep -iH "^[^\!]*call *iom_put *(" {} \;
  71. [ $(( $insrc + $inxml )) -ge 1 ] && exit
  72. #
  73. #------------------------------------------------
  74. #
  75. # list of file containing "CALL iom_put" in $srcdir
  76. #
  77. srclist=$( find $srcdir -name "*.[Ffh]90" -exec grep -il "^[^\!]*call *iom_put *(" {} \; )
  78. #
  79. # list of variables used in "CALL iom_put"
  80. #
  81. badvarsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -ic iom_put )
  82. if [ $badvarsrc -ne 0 ]
  83. then
  84. echo "The following call to iom_put cannot be checked"
  85. echo
  86. find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -i iom_put | sort -d
  87. echo
  88. fi
  89. varlistsrc=$( find $srcdir -name "*.[Ffh]90" -exec grep -i "^[^\!]*call *iom_put *(" {} \; | sed -e "s/.*iom_put *( *[\"\']\([^\"\']*\)[\"\'] *,.*/\1/" | grep -vi iom_put | sort -d )
  90. #
  91. # list of variables defined in the xml file
  92. #
  93. varlistxml=$( grep "< *field *\([^ ].* \)*id *=" $xmlfield_def | sed -e "s/^.*< *field .*id *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d )
  94. #
  95. # list of variables to be outputed in the xml file
  96. #
  97. varlistout=$( grep "< *field *\([^ ].* \)*field_ref *=" $xmlfile | sed -e "s/^.*< *field .*field_ref *= *[\"\']\([^\"\']*\)[\"\'].*/\1/" | sort -d )
  98. #
  99. echo "--------------------------------------------------"
  100. echo check if all iom_put found in $srcdir
  101. echo have a corresponding variable definition in $xmlfield_def
  102. echo "--------------------------------------------------"
  103. for var in $varlistsrc
  104. do
  105. tst=$( echo " "$varlistxml" " | grep -c " $var " )
  106. if [ $tst -ne 1 ]
  107. then
  108. echo "problem with $var: $tst lines corresponding to its definition in $xmlfield_def, but defined in the code in"
  109. for f in $srclist
  110. do
  111. grep -iH "^[^\!]*call *iom_put *( *[\"\']${var}[\"\'] *," $f
  112. done
  113. echo
  114. fi
  115. done
  116. #
  117. echo "--------------------------------------------------"
  118. echo check if all variables defined in $xmlfile
  119. echo have a corresponding \"call iom_put\" in sources found in $srcdir
  120. echo "--------------------------------------------------"
  121. #
  122. for var in $varlistxml
  123. do
  124. found=$( echo " "$varlistsrc" " | grep -c " $var " )
  125. if [ $found -eq 0 ]
  126. then
  127. echo \"call iom_put\" not found for variable $var
  128. grep "< *field * id *= *[\"\']${var}[\"\']" $xmlfile
  129. echo
  130. fi
  131. done
  132. #
  133. echo "--------------------------------------------------"
  134. echo ${xmlfile}: check if all variables to be outputed in files are really defined...
  135. echo "--------------------------------------------------"
  136. #
  137. # list of variables defined in the xml file
  138. for var in $varlistout
  139. do
  140. found=$( echo " "$varlistxml" " | grep -c " $var " )
  141. [ $found -eq 0 ] && echo variable to be outputed but not defined: $var
  142. done
  143. exit