-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlint.sh
More file actions
executable file
·104 lines (86 loc) · 2.69 KB
/
lint.sh
File metadata and controls
executable file
·104 lines (86 loc) · 2.69 KB
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
#!/bin/sh
# Usage:
# ./lint.sh [--add-init.py] [--changed] [--bandit] [--pylint] [--pycodestyle] [--pydoctor] [--pydocstyle] [file, ...]
# shellcheck disable=SC2086
# SC2086 (info): Double quote to prevent globbing and word splitting.
# Reason: The unquoted files are files that need to be processed as separate words.
set -eu
script_path=$(cd "$(dirname "${0}")" && pwd)
# Adds __init.py__ files to all directories that are missing __init__.py files.
add_init_py() {
find . -type d \
-not -path './.git*' \
-exec test ! -f '{}/__init__.py' ';' \
-exec touch '{}/__init__.py' ';' \
-exec echo '{}/__init__.py' ';'
}
files_to_search='forge src tests'
linters=''
while test ${#} -gt 0; do
if test "${1-}" = '--add-init.py'; then
add_init_py
exit 0
elif test "${1-}" = '--changed'; then
# Check only the files that were changed in this branch.
files_to_search="$(git diff --name-only "$(git merge-base origin/master "$(git rev-parse --abbrev-ref HEAD)")")"
shift
elif echo "${1-}" | grep -E '^--'; then
# Run linter.
linter=$(echo "${1}" | sed 's/^--//')
linters="${linters} ${linter}"
shift
else
break
fi
done
if test -z "${linters}"; then
# If no linters were given, run all.
linters='bandit pycodestyle pydocstyle pydoctor pylint'
fi
cd "${script_path}"
if test ${#} -eq 0; then
PY_FILES="$(find ${files_to_search} -name '*.py' -or -name 'forge' | sort -uV)"
if test -z "${PY_FILES}"; then echo "No python scripts to check. Exiting early."; exit 0; fi
else
# Check only the given files.
PY_FILES="${*}"
fi
if [ -f /proc/cpuinfo ]; then
procs=$(grep -Fc proc < /proc/cpuinfo)
else
procs=4
fi
run_bandit() {
bandit -c bandit.yaml ${PY_FILES} || FAILURE=true
}
run_pylint() {
pylint -j "${procs}" --rcfile=pylint.rc ${PY_FILES} || FAILURE=true
}
run_pycodestyle() {
pycodestyle --max-line-length=3000 ${PY_FILES} || FAILURE=true
}
run_pydoctor() {
# __init__.py files are required by pydoctor.
files=$(add_init_py)
pydoctor --docformat restructuredtext --html-output pydoctor-html --warnings-as-errors . || FAILURE=true
echo # pydoctor does not add a trailing new line. Add it ourselves.
rm ${files}
rm -rf pydoctor-html
}
run_pydocstyle() {
pydocstyle ${PY_FILES} || FAILURE=true
}
version() {
$1 --version | head -n 1 | grep -E -o '[0-9]+\.[0-9]+\.[0-9]+'
}
# Call the linters.
printf 'Checking %s files...\n' "$(printf '%s\n' "${PY_FILES}" | wc -w)"
for linter in ${linters}; do
printf '======== %s (%s)\n' "${linter}" "`version ${linter}`"
"run_${linter}"
done
# This trick is so that all linters are run, while the script still exits
# with non-zero code if one of them fails.
if test -n "${FAILURE+x}"; then
exit 1
fi