Skip to content

Commit 8c4343c

Browse files
zhongzhijie1xiaoxiang781216
authored andcommitted
tools/checkpatch.sh: add -x option to auto-format Python files.
Currently only .py files are supported. Non-Python files will report "format not implemented". Signed-off-by: zhongzhijie1 <[email protected]>
1 parent ec84502 commit 8c4343c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tools/checkpatch.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ usage() {
5656
echo "-m Change-Id check in commit message (coupled with -g)"
5757
echo "-g <commit list>"
5858
echo "-f <file list>"
59+
echo "-x format supported files (only .py, requires: pip install black)"
5960
echo "- read standard input mainly used by git pre-commit hook as below:"
6061
echo " git diff --cached | ./tools/checkpatch.sh -"
6162
echo "Where a <commit list> is any syntax supported by git for specifying git revision, see GITREVISIONS(7)"
@@ -93,6 +94,24 @@ is_cmake_file() {
9394
fi
9495
}
9596

97+
format_file() {
98+
if [ ${@##*.} == 'py' ]; then
99+
if command -v black >/dev/null; then
100+
echo "Auto-formatting Python file with black: $@"
101+
setupcfg="${TOOLDIR}/../.github/linters/setup.cfg"
102+
isort --settings-path "${setupcfg}" "$@"
103+
black $@
104+
else
105+
echo "$@: error: black not found. Please install with: pip install black"
106+
fail=1
107+
fi
108+
else
109+
# TODO: extend for other file types in the future
110+
echo "$@: error: format files type not implemented"
111+
fail=1
112+
fi
113+
}
114+
96115
check_file() {
97116
if [ -x $@ ]; then
98117
case $@ in
@@ -295,6 +314,9 @@ while [ ! -z "$1" ]; do
295314
-u )
296315
encoding=1
297316
;;
317+
-x )
318+
check=format_file
319+
;;
298320
-f )
299321
check=check_file
300322
;;

0 commit comments

Comments
 (0)