From 43265614fe010c677c447c1269500478b49e72dd Mon Sep 17 00:00:00 2001 From: Advait Jain Date: Thu, 22 Jul 2021 14:42:08 -0700 Subject: [PATCH] Turn on Python formatting checks as part of CI. (#316) * Groundwork to enable python format checks (via yapf) as part of the CI. With this change: * we are set up to use yapf as part of the docker container * pigweed patch is updated to use the google Python style (pep8 with indent of 2 spaces) * some documentation updates. The code still needs to be formatted, and then the Python formatting check will be turned on as part of ci. BUG=http://b/194404216 * Format all the code. Once this change is merged, we will turn on Python formatting check as part of the CI. * minor formatting update. * Turn on Python formatting checks as part of CI. Also, add an option to fix the formatting via the test_code_style.sh script. BUG=http://b/194404984 and http://b/194404216 --- CONTRIBUTING.md | 18 +++++------ .../micro/tools/ci_build/test_code_style.sh | 32 +++++++++++++------ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 291eabe3..a4afd6a4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -221,6 +221,14 @@ Below are some tips that might be useful and improve the development experience. * Get a copy of [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) +* [yapf](https://github.com/google/yapf/) should be used for formatting Python + code. For example: + + ``` + pip3 install yapf + yapf log_parser.py -i --style='{based_on_style: pep8, indent_width: 2}' + ``` + * Add a git hook to check for code style etc. prior to creating a pull request: ``` cp tensorflow/lite/micro/tools/dev_setup/pre-push.tflm .git/hooks/pre-push @@ -338,18 +346,8 @@ Below are some tips that might be useful and improve the development experience. ## Python notes -Most PRs for TensorFlow Lite Micro will be C++ only. Adding some notes on Python -that can be expanded and improved as necessary. - * [TensorFlow guide](https://www.tensorflow.org/community/contribute/code_style#python_style) for Python development -* [yapf](https://github.com/google/yapf/) should be used for formatting. For example: - - ``` - pip3 install yapf - yapf log_parser.py -i --style='{based_on_style: pep8, indent_width: 2}' - ``` - # Continuous Integration System * Some [additional documentation](docs/continuous_integration.md) on the TFLM CI. diff --git a/tensorflow/lite/micro/tools/ci_build/test_code_style.sh b/tensorflow/lite/micro/tools/ci_build/test_code_style.sh index 7e916171..272afea9 100755 --- a/tensorflow/lite/micro/tools/ci_build/test_code_style.sh +++ b/tensorflow/lite/micro/tools/ci_build/test_code_style.sh @@ -62,20 +62,28 @@ LICENSE_CHECK_RESULT=$? ############################################################ # Formatting Check ############################################################ -# We are currently ignoring Python files (with yapf as the formatter) because -# that needs additional setup. We are also ignoring the markdown files to allow -# for a more gradual rollout of this presubmit check. + +if [[ ${1} == "--fix_formatting" ]] +then + FIX_FORMAT_OPTIONS="--fix" +else + FIX_FORMAT_OPTIONS="" +fi + micro/tools/make/downloads/pigweed/pw_presubmit/py/pw_presubmit/format_code.py \ - kernels/internal/reference/ \ - micro/ \ - ../../third_party/ \ + ${FIX_FORMAT_OPTIONS} \ + -e c/common.c \ + -e core/api/error_reporter.cc \ -e kernels/internal/reference/integer_ops/ \ -e kernels/internal/reference/reference_ops.h \ + -e kernels/internal/types.h \ + -e experimental \ + -e schema/schema_generated.h \ + -e schema/schema_utils.h \ -e "\.inc" \ - -e "\.md" \ - -e "\.py" + -e "\.md" -CLANG_FORMAT_RESULT=$? +FORMAT_RESULT=$? ############################################################################# # Avoided specific-code snippets for TFLM @@ -121,8 +129,12 @@ popd # Re-enable exit on error now that we are done with the temporary git repo. set -e +if [[ ${FORMAT_RESULT} != 0 ]] +then + echo "The formatting errors can be fixed with tensorflow/lite/micro/tols/ci_build/test_code_style.sh --fix_formatting" +fi if [[ ${LICENSE_CHECK_RESULT} != 0 || \ - ${CLANG_FORMAT_RESULT} != 0 || \ + ${FORMAT_RESULT} != 0 || \ ${GTEST_RESULT} != 0 || \ ${ERROR_REPORTER_RESULT} != 0 || \ ${ASSERT_RESULT} != 0 \ -- GitLab