1#! /bin/sh -f 2# 3# Replaces floating point numbers with XXX and then does a diff 4# 5# 6# Method to print out general and script specific options 7# 8print_usage() { 9 10cat >&2 <<EOF 11Usage: $0 [options] file1 file2 12Replaces floating point numbers with XXX and then does a diff 13file1 is typically "expected result" 14 15OPTIONS 16 -m ............. mv file2 to file1 after diffing 17 -M ............. mv file2 to file1 after diffing if it is an alt file 18 -j ............. just use diff without modifying numbers 19 -h ............. print this message 20 -f ............. filter file1 with commands; e.g., 'sort -nr' 21 -F ............. filter file2 with commands; e.g., 'grep -v foo | grep -v bar' 22 -k ............. keep the filter files -- do not delete 23EOF 24 exit $1 25} 26# Unset some environment variables to avoid problems on cygwin 27# See: https://cygwin.com/pipermail/cygwin/2020-March/244153.html 28# Suspect that the problems are with gitlab's environment variables 29unset CI_ENVIRONMENT_NAME 30unset CI_COMMIT_BEFORE_SHA 31unset CI_COMMIT_DESCRIPTION 32unset CI_COMMIT_MESSAGE 33unset CI_CONFIG_PATH 34 35# parse options -- using getopts because it is so easily extensible 36mvfile=false 37mvfile_ifalt=false 38justdiff=false 39filter=false # File2 40filter_output=false # File1 41keep_files=false 42diffargs="" 43while getopts "f:F:hjJ:kmM" opt 44do 45 case $opt in 46 f ) filter_output=true; filter_output_cmd=$OPTARG;; 47 F ) filter=true; filter_cmd=$OPTARG;; 48 j ) justdiff=true;; 49 J ) justdiff=true; diffargs=$OPTARG;; 50 k ) keep_files=true;; 51 m ) mvfile=true;; 52 M ) mvfile_ifalt=true;; 53 h ) print_usage 0;; 54 esac 55done 56shift "$(( $OPTIND - 1 ))" 57 58if [ $# -lt 2 ]; then 59 echo Error! 60 print_usage 1 61fi 62 63# Filter a file and sort output into a file with a unique suffix 64filter_suffix=".filter_tmp" 65filter_file() { 66 file="$1" 67 fcmds="$2" 68 eval "cat ${file} | ${fcmds} > ${file}${filter_suffix}" 69} 70 71if [ "x${RM}" = "x" ]; then RM="rm"; fi 72if [ "x${SED}" = "x" ]; then SED="sed"; fi 73if [ "x${DIFF}" = "x" ]; then DIFF="diff -w"; 74elif [ "`basename ${DIFF}`" = "petscdiff" ]; then DIFF="diff -w"; 75fi 76 77if [ "x${1}" = "x-" ]; then 78 file1=`mktemp -t petscdiff.XXXXXX` 79 $(cat /dev/stdin > ${file1}) 80elif [ -f ${1} -o "${1}" = "/dev/null" ]; then 81 file1=${1} 82else 83 if ${mvfile}; then 84 echo "mv'ing $2 --> $1" 85 # If filter need to filter first 86 if ${filter}; then 87 filter_file "${2}" "${filter_cmd}" 88 mv "${2}${filter_suffix}" "$1" 89 else 90 mv "$2" "$1" 91 fi 92 exit 0 93 else 94 echo Error! file1 check failed: "${1}" 95 exit 1 96 fi 97 if ${mvfile_ifalt}; then 98 echo "mvfile_ifalt" 99 if echo $1 | grep '_alt.out'; then 100 echo "mv'ing $2 --> $1" 101 if ${filter}; then 102 filter_file "${2}" "${filter_cmd}" 103 mv "${2}${filter_suffix}" "$1" 104 else 105 mv "$2" "$1" 106 fi 107 exit 0 108 fi 109 fi 110fi 111 112if [ -f ${2} -o "${2}" = "/dev/null" ]; then 113 file2=${2} 114else 115 echo Error! file2 check failed: "${2}" 116 exit 1 117fi 118 119if ${filter}; then 120 filter_file "${file2}" "${filter_cmd}" 121 file2="${file2}${filter_suffix}" # Will need to remove later 122fi 123result_file=${file1} 124if ${filter_output}; then 125 filter_file "${file1}" "${filter_output_cmd}" 126 file1="${file1}${filter_suffix}" # Will need to remove later 127fi 128 129if ! ${justdiff}; then 130 tmpA=`mktemp -t petscdiffA.XXXXXX` 131 tmpB=`mktemp -t petscdiffB.XXXXXX` 132 133 ${SED} "s/< [-+ ]*[ 0-9][0-9]*\.*[0-9]*[eE][-+][0-9][0-9]*/XXX/g" ${file1} | ${SED} "s/E+000//g" | ${SED} "s/[-+ ]*[-]*[ 0-9][0-9]*\.*[0-9]*[eE][-+][0-9][0-9]*/XXX/g" | ${SED} "s/[-+ ]*[-]*[ 0-9][0-9]*\.[0-9]*/XXX/g" | ${SED} "s/ \*\*\*\*\*\*\*\*\* /XXX/g" > ${tmpA} 134 135 ${SED} "s/< [-+ ]*[ 0-9][0-9]*\.*[0-9]*[eE][-+][0-9][0-9]*/XXX/g" ${file2} | ${SED} "s/E+000//g" | ${SED} "s/[-+ ]*[-]*[ 0-9][0-9]*\.*[0-9]*[eE][-+][0-9][0-9]*/XXX/g" | ${SED} "s/[-+ ]*[-]*[ 0-9][0-9]*\.[0-9]*/XXX/g" | ${SED} "s/ \*\*\*\*\*\*\*\*\* /XXX/g" > ${tmpB} 136 ${DIFF} ${tmpA} ${tmpB} > /dev/null 137 if [ $? -ne 0 ]; then 138 ${DIFF} ${file1} ${file2} 139 err=1 140 else 141 err=0 142 fi 143 ${RM} -f ${tmpA} ${tmpB} 144else 145 ${DIFF} ${diffargs} ${file1} ${file2} 146 err=$? 147fi 148 149if [ "x${1}" = "x-" ]; then 150 ${RM} -f ${file1} 151fi 152 153if ${mvfile} && test ${err} -gt 0; then 154 echo "mv'ing $file2 --> $result_file" 155 mv "$file2" "$result_file" 156fi 157if ${mvfile_ifalt} && test ${err} -gt 0; then 158 if echo $file1 | grep '_alt.out'; then 159 echo "mv'ing $file2 --> $result_file" 160 mv "$file2" "$result_file" 161 fi 162fi 163 164# For debugging filters, it is useful to be able to keep the files 165if ! ${keep_files}; then 166 if ${filter}; then 167 ${RM} -f ${file2} # Temporary file. See above 168 fi 169 if ${filter_output}; then 170 ${RM} -f ${file1} # Temporary file. See above 171 fi 172fi 173 174exit ${err} 175