File: rptdiff.sh

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (266 lines) | stat: -rwxr-xr-x 7,378 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/bash
# This file is part of the xTuple ERP: PostBooks Edition, a free and
# open source Enterprise Resource Planning software suite,
# Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
# It is licensed to you under the Common Public Attribution License
# version 1.0, the full text of which (including xTuple-specific Exhibits)
# is available at www.xtuple.com/CPAL.  By using this software, you agree
# to be bound by its terms.
#
PROG=`basename $0`
OLDDIR=
OLDVER=
NEWDIR=
NEWVER=
TMPDIR="${TMPDIR:-/tmp}/${PROG}_`date +%Y_%m_%d`"
RUNEXTRACT=true	#undocumented feature
XSLT=xsltproc

XSLTFILE=$TMPDIR/rptdiff.xslt

usage () {
  cat > /dev/stderr <<-EOF
	$PROG -h
	$PROG [ -o oldversiondir -n newversiondir ] [ -t temporarydir ]
	$PROG [ -o oldtag -n newtag ] [ -t temporarydir ] reports-subdirs

	-h	print this usage message
	-t	directory in which to store extracted queries [$TMPDIR]

        directory-based comparison (default):
	-o	directory to search for the old versions of the reports
	-n	directory to search for the new versions of the reports

        tag-based comparison (tags should look like vX.Y.Z[optional-suffix]):
	-o	git tag for checking out the old versions of the reports
	-n	git tag for checking out the new versions of the reports
EOF
}

log () {
  echo "$*" > /dev/stderr
}

# x is an undocumented option

while getopts ho:n:t:x OPTION ; do
  case $OPTION in
    h)  usage
        exit 0
        ;;
    o)  if [ -d "$OPTARG" ] ; then
          OLDDIR="$OPTARG"
        elif [[ "$OPTARG" =~ ^v[1-9]+\.[0-9]+\.[0-9]+ ]] ; then
          OLDVER=$OPTARG
        else
          OLDVER=$OPTARG
          log "$PROG: cannot find directory $OPTARG so guessing this is a git commit-ish"
        fi
        ;;
    n)  if [ -d "$OPTARG" ] ; then
          NEWDIR="$OPTARG"
        elif [[ "$OPTARG" =~ ^v[1-9]+\.[0-9]+\.[0-9]+ ]] ; then
          NEWVER=$OPTARG
        else
          NEWVER=$OPTARG
          log "$PROG: cannot find directory $OPTARG so guessing this is a git commit-ish"
        fi
        ;;
    t)  TMPDIR=$2
        XSLTFILE=$TMPDIR/rptdiff.xslt
        shift
        ;;
    x)  RUNEXTRACT=false	#undocumented feature
        ;;
  esac
done
shift $[$OPTIND - 1]

if [ -z "$TMPDIR" ] ; then
  log "$PROG: no temporarydir given"
  usage
  exit 1
elif [ -e "$TMPDIR" -a ! -d "$TMPDIR" ] ; then
  log "$PROG: temporarydir is not a directory"
  usage
  exit 1
elif ! mkdir -p $TMPDIR/old || ! mkdir -p $TMPDIR/new ; then
  exit 2
fi

if [ -n "$OLDDIR" -a -n "$NEWVER" ] ; then
  log "$PROG: please don't mix directories and tags"
  usage
  exit 1
elif [ -n "$OLDVER" -a -n "$NEWDIR" ] ; then
  log "$PROG: please don't mix directories and tags"
  usage
  exit 1
elif [ -n "$OLDDIR" -a -z "$NEWDIR" ] ; then
  log "$PROG: please give two directories"
  usage
  exit 1
elif [ -z "$OLDDIR" -a -n "$NEWDIR" ] ; then
  log "$PROG: please give two directories"
  usage
  exit 1
elif [ -n "$OLDVER" -a -z "$NEWVER" ] ; then
  log "$PROG: please give two version tags"
  usage
  exit 1
elif [ -z "$OLDVER" -a -n "$NEWVER" ] ; then
  log "$PROG: please give two version tags"
  usage
  exit 1
elif [ -z "$OLDVER" -a -z "$NEWVER" -a -z "$OLDDIR" -a -z "$NEWDIR" ] ; then
  log "$PROG: please give two directories or two version tags"
  usage
  exit 1
fi

# convert tag-based to directory-based:
#       check out the code & copy the reports
if [ -n "$OLDVER" ] ; then
  if [ "$OLDVER" = "$NEWVER" ] ; then
    log "$PROG: old version equals new version so doing nothing"
    exit 1
  fi
  if [ $# -lt 1 ] ; then
    log "$PROG: tag-based comparison needs at least one report subdirectory"
    exit 1
  fi
  for VER in $OLDVER $NEWVER ; do
    git checkout $VER                                  || exit 1
    rm -rf $TMPDIR/$VER
    mkdir $TMPDIR/$VER
    for DIR in $* ; do
      if [ `ls $DIR/*.xml | wc -l` -le 0 ] ; then
        log "$PROG: $DIR doesn't seem to have any xml files in it"
      else
        cp $DIR/*.xml $TMPDIR/$VER
      fi
    done
  done

  OLDDIR=$TMPDIR/$OLDVER
  NEWDIR=$TMPDIR/$NEWVER
fi



if [ `ls $OLDDIR/*.xml | wc -l` -le 0 ] ; then
  log "$PROG: $OLDDIR doesn't seem to have any xml files in it"
  exit 1
fi
if [ `ls $NEWDIR/*.xml | wc -l` -le 0 ] ; then
  log "$PROG: $NEWDIR doesn't seem to have any xml files in it"
  exit 1
fi

cat > $XSLTFILE <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output indent="yes" method="text" />

  <xsl:template match="text()"/>

  <xsl:template match="report/name">
====================================================================
REPORT: <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="report/querysource">
QUERY: <xsl:value-of select="name"/><xsl:text>
</xsl:text>
    <xsl:value-of select="sql"     />
    <xsl:if test="mqlgroup">== MetaSQL statement </xsl:if>
    <xsl:value-of select="mqlgroup"/>
    <xsl:if test="mqlname">-</xsl:if>
    <xsl:value-of select="mqlname" />
--------------------------------------------------------------------
  </xsl:template>

</xsl:stylesheet>
EOF

if $RUNEXTRACT ; then
for DIR in $OLDDIR $NEWDIR ; do
  if [ "$DIR" = "$OLDDIR" ] ; then
    DESTDIR="$TMPDIR/old"
  elif [ "$DIR" = "$NEWDIR" ] ; then
    DESTDIR="$TMPDIR/new"
  else
    log "$PROG: I'm confused and don't know where to write my output"
    exit 3
  fi
  if ! rm -f "$DESTDIR/*" ; then
    exit 4
  fi

  FILELIST=`ls $DIR/*.xml`
  if [ -z "$FILELIST" ] ; then
    log no .xml files in $DIR
    exit 2
  fi

  for FILE in $FILELIST ; do
    INPUTFILE=$FILE
    OUTPUTFILE=$DESTDIR/`basename $INPUTFILE`
    log processing $INPUTFILE > /dev/stderr

    if [ "$XSLT" = Xalan ] ; then
      "$XSLT" "$INPUTFILE" "$XSLTFILE" > "$OUTPUTFILE"
    elif [ "$XSLT" = xsltproc ] ; then
      "$XSLT" "$XSLTFILE" "$INPUTFILE" > "$OUTPUTFILE"
    else
      Cannot find XSLT Processor "$XSLT"
      exit 2
    fi

  done
done
fi	# if $RUNEXTRACT

RMLIST=
NEWLIST=
CHANGEDLIST=

if [ -s $TMPDIR/diffs ] && ! rm $TMPDIR/diffs ; then
  exit 3
fi

for FILE in `ls $TMPDIR/old/* $TMPDIR/new/* | xargs -J X -n 1 basename X | sort -u` ; do
  if  [ -f $TMPDIR/old/$FILE -a ! -f $TMPDIR/new/$FILE ] ; then
    REPORTNAME="`head -3 $TMPDIR/old/$FILE | tail -1 | cut -f2 -d:`"
    RMLIST="$RMLIST $REPORTNAME"
  elif [ ! -f $TMPDIR/old/$FILE -a -f $TMPDIR/new/$FILE ] ; then
    REPORTNAME="`head -3 $TMPDIR/new/$FILE | tail -1 | cut -f2 -d:`"
    NEWLIST="$NEWLIST $REPORTNAME"
  elif ! cmp -s $TMPDIR/old/$FILE $TMPDIR/new/$FILE ; then
    REPORTNAME="`head -3 $TMPDIR/new/$FILE | tail -1 | cut -f2 -d:`"
    CHANGEDLIST="$CHANGEDLIST $REPORTNAME"
    diff -w --unchanged-line-format='   %L' \
                  --old-line-format='-  %L' \
                  --new-line-format='+  %L' $TMPDIR/{old,new}/$FILE >> $TMPDIR/diffs
  fi
done

if [ -n "$RMLIST" ] ; then
  echo ====================================================================
  echo REMOVED REPORTS:
  echo $RMLIST | tr -s [:blank:] "\n"
fi

if [ -n "$NEWLIST" ] ; then
  echo ====================================================================
  echo NEW REPORTS:
  echo $NEWLIST | tr -s [:blank:] "\n"
fi

if [ -n "$CHANGEDLIST" ] ; then
  echo ====================================================================
  echo CHANGED REPORTS:
  echo $CHANGEDLIST | tr -s [:blank:] "\n"
  echo
  cat $TMPDIR/diffs
fi