forked from Guake/guake
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.sh
executable file
·212 lines (188 loc) · 5.45 KB
/
validate.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
#!/bin/bash
if [[ ! -z $1 ]] && [[ $1 == "--help" ]]; then
echo "USAGE: validate.sh [oldrev [--quick]]"
echo " This script will test a set of patches (oldrev..HEAD) for basic acceptability as a patch"
echo " Run it in an activated virtualenv with the current Buildbot installed, as well as"
echo " sphinx, pyflakes, mock, and so on"
echo "To use a different directory for tests, pass TRIALTMP=/path as an env variable"
echo "if --quick is passed validate will skip unit tests and concentrate on coding style"
echo
echo "If no argument is given, all files from current directory will be inspected"
exit 1
fi
REVRANGE=
if [ ! -z $1 ]; then
REVRANGE="$1..HEAD"
fi
TRIAL_TESTS=
# some colors
# plain
_ESC=$'\e'
GREEN="$_ESC[0;32m"
MAGENTA="$_ESC[0;35m"
RED="$_ESC[0;31m"
LTCYAN="$_ESC[1;36m"
YELLOW="$_ESC[1;33m"
NORM="$_ESC[0;0m"
function status()
{
echo "${LTCYAN}-- ${*} --${NORM}"
}
slow=true
if [[ $2 == '--quick' ]]; then
slow=false
fi
ok=true
problem_summary=""
function not_ok()
{
ok=false
echo "${RED}** ${*} **${NORM}"
problem_summary="$problem_summary"$'\n'"${RED}**${NORM} ${*}"
}
function warning()
{
echo "${YELLOW}** ${*} **${NORM}"
problem_summary="$problem_summary"$'\n'"${YELLOW}**${NORM} ${*} (warning)"
}
function check_tabs()
{
git diff "$REVRANGE" | grep -q $'+.*\t'
}
function check_relnotes()
{
if git diff --exit-code "$REVRANGE" master/docs/relnotes/index.rst >/dev/null 2>&1; then
return 1
else
return 0
fi
}
function run_tests()
{
if [ -z $TRIAL_TESTS ]; then
return
fi
if [ -n "${TRIALTMP}" ]; then
TEMP_DIRECTORY_OPT="--temp-directory ${TRIALTMP}"
fi
find . -name \*.pyc -exec rm {} \;
trial --reporter text ${TEMP_DIRECTORY_OPT} ${TRIAL_TESTS}
}
if [ -z $REVRANGE ]; then
py_files=$(find . -name "*.py" -o -name "*.py.in" | grep -v -E 'src/guake/globals.py$' | grep -v -E 'doc/src/conf.py$')
echo "Validating files: "
echo $py_files
else
if ! git diff --no-ext-diff --quiet --exit-code; then
not_ok "changed files in working copy"
if $slow; then
exit 1
fi
fi
# get a list of changed files, used below; this uses a tempfile to work around
# shell behavior when piping to 'while'
tempfile=$(mktemp)
trap 'rm -f ${tempfile}' 1 2 3 15
git diff --name-only $REVRANGE | grep -E '(src/guake\/guake$|\.py$)' | grep -v '\(^master/\(contrib\|docs\)\|/setup\.py\)' > ${tempfile}
py_files=()
while read line; do
if test -f "${line}"; then
py_files+=($line)
fi
done < ${tempfile}
echo "${MAGENTA}Validating the following commits:${NORM}"
git --no-pager log "$REVRANGE" --pretty=oneline || exit 1
if $slow; then
status "running tests"
run_tests || not_ok "tests failed"
fi
status "checking formatting"
check_tabs && not_ok "$REVRANGE adds tabs"
status "checking for release notes"
check_relnotes || warning "$REVRANGE does not add release notes"
fi
status "checking import module convention in modified files"
RES=true
for filename in ${py_files[@]}; do
if ! python fiximports.py "$filename"; then
echo "cannot fix imports of $filename"
RES=false
fi
done
$RES || warning "some import fixes failed -- not enforcing for now"
status "running autopep8"
if [[ -z `which autopep8` ]]; then
warning "autopep8 is not installed"
elif [[ ! -f pep8rc ]]; then
warning "pep8rc not found"
else
changes_made=false
for filename in ${py_files[@]}; do
LINEWIDTH=$(grep -E "max-line-length" pep8rc | sed 's/ //g' | cut -d'=' -f 2)
# even if we dont enforce errors, if they can be fixed automatically, thats better..
IGNORES=W6
# ignore is not None for SQLAlchemy code..
if [[ "$filename" =~ "/db/" ]]; then
IGNORES=$IGNORES,E711,E712
fi
autopep8 --in-place --max-line-length=$LINEWIDTH --ignore=$IGNORES "$filename"
if ! git diff --quiet --exit-code "$filename"; then
changes_made=true
fi
done
if ${changes_made}; then
not_ok "autopep8 made changes"
fi
fi
status "running pep8"
if [[ -z `which pep8` ]]; then
warning "pep8 is not installed"
elif [[ ! -f pep8rc ]]; then
warning "pep8rc not found"
else
pep8_ok=true
for filename in ${py_files[@]}; do
if ! pep8 --config=pep8rc "$filename"; then
pep8_ok=false
fi
done
$pep8_ok || not_ok "pep8 failed"
fi
status "running pyflakes"
if [[ -z `which pyflakes` ]]; then
warning "pyflakes is not installed"
else
pyflakes_ok=true
for filename in ${py_files[@]}; do
if ! pyflakes "$filename"; then
pyflakes_ok=false
fi
done
$pyflakes_ok || not_ok "pyflakes failed"
fi
status "running pylint"
if [[ -z `which pylint` ]]; then
warning "pylint is not installed"
elif [[ ! -f pylintrc ]]; then
warning "pylintrc not found"
else
pylint_ok=true
for filename in ${py_files[@]}; do
if ! pylint --rcfile=pylintrc --disable=R,line-too-long --enable=W0611 --output-format=text --report=no "$filename"; then
pylint_ok=false
fi
done
$pylint_ok || not_ok "pylint failed"
fi
echo ""
if $ok; then
if [ -z "${problem_summary}" ]; then
echo "${GREEN}GOOD!${NORM}"
else
echo "${YELLOW}WARNINGS${NORM}${problem_summary}"
fi
exit 0
else
echo "${RED}NO GOOD!${NORM}${problem_summary}"
exit 1
fi