Skip to content

Commit 9e905f9

Browse files
authored
[CI] Enhance python linting scripts to support revision-based checks (apache#18470)
## How support revision-based checks like `python_format` for `flake8` and `pylint`
1 parent 91c1921 commit 9e905f9

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

docker/lint.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,18 @@ function run_lint_step() {
5555
cmd=( tests/lint/cpplint.sh )
5656
;;
5757
flake8)
58-
cmd=( tests/lint/flake8.sh )
58+
if [ $inplace_fix -eq 0 ]; then
59+
cmd=( tests/lint/flake8.sh )
60+
else
61+
cmd=( tests/lint/flake8.sh --rev origin/main )
62+
fi
5963
;;
6064
pylint)
61-
cmd=( tests/lint/pylint.sh )
65+
if [ $inplace_fix -eq 0 ]; then
66+
cmd=( tests/lint/pylint.sh )
67+
else
68+
cmd=( tests/lint/pylint.sh --rev origin/main )
69+
fi
6270
;;
6371
python_format)
6472
if [ $inplace_fix -eq 0 ]; then

tests/lint/flake8.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@
1616
# specific language governing permissions and limitations
1717
# under the License.
1818

19-
set -e
19+
set -euo pipefail
2020

21-
python3 -m flake8 . --count --select=E9,F63,F7 --show-source --statistics --exclude 3rdparty
21+
LINT_ALL_FILES=true
22+
REVISION=
23+
24+
while (( $# )); do
25+
case "$1" in
26+
--rev)
27+
LINT_ALL_FILES=false
28+
REVISION=$2
29+
shift 2
30+
;;
31+
*)
32+
echo "Usage: tests/lint/flake8.sh [--rev <commit>]"
33+
echo ""
34+
echo "Run flake8 on Python files that changed since <commit> or on all files in the repo"
35+
echo "Examples:"
36+
echo "- Compare last one commit: tests/lint/flake8.sh --rev HEAD~1"
37+
echo "- Compare against upstream/main: tests/lint/flake8.sh --rev upstream/main"
38+
exit 1
39+
;;
40+
esac
41+
done
42+
43+
if [[ "$LINT_ALL_FILES" == "true" ]]; then
44+
echo "Running flake8 on all files"
45+
python3 -m flake8 . --count --select=E9,F63,F7 --show-source --statistics --exclude 3rdparty
46+
else
47+
# Get changed Python files, excluding 3rdparty
48+
IFS=$'\n' read -a FILES -d'\n' < <(git diff --name-only --diff-filter=ACMRTUX $REVISION -- "*.py" "*.pyi" | grep -v "^3rdparty/") || true
49+
if [ -z ${FILES+x} ] || [ ${#FILES[@]} -eq 0 ]; then
50+
echo "No changes in Python files"
51+
exit 0
52+
fi
53+
echo "Running flake8 on changed files: ${FILES[@]}"
54+
python3 -m flake8 ${FILES[@]} --count --select=E9,F63,F7 --show-source --statistics
55+
fi

tests/lint/pylint.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@
1515
# KIND, either express or implied. See the License for the
1616
# specific language governing permissions and limitations
1717
# under the License.
18-
set -euxo pipefail
18+
set -euo pipefail
1919

20-
python3 -m pylint python/tvm --rcfile="$(dirname "$0")"/pylintrc
20+
LINT_ALL_FILES=true
21+
REVISION=
22+
23+
while (( $# )); do
24+
case "$1" in
25+
--rev)
26+
LINT_ALL_FILES=false
27+
REVISION=$2
28+
shift 2
29+
;;
30+
*)
31+
echo "Usage: tests/lint/pylint.sh [--rev <commit>]"
32+
echo ""
33+
echo "Run pylint on Python files that changed since <commit> or on all files in python/tvm"
34+
echo "Examples:"
35+
echo "- Compare last one commit: tests/lint/pylint.sh --rev HEAD~1"
36+
echo "- Compare against upstream/main: tests/lint/pylint.sh --rev upstream/main"
37+
exit 1
38+
;;
39+
esac
40+
done
41+
42+
if [[ "$LINT_ALL_FILES" == "true" ]]; then
43+
echo "Running pylint on all files in python/tvm"
44+
python3 -m pylint python/tvm --rcfile="$(dirname "$0")"/pylintrc
45+
else
46+
# Get changed Python files in python/tvm directory
47+
IFS=$'\n' read -a FILES -d'\n' < <(git diff --name-only --diff-filter=ACMRTUX $REVISION -- "python/tvm/*.py" "python/tvm/**/*.py") || true
48+
if [ -z ${FILES+x} ] || [ ${#FILES[@]} -eq 0 ]; then
49+
echo "No changes in Python files under python/tvm"
50+
exit 0
51+
fi
52+
echo "Running pylint on changed files: ${FILES[@]}"
53+
python3 -m pylint ${FILES[@]} --rcfile="$(dirname "$0")"/pylintrc
54+
fi

0 commit comments

Comments
 (0)