forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint-python
executable file
·315 lines (265 loc) · 7.93 KB
/
lint-python
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# define test binaries + versions
FLAKE8_BUILD="flake8"
MINIMUM_FLAKE8="3.9.0"
MINIMUM_MYPY="0.920"
MYPY_BUILD="mypy"
PYTEST_BUILD="pytest"
PYTHON_EXECUTABLE="${PYTHON_EXECUTABLE:-python3}"
BLACK_BUILD="$PYTHON_EXECUTABLE -m black"
function exit_with_usage {
set +x
echo "lint-python - linter for Python"
echo ""
echo "usage:"
cl_options="[--compile] [--black] [--flake8] [--mypy] [--mypy-examples] [--mypy-data]"
echo "lint-python $cl_options"
echo ""
exit 1
}
# Parse arguments
while (( "$#" )); do
case $1 in
--compile)
COMPILE_TEST=true
;;
--black)
BLACK_TEST=true
;;
--flake8)
FLAKE8_TEST=true
;;
--mypy)
MYPY_TEST=true
;;
--mypy-examples)
MYPY_EXAMPLES_TEST=true
;;
--mypy-data)
MYPY_DATA_TEST=true
;;
*)
echo "Error: $1 is not supported"
exit_with_usage
;;
esac
shift
done
if [[ -z "$COMPILE_TEST$BLACK_TEST$FLAKE8_TEST$MYPY_TEST$MYPY_EXAMPLES_TEST$MYPY_DATA_TEST" ]]; then
COMPILE_TEST=true
BLACK_TEST=true
FLAKE8_TEST=true
MYPY_TEST=true
MYPY_EXAMPLES_TEST=true
MYPY_DATA_TEST=true
fi
function satisfies_min_version {
local provided_version="$1"
local expected_version="$2"
echo "$(
"$PYTHON_EXECUTABLE" << EOM
from setuptools.extern.packaging import version
print(version.parse('$provided_version') >= version.parse('$expected_version'))
EOM
)"
}
function compile_python_test {
local COMPILE_STATUS=
local COMPILE_REPORT=
if [[ ! "$1" ]]; then
echo "No python files found! Something is very wrong -- exiting."
exit 1;
fi
# compileall: https://docs.python.org/3/library/compileall.html
echo "starting python compilation test..."
COMPILE_REPORT=$( ("$PYTHON_EXECUTABLE" -B -mcompileall -q -l -x "[/\\\\][.]git" $1) 2>&1)
COMPILE_STATUS=$?
if [ $COMPILE_STATUS -ne 0 ]; then
echo "Python compilation failed with the following errors:"
echo "$COMPILE_REPORT"
echo "$COMPILE_STATUS"
exit "$COMPILE_STATUS"
else
echo "python compilation succeeded."
echo
fi
}
function mypy_annotation_test {
local MYPY_REPORT=
local MYPY_STATUS=
echo "starting mypy annotations test..."
MYPY_REPORT=$( ($MYPY_BUILD \
--namespace-packages \
--config-file python/mypy.ini \
--cache-dir /tmp/.mypy_cache/ \
python/pyspark) 2>&1)
MYPY_STATUS=$?
if [ "$MYPY_STATUS" -ne 0 ]; then
echo "annotations failed mypy checks:"
echo "$MYPY_REPORT"
echo "$MYPY_STATUS"
exit "$MYPY_STATUS"
else
echo "annotations passed mypy checks."
echo
fi
}
function mypy_data_test {
local PYTEST_REPORT=
local PYTEST_STATUS=
echo "starting mypy data test..."
$PYTHON_EXECUTABLE -c "import importlib.util; import sys; \
sys.exit(0 if importlib.util.find_spec('pytest_mypy_plugins') else 1)"
if [ $? -ne 0 ]; then
echo "pytest-mypy-plugins missing. Skipping for now."
return
fi
PYTEST_REPORT=$( (MYPYPATH=python $PYTEST_BUILD \
-c dev/pyproject.toml \
--rootdir python \
--mypy-only-local-stub \
--mypy-ini-file python/mypy.ini \
python/pyspark ) 2>&1)
PYTEST_STATUS=$?
if [ "$PYTEST_STATUS" -ne 0 ]; then
echo "annotations failed data checks:"
echo "$PYTEST_REPORT"
echo "$PYTEST_STATUS"
exit "$PYTEST_STATUS"
else
echo "annotations passed data checks."
echo
fi
}
function mypy_examples_test {
local MYPY_REPORT=
local MYPY_STATUS=
echo "starting mypy examples test..."
MYPY_REPORT=$( (MYPYPATH=python $MYPY_BUILD \
--namespace-packages \
--config-file python/mypy.ini \
--exclude "mllib/*" \
examples/src/main/python/) 2>&1)
MYPY_STATUS=$?
if [ "$MYPY_STATUS" -ne 0 ]; then
echo "examples failed mypy checks:"
echo "$MYPY_REPORT"
echo "$MYPY_STATUS"
exit "$MYPY_STATUS"
else
echo "examples passed mypy checks."
echo
fi
}
function mypy_test {
if ! hash "$MYPY_BUILD" 2> /dev/null; then
echo "The $MYPY_BUILD command was not found. Skipping for now."
return
fi
_MYPY_VERSION=($($MYPY_BUILD --version))
MYPY_VERSION="${_MYPY_VERSION[1]}"
EXPECTED_MYPY="$(satisfies_min_version $MYPY_VERSION $MINIMUM_MYPY)"
if [[ "$EXPECTED_MYPY" == "False" ]]; then
echo "The minimum mypy version needs to be $MINIMUM_MYPY. Your current version is $MYPY_VERSION. Skipping for now."
return
fi
if [[ "$MYPY_TEST" == "true" ]]; then
mypy_annotation_test
fi
if [[ "$MYPY_EXAMPLES_TEST" == "true" ]]; then
mypy_examples_test
fi
if [[ "$MYPY_DATA_TEST" == "true" ]]; then
mypy_data_test
fi
}
function flake8_test {
local FLAKE8_VERSION=
local EXPECTED_FLAKE8=
local FLAKE8_REPORT=
local FLAKE8_STATUS=
if ! hash "$FLAKE8_BUILD" 2> /dev/null; then
echo "The flake8 command was not found. Skipping for now."
return
fi
_FLAKE8_VERSION=($($FLAKE8_BUILD --version))
FLAKE8_VERSION="${_FLAKE8_VERSION[0]}"
EXPECTED_FLAKE8="$(satisfies_min_version $FLAKE8_VERSION $MINIMUM_FLAKE8)"
if [[ "$EXPECTED_FLAKE8" == "False" ]]; then
echo "\
The minimum flake8 version needs to be $MINIMUM_FLAKE8. Your current version is $FLAKE8_VERSION
flake8 checks failed."
exit 1
fi
echo "starting $FLAKE8_BUILD test..."
FLAKE8_REPORT=$( ($FLAKE8_BUILD --append-config dev/tox.ini --count --show-source --statistics .) 2>&1)
FLAKE8_STATUS=$?
if [ "$FLAKE8_STATUS" -ne 0 ]; then
echo "flake8 checks failed:"
echo "$FLAKE8_REPORT"
echo "$FLAKE8_STATUS"
exit "$FLAKE8_STATUS"
else
echo "flake8 checks passed."
echo
fi
}
function black_test {
local BLACK_REPORT=
local BLACK_STATUS=
# Skip check if black is not installed.
$PYTHON_EXECUTABLE -c 'import black' &> /dev/null
if [ $? -ne 0 ]; then
echo "The Python library providing 'black' module was not found. Skipping black checks for now."
echo
return
fi
echo "starting black test..."
BLACK_REPORT=$( ($BLACK_BUILD --config dev/pyproject.toml --check python/pyspark dev python/setup.py) 2>&1)
BLACK_STATUS=$?
if [ "$BLACK_STATUS" -ne 0 ]; then
echo "black checks failed:"
echo "$BLACK_REPORT"
echo "Please run 'dev/reformat-python' script."
echo "$BLACK_STATUS"
exit "$BLACK_STATUS"
else
echo "black checks passed."
echo
fi
}
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
SPARK_ROOT_DIR="$(dirname "${SCRIPT_DIR}")"
pushd "$SPARK_ROOT_DIR" &> /dev/null
PYTHON_SOURCE="$(git ls-files '*.py')"
if [[ "$COMPILE_TEST" == "true" ]]; then
compile_python_test "$PYTHON_SOURCE"
fi
if [[ "$BLACK_TEST" == "true" ]]; then
black_test
fi
if [[ "$FLAKE8_TEST" == "true" ]]; then
flake8_test
fi
if [[ "$MYPY_TEST" == "true" ]] || [[ "$MYPY_EXAMPLES_TEST" == "true" ]] || [[ "$MYPY_DATA_TEST" == "true" ]]; then
mypy_test
fi
echo
echo "all lint-python tests passed!"
popd &> /dev/null