This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
/
lint.sh
executable file
·151 lines (129 loc) · 4.03 KB
/
lint.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
#!/bin/bash
set -e
BRANCH="origin/develop"
FULL=false
VERBOSE=false
COLUMNS=$(( $(tput cols || echo 80) - 10 ))
#credit: https://stackoverflow.com/a/28938235/3805131
RESET='\033[0m' # Text Reset
RED='\033[1;31m' # Bold Red
GREEN='\033[1;32m' # Bold Green
usage() {
printf "Usage: $0 [-b <branch>] [-v] [-h]\n"
printf "\nPerforms automatic lint checks for python code; lazy way - only changes\n"
printf "\nOPTIONS:\n"
printf " -b <branch> branch to use for comparison. default is ${BRANCH}\n"
printf " -f do a full check, not only changed files\n"
printf " -v be verbose\n"
printf " -h show this help message\n"
exit 1
}
while getopts "b:fvh" opt; do
case $opt in
b)
BRANCH="${OPTARG}"
;;
f)
FULL=true
;;
v)
VERBOSE=true
;;
*)
usage
;;
esac
done
# to be back compatible
shift $((OPTIND - 1))
[[ -n "$1" ]] && BRANCH="$1"
hline() {
printf "%${COLUMNS}s\n" | tr " " "-"
}
message() {
printf "\n⚡️ $1\n\n" >&2
}
status() {
if [[ "$1" -eq 0 ]]; then
printf " [${GREEN}OK${RESET}]\n"
else
printf " [${RED}FAIL${RESET}]\n"
fi
}
files_to_check() {
if [[ "${FULL}" = true ]]; then
message "Performing full check"
find . -name '*.py' | cut -f2 -d'/' | uniq
else
local cur_hash=$(git rev-parse --short HEAD)
local ref_hash=$(git rev-parse --short "${BRANCH}")
message "Comparing HEAD(${cur_hash})..${BRANCH}(${ref_hash})"
# credit: https://gist.github.com/kentcdodds/9768d9a8d0bfbf6797cd
git diff --name-only --diff-filter=d "${BRANCH}..HEAD" -- '*.py'
fi
}
main() {
# credit: https://unix.stackexchange.com/questions/155046/determine-if-git-working-directory-is-clean-from-a-script/183976
changed_files="$(git status --untracked-files=no --porcelain)"
if [[ -n "${changed_files}" ]]; then
message "You must commit or stash changes:\n\n ${changed_files}"
exit -1
fi
message "Checking if the requirements are sorted properly"
ls | grep '^requirements.*txt$' | LC_ALL=C xargs -I@ sort --ignore-case -c @
files2chk=$(files_to_check)
if [[ -z "${files2chk}" ]]; then
message "No python files to check."
exit 0
elif [[ "${VERBOSE}" = true ]]; then
printf "Python files/modules to check:\n" >&2
printf "${files2chk}\n\n" | sed "s/^/ /"
fi
local prod_files2chk=$(printf "${files2chk}" | grep -v '^tests\>' | grep -v '^scripts\>') || true
local test_files2chk=$(printf "${files2chk}" | grep '^tests\>') || true
local lintdiff_cmd="./lintdiff.sh -o -b ${BRANCH}"
local commands=(
"$lintdiff_cmd pylint ${prod_files2chk}"
"$lintdiff_cmd pylint --disable=protected-access,no-self-use ${test_files2chk}"
"$lintdiff_cmd flake8 ${files2chk}"
"$lintdiff_cmd mypy ${files2chk}"
)
local names=(
"pylint main"
"pylint tests"
"flake8"
"mypy"
)
for i in "${!names[@]}"; do
if [[ "${VERBOSE}" = true ]]; then
printf "\n${names[$i]}:\n"
printf "%-${COLUMNS}s" " ${commands[$i]} ..." | tr '\r\n' ' '
else
printf "%-20s" "${names[$i]}..."
fi
exitcode[$i]=0
outputs[$i]=$(${commands[$i]} 2>&1) || exitcode[$i]=$?
status ${exitcode[$i]}
done
local nfailed=0
for i in "${!names[@]}"; do
if [[ "${exitcode[$i]}" -ne 0 ]]; then
let "nfailed++" || true
hline
printf "${names[$i]} failed, output:\n\n"
echo "${outputs[$i]}\n"
hline
fi
done
if [[ "${nfailed}" -gt 0 ]]; then
printf "Errors occurred, summary:\n"
for i in "${!names[@]}"; do
printf "%-20s" "${names[$i]}..."
status ${exitcode[$i]}
done
exit 1
else
message "All checked files passed linting!"
fi
}
main "$@"