Skip to content

Commit

Permalink
add python to script/make-pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
lmnotran committed Feb 7, 2024
1 parent 10d7e90 commit 67d3fc6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake~=3.26.3
Jinja2~=3.1.2
GitPython~=3.1.31
PyYAML~=6.0
PyYAML~=6.0
yapf~=0.40.2
44 changes: 41 additions & 3 deletions script/make-pretty
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#
# The script to check or format source code of OpenThread.
#
# Format c/c++, markdown, and shell:
# Format c/c++, markdown, python, and shell:
#
# script/make-pretty
#
Expand All @@ -42,6 +42,10 @@
#
# script/make-pretty markdown
#
# Format python only:
#
# script/make-pretty python
#
# Format shell only:
#
# script/make-pretty shell
Expand All @@ -50,6 +54,7 @@
#
# script/make-pretty check clang-format
# script/make-pretty check markdown
# script/make-pretty check python
# script/make-pretty check shell
#

Expand Down Expand Up @@ -78,6 +83,9 @@ readonly OT_CLANG_SOURCES
OT_MARKDOWN_SOURCES=('*.md')
readonly OT_MARKDOWN_SOURCES

OT_PYTHON_SOURCES=('*.py')
readonly OT_PYTHON_SOURCES

OT_SCRIPT_DIR="${repo_dir}"/openthread/script
readonly OT_SCRIPT_DIR

Expand Down Expand Up @@ -129,6 +137,30 @@ do_markdown_check()
popd
}

do_python_format()
{
echo -e '========================================'
echo -e ' format python'
echo -e '========================================'

pushd "${repo_dir}"
git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -ipr
popd
}

do_python_check()
{
echo -e '========================================'
echo -e ' check python'
echo -e '========================================'

pushd "${repo_dir}"
git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
| xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style '{based_on_style: google, column_limit: 119}' -dpr
popd
}

do_shell_format()
{
echo -e '========================================'
Expand Down Expand Up @@ -160,17 +192,20 @@ do_check()
if [ $# == 0 ]; then
do_clang_format_check
do_markdown_check
do_python_check
do_shell_check
elif [ "$1" == 'clang' ]; then
do_clang_format_check
elif [ "$1" == 'clang-format' ]; then
do_clang_format_check
elif [ "$1" == 'markdown' ]; then
do_markdown_check
elif [ "$1" == 'python' ]; then
do_python_check
elif [ "$1" == 'shell' ]; then
do_shell_check
else
echo >&2 "Unsupported check: $1. Supported: clang-format, markdown, shell"
echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell"
# 128 for Invalid arguments
exit 128
fi
Expand All @@ -181,20 +216,23 @@ main()
if [ $# == 0 ]; then
do_clang_format
do_markdown_format
do_python_format
do_shell_format
elif [ "$1" == 'clang' ]; then
do_clang_format
elif [ "$1" == 'clang-format' ]; then
do_clang_format
elif [ "$1" == 'markdown' ]; then
do_markdown_format
elif [ "$1" == 'python' ]; then
do_python_format
elif [ "$1" == 'shell' ]; then
do_shell_format
elif [ "$1" == 'check' ]; then
shift
do_check "$@"
else
echo >&2 "Unsupported action: $1. Supported: clang-format, markdown, shell"
echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell"
# 128 for Invalid arguments
exit 128
fi
Expand Down

0 comments on commit 67d3fc6

Please sign in to comment.