-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlint.sh
More file actions
executable file
·149 lines (129 loc) · 4.23 KB
/
lint.sh
File metadata and controls
executable file
·149 lines (129 loc) · 4.23 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
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
#!/usr/bin/env bash
set -o errexit
source "$(dirname "$0")/scripts/common.sh"
cd $PWNDBG_ABS_PATH
help_and_exit() {
echo "Usage: ./lint.sh [--check | -fo|--fix-only | -f|--fix-and-check]"
echo " --check run all checks without applying fixes (default behavior)"
echo " -fo, --fix-only fix formatting only, without running checks"
echo " -f, --fix-and-check fix formatting first, then run checks"
echo ""
echo "By default, all checks are run. Fixes are not applied unless specified."
exit 1
}
if [[ $# -gt 1 ]]; then
help_and_exit
fi
CHECK_ONLY=1
FIX_ONLY=0
FIX_AND_CHECK=0
while [[ $# -gt 0 ]]; do
case $1 in
--check)
CHECK_ONLY=1
FIX_ONLY=0
FIX_AND_CHECK=0
shift
;;
-fo | --fix-only)
CHECK_ONLY=0
FIX_ONLY=1
FIX_AND_CHECK=0
shift
;;
-f | --fix-and-check)
CHECK_ONLY=0
FIX_ONLY=0
FIX_AND_CHECK=1
shift
;;
*)
help_and_exit
;;
esac
done
print_info() {
set +o xtrace
local MSG=$1
echo ""
echo "[info] ${MSG}"
echo ""
set -o xtrace
}
set -o xtrace
LINT_FILES="pwndbg pwndbginit tests *.py scripts"
call_shfmt() {
local FLAGS=$1
if [ -x "$(command -v shfmt)" ]; then
local SHFMT_FILES=$(find . -name "*.sh" -not -path "./.venv/*")
# Indents are four spaces, binary ops can start a line, indent switch cases,
# and allow spaces following a redirect
print_info "Running shfmt on .sh files..."
$UV_RUN_LINT shfmt ${FLAGS} -i 4 -bn -ci -sr -d ${SHFMT_FILES}
else
echo "shfmt not installed, please install it"
exit 2
fi
}
print_info "Running ruff on python files..."
if [[ $FIX_ONLY == 1 ]]; then
$UV_RUN_LINT ruff format ${LINT_FILES}
$UV_RUN_LINT ruff check --fix --output-format=full ${LINT_FILES}
call_shfmt -w
set +o xtrace
echo ""
echo "========================================="
echo "NOTE: Only ruff, shfmt were run."
echo " mypy and vermin were NOT run."
echo " Use -f or no flags to run all checks."
echo "========================================="
exit 0
elif [[ $FIX_AND_CHECK == 1 ]]; then
$UV_RUN_LINT ruff format ${LINT_FILES}
$UV_RUN_LINT ruff check --fix --output-format=full ${LINT_FILES}
call_shfmt -w
else
if ! $UV_RUN_LINT ruff format --check --diff ${LINT_FILES}; then
set +o xtrace
echo ""
echo "========================================="
echo "ERROR: Formatting issues detected by ruff."
echo " Exiting early. All checks were NOT run."
echo " Use -f to fix issues automatically."
echo "========================================="
exit 1
fi
if ! call_shfmt; then
set +o xtrace
echo ""
echo "========================================="
echo "ERROR: Formatting issues detected by shfmt."
echo " Exiting early. All checks were NOT run."
echo " Use -f to fix issues automatically."
echo "========================================="
exit 1
fi
if [[ -z "$GITHUB_ACTIONS" ]]; then
RUFF_OUTPUT_FORMAT=full
else
RUFF_OUTPUT_FORMAT=github
fi
$UV_RUN_LINT ruff check --output-format="${RUFF_OUTPUT_FORMAT}" ${LINT_FILES}
fi
# Checking minimum python version
print_info "Using vermin to check that the code is compatible with the lowest supported python version..."
# We have to use `--backport typing_extensions` because we use `override`, and the modern way to do it is
# `from typing import override`, but that only became available in 3.12 .
$UV_RUN_LINT vermin -vvv --no-tips -t=3.10- --eval-annotations --backport typing_extensions --violations ${LINT_FILES}
# Check our custom rules.
print_info "Checking custom Pwndbg lint rules..."
$UV_RUN_LINT scripts/custom-lint.py
# mypy is run in a separate step on GitHub Actions
if [[ -z "$GITHUB_ACTIONS" ]]; then
print_info "Running mypy to check for type errors in python files..."
$UV_RUN_MYPY mypy $LINT_FILES
fi
set +o xtrace
echo ""
echo "[success] Lint passed!"
set -o xtrace