-
Notifications
You must be signed in to change notification settings - Fork 30
/
run_pylint.sh
executable file
·218 lines (197 loc) · 5.48 KB
/
run_pylint.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
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
#!/bin/bash
# FIXME: Replace this whole goddamn thing with something better
# Optional fast-fail on first error encountered
if [ "$#" -gt "0" ] && [ "$1" == "--FF" ]
then
FF=1
shift
echo "Fast-failing on first error encountered"
else
FF=0
fi
# Red Hat systems still have W0142, dropped in later upstream versions of pylint
if [ -e "/etc/redhat-release" ]
then
if grep -iq 'enterprise' '/etc/redhat-release' || \
grep -iq 'centos' '/etc/redhat-release'
then
SPECIALONE=",W0142"
fi
fi
# Tests need to include autotest modules; make sure we can access them.
if [ -z "$AUTOTEST_PATH" ]
then
python -c 'import autotest'
if [ "$?" -ne "0" ]
then
echo "ERROR: autotest module won't load or " \
"AUTOTEST_PATH env. var. is not set"
exit 1
fi
fi
TMPFILENAME="$(mktemp --suffix=run_pylint)"
PEP8=`type -p pep8`
# for readability, allow multiple spaces after commas, colons & before '='
PEP8IGNORE='E731,E221,E241'
MSGFMT='(pylint) {msg_id}:{line:3d},{column}: {obj}: {msg}'
# General 'bad' stuff that should never appear
# Disable 'line too long' - will be picked up by pep8
# Check "note" (W0511) separately
DISABLEMSG="I0011,R0801,R0904,R0921,R0922,C0301,C0326,W0511${SPECIALONE}"
INIT_HOOK="
AP = os.environ.get('AUTOTEST_PATH', '/usr/local/autotest')
sys.path.append(os.path.abspath(AP + '/..'))
sys.path.append(os.path.abspath('.'))
import autotest
import autotest.common
"
SUBTESTDISABLEMSG="I0011,R0801,R0904,E1101,E1002,R0903,F0401,C0103,C0326,C0111,W0232,C0301,W0511,${SPECIALONE}"
SUBTESTINIT_HOOK="
AP = os.environ.get('AUTOTEST_PATH', '/usr/local/autotest')
sys.path.append(os.path.abspath(AP + '/..'))
sys.path.append(os.path.abspath('.'))
import autotest
import autotest.common
import dockertest
"
declare -a PYLINT_OPTS=(-rn
--output-format=colorized
--rcfile=/dev/null
--deprecated-modules=regsub,TERMIOS,Bastion,rexec,pdb
--msg-template="${MSGFMT}")
# pylint 1.7.1 (f26) has an annoying "Your code has been rated at" message
if pylint --help 2>/dev/null | grep -q -- -score; then
PYLINT_OPTS+=("--score=n")
fi
# Run from top-level dir
MYDIR=$(dirname "$0")
if [ "$PWD" != "$MYDIR" ]
then
cd "$MYDIR"
fi
cleanup() {
rm -f "$TMPFILENAME"
}
trap "cleanup" EXIT
echo "0" > "$TMPFILENAME"
record_return() {
VALUE=$(head -1 "$TMPFILENAME")
if [ "$1" -gt "0" ]
then
echo " ^^^^^Problem(s) need fixing^^^^^" > /dev/stderr
echo
let "VALUE++"
echo "$VALUE" > "$TMPFILENAME"
fi
}
check_print() {
PRINTS=$(egrep --with-filename --line-number '^\s*print\s+' "$1")
if [ -n "$PRINTS" ]
then
echo "Python print statement(s) found:"
echo "$PRINTS"
return 1
fi
return 0
}
# Run a command, checking exit status.
# If command fails: if called with --FF, exit immediately. Otherwise,
# continue but remember to exit with failure at end of tests.
run_ff() {
"$@"
if [ $? -ne 0 ]; then
if [ $FF -ne 0 ]; then
exit 1
fi
record_return 1
fi
}
check_dockertest() {
WHAT="$1"
echo -e "Checking: ${WHAT} "
run_ff pylint "${PYLINT_OPTS[@]}" \
--init-hook="$INIT_HOOK" \
--disable="$DISABLEMSG" \
--max-args=6 \
--min-public-methods=2\
--no-docstring-rgx='(__.*__)|(_.*)|(__init__)' \
"${WHAT}"
# Just print FIXME/TODO warnings, don't fail on them.
pylint "${PYLINT_OPTS[@]}" \
--init-hook="$INIT_HOOK" \
--disable=all \
--enable=W0511 \
"${WHAT}"
if [ -n "$PEP8" ]
then
run_ff $PEP8 --ignore=$PEP8IGNORE "$WHAT"
fi
run_ff check_print "$WHAT"
}
check_dockertests() {
echo -e "\n\n======================================= dockertest"
find dockertest -name '*.py' -a -not -name '*_unittest*.py' | sort | \
while read LINE; do
trap "break" INT
check_dockertest "${LINE}"
done || exit 1
}
check_subtest() {
WHAT="$1"
echo -e "Checking: ${WHAT} "
run_ff pylint "${PYLINT_OPTS[@]}" \
--init-hook="$SUBTESTINIT_HOOK" \
--disable="$SUBTESTDISABLEMSG" \
--max-args=8 \
--max-locals=20 \
--min-public-methods=1\
"${WHAT}"
# Just print FIXME/TODO warnings, don't fail on them.
pylint "${PYLINT_OPTS[@]}" \
--init-hook="$SUBTESTINIT_HOOK" \
--disable=all \
--enable=W0511 \
"${WHAT}"
if [ -n "$PEP8" ]
then
run_ff $PEP8 --ignore=$PEP8IGNORE "$WHAT"
fi
run_ff check_print "$WHAT"
}
check_subtests() {
for thing in pretests subtests intratests posttests
do
trap "break" INT
echo -e "\n\n======================================= ${thing}"
find ${thing} -name '*.py' | sort | while read LINE; do
trap "break" INT
check_subtest "${LINE}"
done || exit 1
done
}
if [ "$#" -eq "0" ]
then
check_dockertests
check_subtests
else
for THING in $@
do
if echo "$THING" | grep -q 'dockertest'
then
check_dockertest "$THING"
elif echo "$THING" | grep -q 'tests'
then
check_subtest "$THING"
else
echo "Ignoring $THING"
fi
done
fi
FAULTS=$(head -1 "$TMPFILENAME")
if [ "$FAULTS" -gt "0" ]
then
echo "Total Faults: $FAULTS"
exit $FAULTS
else
exit 0
fi