forked from STIXProject/stixproject.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_idioms.sh
executable file
·136 lines (115 loc) · 3.51 KB
/
test_idioms.sh
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
#!/bin/bash
: ${VERBOSE:=1}
# Validate that VERBOSE is an integer to prevent
# code injection exploits.
# Prolly overkill for this little dumb script...
if [[ "$VERBOSE" == *[!0-9]* ]] ; then
echo "VERBOSE is expected to be numeric!"
exit 1
fi
validator=stix-validator.py
realpath() { echo $(cd $(dirname $1); pwd)/$(basename $1); }
log()
{
(( VERBOSE != 0 )) && echo -e "$1"
}
# Run with redirection of stderr according to the
# verbosity setting.
run_redir()
{
if (( VERBOSE != 0 )) ; then
"$@"
else
"$@" 2> /dev/null
fi
}
run_producer_consumer()
{
local producer="$1"
local stem="${producer%producer.py}"
local consumer="${stem}consumer.py"
local outfile="${stem}out.xml"
local parsedfile="${stem}parsed.txt"
/bin/rm -f "$outfile" "$parsedfile"
log " Producer: $producer"
run_redir python "$producer" > "$outfile"
if (( $? != 0 )) || [[ ! -e "$outfile" ]] ; then
return 1
fi
if [[ -f "$consumer" ]] ; then
log " Consumer: $consumer"
run_redir python "$consumer" "$outfile" > "$parsedfile"
if (( $? != 0 )) || [[ ! -e "$parsedfile" ]] ; then
return 2 # special status meaning consumer error
fi
fi
return 0
}
# User can specify their own idiom dirs if they want.
if (( $# > 0 )) ; then
IDIOM_DIRS="$@"
else
IDIOM_DIRS=./documentation/idioms/*
fi
# Not every idiom dir has any *.py files.
# Let fileglobs expand to "" in that case.
shopt -s nullglob
RETVAL=0
for dir in $IDIOM_DIRS; do
[[ -d "$dir" ]] || continue
log "Idiom: $dir"
pushd "$dir" > /dev/null #change to idiom directory
# Assume sets of paired scripts, named *producer.py and
# *consumer.py. The output of *producer.py is checked
# with *consumer.py.
#
# Any misc scripts which don't conform to this naming
# convention are just run and checked for exit status.
#
# Dangling producer scripts are simply run for exit status.
# Dangling consumer scripts are ignored.
for scriptfile in *.py ; do
case "$scriptfile" in
*producer.py)
run_producer_consumer "$scriptfile"
status=$?
if (( status != 0 )) ; then
# disambiguate whether producer or consumer failed
(( status == 1 )) && ERR_FILE="$scriptfile" ||
ERR_FILE="${scriptfile%producer.py}consumer.py"
echo -e "\nERROR running $(realpath $ERR_FILE)"
RETVAL=1
fi
;;
*consumer.py)
continue
;;
*)
log " Run: $scriptfile"
run_redir python "$scriptfile" > /dev/null
if (( $? != 0 )) ; then
echo -e "\nERROR running $(realpath $scriptfile)"
RETVAL=1
fi
;;
esac
# in verbose mode, the "."s would be drowned out
# by log messages and be kinda pointless...
(( VERBOSE == 0 )) && echo -n "."
done
for xmlfile in ./*.xml ; do
log " Validate: $xmlfile"
run_redir "$validator" "$xmlfile" > /dev/null
if (( $? != 0 ))
# the file had validation errors
then
echo -e "\nERROR: $(realpath $xmlfile) had validation errors"
RETVAL=1
else
(( VERBOSE == 0 )) && echo -n "."
fi
done
popd > /dev/null #return to root dir
done
echo -e "\nDone!"
exit $RETVAL