From f7b0b7ca71c6b1a4291b9f43355ba15f02eb9a5f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 9 Apr 2010 16:55:47 +0200 Subject: [PATCH] STYLE: drop 'getopt' in favour of hand-rolled option parsing - improves flexibility and allows more consistent long options --- bin/foamLog | 339 ++++++++---------- bin/foamUpdateCaseFileHeader | 109 +++--- tutorials/Alltest | 59 +-- .../biconic25-55Run35/sampleCone | 28 +- 4 files changed, 259 insertions(+), 276 deletions(-) diff --git a/bin/foamLog b/bin/foamLog index a5d2ecf254..e0e316784d 100755 --- a/bin/foamLog +++ b/bin/foamLog @@ -31,35 +31,36 @@ # Bugs # -solution singularity not handled #------------------------------------------------------------------------------ +Script=${0##*/} +toolsDir=${0%/*}/tools -PROGDIR=`dirname $0` -PROGNAME=`basename $0` -DBFILE=${PROGNAME}.db +usage() { + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + cat < + -list lists but does not extract + -n create single column files with the extracted data only + -quiet quiet operation + -help print the usage -Usage: $PROGNAME [-n][-s] - extracts xy files from log - $PROGNAME -l - lists but does not extract - $PROGNAME -h - for a help message +$Script - extracts xy files from OpenFOAM logs. USAGE + exit 1 } +#------------------------------------------------------------------------------ printHelp() { -printUsage -cat <_, for every specified, for every occurrence inside @@ -79,7 +80,7 @@ separated with '/' : Column 2 is the extended regular expression (egrep) to select the line. Column 3 is the string (fgrep) to select the column inside the line. The value taken will be the first (non-space)word after this column. -The database ($PROGNAME.db) will taken from these locations: +The database ($Script.db) will taken from these locations: . $HOME/.OpenFOAM/$WM_PROJECT_VERSION @@ -87,61 +88,97 @@ The database ($PROGNAME.db) will taken from these locations: $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION $WM_PROJECT_INST_DIR/site $WM_PROJECT_DIR/etc - $PROGDIR/tools + $toolsDir -Option -s suppresses the default information and only prints the extracted +Option -q suppresses the default information and only prints the extracted variables. +----------------------------------------------------------------------------- +HELP -LABHELP +usage } -# The various places to be searched: -for i in \ - . \ - $HOME/.OpenFOAM/$WM_PROJECT_VERSION \ - $HOME/.OpenFOAM \ - $WM_PROJECT_INST_DIR/site/$WM_PROJECT_VERSION \ - $WM_PROJECT_INST_DIR/site \ - $WM_PROJECT_DIR/etc \ - $PROGDIR/tools \ - ; +timeName=Time +unset listOpt quietOpt + +# parse options +while [ "$#" -gt 0 ] do - if [ -r $i/$DBFILE ] - then - DBFILE="$i/$DBFILE" + case "$1" in + -h | -help) + printHelp + exit 0 + ;; + -n) + unset timeName + shift + ;; + -l | -list) + listOpt=true + shift + ;; + -q | -quiet | -s | -silent) + quietOpt=true + shift + ;; + -*) + usage "unknown option: '$*'" + ;; + *) break - fi + ;; + esac done +# find the database file +DBFILE=$Script.db +[ -f $DBFILE ] || DBFILE=`foamEtcFile $Script.db` || DBFILE=$toolsDir/$Script.db -myEcho() { - if [ "$VERBOSE" ] - then - echo "$*" - fi +# need the database file +[ -f $DBFILE ] || { + echo "$Script: Cannot read database $DBFILE" + exit 1 } +# single logFile +if [ $# -eq 1 ] +then + LOG=$1 + [ -r "$LOG" ] && [ -f "$LOG" ] || usage "Cannot read log $LOG" +else + usage +fi + + +myEcho() +{ + [ "$quietOpt" = true ] || echo "$*" +} + + # getSolvedVars logFile # Prints names of all 'solved for' variables in the log file. -getSolvedVars() { +getSolvedVars() +{ fgrep ' Solving for ' $1 | fgrep ',' | sed -e 's/.* Solving for \([^,]*\)[,:].*/\1/' | sort -u } # getQueries dbFile queryName # Gets regular expressions for a certain queryName from the database -getQueries() { - if [ ! -f "$1" ] - then - echo "Cannot find dbFile $1" - exit 1 - fi - +getQueries() +{ + dbFile=$1 queryName=$2 - LINEQ=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $2}'` - NUMQ=`grep -v '^#' $1 | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'` + [ -f "$dbFile" ] || { + echo "Cannot find dbFile $dbFile" + exit 1 + } + + LINEQ=`grep -v '^#' $dbFile | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $2}'` + NUMQ=`grep -v '^#' $dbFile | awk -F '/' "/$queryName/ {if (\"$queryName\" "'!= $1) next; print $3}'` #echo "For $queryName found line selection /$LINEQ/ , column selection /$NUMQ/" 1>&2 #if [ ! "$LINEQ" -o ! "$NUMQ" ] @@ -153,14 +190,16 @@ getQueries() { # getDbQueryList dbFile # Echoes list of possible queries -getDbQueryList() { +getDbQueryList() +{ grep -v '^#' $1 | grep '[^ \t]' | awk -F '/' '{print $1}' } # getSolveQueryList logFile # Echoes list of queries from "solved for" variables in log file -getSolveQueryList() { +getSolveQueryList() +{ solvedVars=`getSolvedVars $1` for var in $solvedVars @@ -174,7 +213,8 @@ getSolveQueryList() { # getAllQueries dbFile logFile # Gets all queries from database and from logfile -getAllQueries() { +getAllQueries() +{ #-- All solved for queries from log file queries=`getSolveQueryList $2` @@ -208,107 +248,33 @@ getAllQueries() { # Main #----------------------------- -# sort arguments -TIMENAME='Time' -VERBOSE='yes' -LISTONLY='' - -while getopts nslh flags -do - case $flags in - n) - TIMENAME="" - ;; - h) - printHelp - exit 0 - ;; - s) - VERBOSE="" - ;; - l) - LISTONLY='yes' - ;; - \?) - printUsage - exit 1 - ;; - esac -done - - -# Shift options -shift `expr $OPTIND - 1` - -if [ ! -f $DBFILE ] +if [ "$listOpt" = true ] then - echo "$PROGNAME: Cannot read database $DBFILE" - exit 1 -fi - -if [ "$LISTONLY" ] -then - if [ $# -ne 1 ] - then - printUsage - exit 1 - fi - LOG=$1; - if [ ! -r $LOG ] - then - echo "$PROGNAME: Cannot read log $LOG" - exit 1 - fi getAllQueries $DBFILE $LOG exit 0 fi -if [ $# -ne 1 ] -then - printUsage - exit 1 -fi +caseDir=. +outputDir=$caseDir/logs -CASEDIR=. -LOG=$1 -if [ ! -r $LOG ] -then - echo "$PROGNAME: Cannot read log $LOG" +[ -d "$caseDir" ] || { + echo "$Script: Cannot read $caseDir" exit 1 -fi +} QUERYNAMES=`getAllQueries $DBFILE $LOG` - -if [ ! "$CASEDIR" ] -then - printUsage - exit 1 -fi - -if [ ! -d "$CASEDIR" ] -then - echo "$PROGNAME: Cannot read $CASEDIR" - exit 1 -fi - -if [ ! -f "$LOG" ] -then - echo "$PROGNAME: Cannot read log file $LOG" - exit 1 -fi - - -#-- Make logs dir in case directory and put awk file there. - -mkdir -p $CASEDIR/logs -AWKFILE=$CASEDIR/logs/$PROGNAME.awk +# +# Make logs dir in case directory and place awk file there +# +mkdir -p $outputDir +AWKFILE=$outputDir/$Script.awk myEcho "Using:" myEcho " log : $LOG" myEcho " database : $DBFILE" myEcho " awk file : $AWKFILE" -myEcho " files to : $CASEDIR/logs" +myEcho " files to : $outputDir" myEcho "" @@ -316,22 +282,25 @@ myEcho "" # Generate Awk program #----------------------------- -#-- header +rm -f $AWKFILE 2> /dev/null +cat << AWK_CONTENTS > $AWKFILE +# header +BEGIN { + Iteration=0 + resetCounters() +} -rm -f $AWKFILE; touch $AWKFILE -echo "BEGIN {" >> $AWKFILE -echo " Iteration=0" >> $AWKFILE -echo " resetCounters()" >> $AWKFILE -echo "}" >> $AWKFILE +# reset counters used for variable postfix +function resetCounters() { +AWK_CONTENTS +# ---------- -echo "" >> $AWKFILE -echo "# reset counters used for variable postfix" >> $AWKFILE -echo "function resetCounters() {" >> $AWKFILE for queryName in $QUERYNAMES do varName=${queryName}Cnt echo " ${varName}=0" >> $AWKFILE done + echo " # Reset counters for general Solving for extraction" >> $AWKFILE echo " for (varName in subIter)" >> $AWKFILE echo " {" >> $AWKFILE @@ -341,10 +310,9 @@ echo "}" >> $AWKFILE echo "" >> $AWKFILE -cat <