get_tmflist 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/ksh
  2. #
  3. # TM_FLIST
  4. #
  5. # Print list of files used in a TM5 project, as defined by an RC
  6. # file.
  7. #
  8. #
  9. # ARGUMENT : RCFILE : rc file
  10. # ---------- (default is $RCDEFAULT, set in "settings"
  11. # section of the code. If it does not exists, the most
  12. # recent rc file is used)
  13. #
  14. # SETTING : specify your TM5 dir and default rc file in the SETTINGS
  15. # --------- section
  16. #
  17. # REQUIREMENT : the "without" flags in your rc file should be grouped into a "my.without" rc key.
  18. # -------------
  19. #
  20. # EXAMPLES: Assuming that the script is in your $PATH (add current dir to it) :
  21. # ---------
  22. #
  23. # # (1) Print the listing
  24. #
  25. # get_tmflist # default rc file
  26. # get_tmflist mytm5.rc # pycasso rc file
  27. #
  28. #
  29. # # (2) Save the listing, so it can be used in grep/find:
  30. #
  31. # tmfiles=$(get_tmflist my.rc)
  32. #
  33. # grep -i something $tmfiles
  34. # find $tmfiles -name [Tt]racer*
  35. #
  36. #
  37. # LIMITATIONS
  38. # -----------
  39. #
  40. # Filenames that include .svn are not seen.
  41. #
  42. # 3 Jan 2011 - P. Le Sager - v0
  43. # 27 Sep 2011 - P. Le Sager - added rule #3 (applied only for pycasso cases, see REQUIREMENT)
  44. # 11 Jan 2013 - P. Le Sager - bug fix in location of rc.py
  45. # 13 Oct 2014 - Ph. Le Sager - adapted for new repository
  46. #
  47. ########################################################
  48. USAGE="\
  49. usage: ${0##*/} [-d ROOTDIR] [RCFILE]
  50. "
  51. #----------------
  52. # DEFAULT SETTINGS
  53. #----------------
  54. # TM5 dir
  55. TM=$HOME/TM5MP
  56. # default RC file (leave empty to use the latest .rc, if you do not pass an
  57. # argument). Filename is relative to $TM.
  58. #rcdefault='base/trunk/rc/pycasso-tm5_chembase.rc'
  59. rcdefault='vais.rc'
  60. pycasso=1
  61. # ------------------------------
  62. # rule #3...
  63. # ------------------------------
  64. function to_remove {
  65. # Return list of files to remove according to rc file settings.
  66. # From rc/expert-default.rc, we should limit the file list if some flags are not used:
  67. aa="with_tmm_tmpp \
  68. with_tmm_tm5 with_tmm_ecmwf \
  69. with_tmm_ncep with_tmm_msc \
  70. with_prism with_tendencies \
  71. with_budgets with_cariolle with_ecearth_optics with_gribex with_grib_api \
  72. with_udunits with_cf_output with_optics with_pm"
  73. # used macro
  74. used=$($cmd $rcfile build.configure.macro.define)
  75. # find the not used flags, and get list of files associated with them
  76. printf "%s\n" $aa | sort > temp1
  77. printf "%s\n" $used | sort > temp2
  78. notused=$(comm -23 temp1 temp2)
  79. for flag in $notused; do
  80. nofiles="$nofiles $($cmd $rcfile build.configure.remove.ifndef.$flag)"
  81. done
  82. # the "without"
  83. without=$($cmd $rcfile my.without)
  84. for wo in $without; do
  85. nofiles="$nofiles $($cmd $rcfile build.configure.remove.ifdef.$wo)"
  86. done
  87. \rm -f temp1 temp2
  88. print $nofiles
  89. return 0
  90. }
  91. # ------------------------------
  92. # options
  93. # ------------------------------
  94. while getopts :d: OPT; do
  95. case $OPT in
  96. d)
  97. TM="$OPTARG"
  98. ;;
  99. *)
  100. print -u2 "$USAGE"
  101. exit 2
  102. esac
  103. done
  104. shift $(( OPTIND - 1 ))
  105. cd $TM
  106. # use the latest rc.py
  107. cmd=${TM}/tools/ttb/bin/rc.py
  108. # default RC file, if not passed
  109. rcfile=${1-${rcdefault}}
  110. # Use the most recent rc file if not passed and default does not exist
  111. [[ -f $rcfile ]] || rcfile=$(ls -1trL *.rc | tail -1)
  112. [[ -z $rcfile ]] && echo "No RC file. Returning..." && return 1
  113. # -----------------------------------------
  114. # dirs = Array of source directories (rule #1)
  115. # -----------------------------------------
  116. set -A dirs $($cmd $rcfile build.copy.dirs)
  117. # -----------------------------------------
  118. # List of files in the project (ignoring .svn dir and swap files).
  119. # Ordered by starting from end of the dir list.
  120. # -----------------------------------------
  121. tempfile=todelete.$$
  122. klm=$((${#dirs[*]}-1))
  123. if [[ $klm -eq 0 ]]; then
  124. echo " no source directory found"
  125. exit 1
  126. fi
  127. while [[ ${klm} -ge 0 ]]; do
  128. # debug
  129. # echo $klm ${dirs[$klm]}
  130. searchdir=$TM/${dirs[$klm]}
  131. [[ ! -e $searchdir ]] && ((klm-=1)) && continue
  132. find "$searchdir" -name \.svn -prune -o \( -type f ! -name "*~" \) | egrep -v "\.svn" >> $tempfile
  133. ((klm-=1))
  134. done
  135. # -----------------------------------------
  136. # Remove overwritten files (rule #2) and to-remove files (rule #3)
  137. # -----------------------------------------
  138. toremove=$(to_remove)
  139. # printf '%s\n' $toremove
  140. # print ""
  141. ii=0
  142. unset list qlist TM5_FLIST
  143. { while read fname
  144. do
  145. # rule #2 : effective name according to TM5 rules
  146. nfile=$(basename "$fname" | sed "s,__.*\.,\.,")
  147. # rule #3
  148. if echo $toremove | grep " $nfile " > /dev/null; then continue; fi
  149. # build lists with uniq file names
  150. if [[ $ii == 0 ]]; then
  151. list[$ii]=$nfile
  152. qlist[$ii]=$fname
  153. jj=$ii
  154. else
  155. noskip=1
  156. for k in $(seq 0 $jj) ; do
  157. if [[ $nfile == ${list[$k]} ]]; then
  158. noskip=0
  159. break
  160. fi
  161. done
  162. (( $noskip )) && ((jj+=1)) && list[$jj]=$nfile && qlist[$jj]=$fname
  163. fi
  164. ((ii+=1))
  165. done } < $tempfile
  166. # export array
  167. # export TM5_FLIST=$( printf "%s " "${qlist[@]}" )
  168. # export TM5_RCFILE=$rcfile
  169. # cleanup & output
  170. \rm -f $tempfile
  171. printf '%s\n' ${qlist[*]}
  172. return