]>
Commit | Line | Data |
---|---|---|
8ab43d1b MW |
1 | #!/bin/sh |
2 | ############################################################################# | |
3 | # Name: check.sh | |
e09c4c1d | 4 | # Purpose: Offline checker for the buildbot configuration files |
8ab43d1b | 5 | # Author: Mike Wetherell |
8ab43d1b | 6 | # Copyright: (c) 2007 Mike Wetherell |
526954c5 | 7 | # Licence: wxWindows licence |
8ab43d1b MW |
8 | ############################################################################# |
9 | ||
10 | usage() { | |
11 | echo "Usage: $0 [options] FILE..." | |
e09c4c1d | 12 | echo "Offline checker for the buildbot configuration files" |
8ab43d1b MW |
13 | echo |
14 | echo "Options:" | |
15 | echo " -g generate xslt" | |
16 | echo " -h show help" | |
17 | echo " -l N output only line N" | |
18 | echo " -p preprocess" | |
19 | echo " -v validate" | |
20 | exit 0 | |
21 | } | |
22 | ||
23 | badopt() { | |
24 | echo "try '$0 -h' for more information" >&2 | |
25 | exit 1 | |
26 | } | |
27 | ||
e09c4c1d MW |
28 | progcheck() { |
29 | prog="$1" | |
30 | $prog --version >/dev/null 2>&1 || { | |
31 | echo "$0: requires $prog, not found" >&2 | |
32 | exit 1 | |
33 | } | |
34 | } | |
35 | ||
8ab43d1b MW |
36 | GENERATE=1 |
37 | PREPROCESS=2 | |
38 | VALIDATE=3 | |
39 | ||
40 | MODE=$VALIDATE | |
41 | FILTER=cat | |
42 | ||
43 | while getopts ghl:pv opt; do | |
44 | case "$opt" in | |
45 | \?) badopt ;; | |
46 | g) MODE=$GENERATE ;; | |
47 | h) usage ;; | |
48 | l) FILTER="sed -ne ${OPTARG}p" ;; | |
49 | p) MODE=$PREPROCESS ;; | |
50 | v) MODE=$VALIDATE ;; | |
51 | esac | |
52 | done | |
53 | ||
54 | if [ $OPTIND -gt 1 ]; then | |
55 | shift `expr $OPTIND - 1` | |
56 | fi | |
57 | ||
58 | if [ $# -eq 0 ]; then | |
59 | usage | |
60 | fi | |
61 | ||
e09c4c1d MW |
62 | XSLTPROC=xsltproc |
63 | XMLLINT=xmllint | |
64 | ||
65 | progcheck $XSLTPROC | |
66 | progcheck $XMLLINT | |
67 | ||
8ab43d1b MW |
68 | DIR="`dirname $0`" |
69 | WORKDIR="${TMPDIR:-/tmp}/wx.$$" | |
70 | mkdir "$WORKDIR" || exit | |
71 | trap 'rm -rf "$WORKDIR"' EXIT | |
72 | WORKPAT=`echo "$WORKDIR" | sed 's|[^A-Za-z0-9]/|.|g'` | |
73 | XSLT="$WORKDIR/XSLT" | |
74 | PREP="$WORKDIR/PREP" | |
75 | STDERR="$WORKDIR/STDERR" | |
76 | ERR=0 | |
77 | ||
fa5a0e70 MW |
78 | # Filter to show lines of genertated XSLT when they are mentioned. |
79 | showxslt() { | |
80 | awk '{ | |
81 | print; | |
82 | if (sub(/.*generated XSLT line */, "") && sub(/[^0-9].*/, "")) | |
83 | { | |
84 | system("sed -ne "$0"p '$XSLT'"); | |
85 | } | |
86 | }' | |
87 | } | |
88 | ||
89 | # Test it works as old version of awk don't have sub or system functions. | |
90 | if showxslt </dev/null 2>/dev/null; then | |
91 | SHOWXSLT=showxslt | |
92 | else | |
93 | SHOWXSLT=cat | |
94 | fi | |
95 | ||
8ab43d1b MW |
96 | # Change the names of the temporary files in an error message to something |
97 | # to something more informative | |
98 | errout() | |
99 | { | |
100 | NAME="$1" | |
101 | ||
102 | if [ -s "$STDERR" ]; then | |
103 | sed "s|file ${WORKPAT}|${WORKPAT}|g;\ | |
104 | s|${WORKPAT}/XSLT|generated XSLT|g;\ | |
fa5a0e70 | 105 | s|${WORKPAT}/PREP|$NAME (preprocessed)|g" "$STDERR" | $SHOWXSLT |
8ab43d1b MW |
106 | fi |
107 | } | |
108 | ||
109 | output() | |
110 | { | |
111 | $FILTER "$1" | |
112 | } | |
113 | ||
114 | generate() | |
115 | { | |
116 | INPUT="$1" | |
117 | ||
e09c4c1d | 118 | if $XSLTPROC --xinclude -o "$XSLT" $DIR/embedded.xsl "$INPUT" 2>"$STDERR" && |
8ab43d1b MW |
119 | test \! -s "$STDERR" |
120 | then | |
121 | if [ $MODE -eq $GENERATE ]; then | |
122 | output "$XSLT" | |
123 | fi | |
124 | else | |
125 | return 1 | |
126 | fi | |
127 | } | |
128 | ||
129 | preprocess() | |
130 | { | |
131 | INPUT="$1" | |
132 | ||
133 | if [ $MODE -lt $PREPROCESS ]; then | |
134 | return 0 | |
135 | fi | |
136 | ||
e09c4c1d | 137 | if $XSLTPROC --xinclude -o "$PREP" "$XSLT" "$INPUT" 2>"$STDERR" && |
8ab43d1b MW |
138 | test \! -s "$STDERR" |
139 | then | |
140 | if [ $MODE -eq $PREPROCESS ]; then | |
141 | output "$PREP" | |
142 | fi | |
143 | else | |
144 | return 1 | |
145 | fi | |
146 | } | |
d5940c63 | 147 | |
8ab43d1b MW |
148 | validate() |
149 | { | |
150 | NAME="$1" | |
151 | ||
152 | if [ $MODE -lt $VALIDATE ]; then | |
153 | return 0 | |
154 | fi | |
155 | ||
e09c4c1d | 156 | if $XMLLINT --noout --schema $DIR/bot.xsd "$PREP" 2>"$STDERR" |
8ab43d1b MW |
157 | then |
158 | errout "$NAME" | |
159 | else | |
160 | return 1 | |
161 | fi | |
162 | } | |
163 | ||
164 | while [ $# -gt 0 ]; do | |
165 | INPUT="$1" | |
166 | NAME="`echo \"$INPUT\" | sed 's/[|\]/\\\&/g'`" | |
167 | ||
168 | { | |
169 | generate "$INPUT" && | |
170 | preprocess "$INPUT" && | |
171 | validate "$NAME" | |
172 | } || { | |
173 | errout "$NAME" >&2 | |
174 | ERR=1 | |
175 | } | |
d5940c63 | 176 | |
8ab43d1b MW |
177 | rm -f "$XSLT" "$PREP" "$STDERR" |
178 | ||
179 | shift | |
180 | done | |
181 | ||
182 | exit $ERR |