From 3c83d52e52a79ff311dff54ceb2a3118990923ae Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 11 Feb 2021 10:34:53 +1100 Subject: [PATCH] Tools: add script to check Python files for flake8-cleanliness --- .../install-prereqs-ubuntu.sh | 2 +- Tools/scripts/build_ci.sh | 6 +++ Tools/scripts/run_flake8.py | 53 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 Tools/scripts/run_flake8.py diff --git a/Tools/environment_install/install-prereqs-ubuntu.sh b/Tools/environment_install/install-prereqs-ubuntu.sh index 0ebc5bda9f..c1be12df64 100755 --- a/Tools/environment_install/install-prereqs-ubuntu.sh +++ b/Tools/environment_install/install-prereqs-ubuntu.sh @@ -91,7 +91,7 @@ fi # Lists of packages to install BASE_PKGS="build-essential ccache g++ gawk git make wget" -PYTHON_PKGS="future lxml pymavlink MAVProxy pexpect" +PYTHON_PKGS="future lxml pymavlink MAVProxy pexpect flake8" # add some Python packages required for commonly-used MAVProxy modules and hex file generation: if [[ $SKIP_AP_EXT_ENV -ne 1 ]]; then PYTHON_PKGS="$PYTHON_PKGS pygame intelhex" diff --git a/Tools/scripts/build_ci.sh b/Tools/scripts/build_ci.sh index b63a8cd192..71f80d028a 100755 --- a/Tools/scripts/build_ci.sh +++ b/Tools/scripts/build_ci.sh @@ -259,6 +259,12 @@ for t in $CI_BUILD_TARGET; do continue fi + if [ "$t" == "python-cleanliness" ]; then + echo "Checking Python code cleanliness" + ./Tools/scripts/run_flake8.py + continue + fi + if [ "$t" == "configure-all" ]; then echo "Checking configure of all boards" ./Tools/scripts/configure_all.py diff --git a/Tools/scripts/run_flake8.py b/Tools/scripts/run_flake8.py new file mode 100755 index 0000000000..695f86f014 --- /dev/null +++ b/Tools/scripts/run_flake8.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +""" +Runs flake8 over Python files which contain a marker indicating +they are clean, ensures that they actually are + + AP_FLAKE8_CLEAN +""" + +import os +import subprocess +import sys + +import argparse + +os.environ['PYTHONUNBUFFERED'] = '1' + + +class Flake8Checker(object): + def __init__(self): + self.retcode = 0 + + def progress(self, string): + print("****** %s" % (string,)) + + def check(self, filepath): + self.progress("Checking (%s)" % filepath) + retcode = subprocess.call(["flake8", filepath]) + if retcode != 0: + self.progress("File (%s) failed with retcode (%s)" % + (filepath, retcode)) + self.retcode = 1 + + def run(self): + for (dirpath, dirnames, filenames) in os.walk("Tools"): + for filename in filenames: + if os.path.splitext(filename)[1] != ".py": + continue + filepath = os.path.join(dirpath, filename) + content = open(filepath).read() + if "AP_FLAKE8_CLEAN" not in content: + continue + self.check(filepath) + return self.retcode + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Check all Python files for flake8 cleanliness') + # parser.add_argument('--build', action='store_true', default=False, help='build as well as configure') + args = parser.parse_args() + + checker = Flake8Checker() + sys.exit(checker.run())