Browse Source

Tools: add script to check Python files for flake8-cleanliness

zr-v5.1
Peter Barker 4 years ago committed by Peter Barker
parent
commit
3c83d52e52
  1. 2
      Tools/environment_install/install-prereqs-ubuntu.sh
  2. 6
      Tools/scripts/build_ci.sh
  3. 53
      Tools/scripts/run_flake8.py

2
Tools/environment_install/install-prereqs-ubuntu.sh

@ -91,7 +91,7 @@ fi @@ -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"

6
Tools/scripts/build_ci.sh

@ -259,6 +259,12 @@ for t in $CI_BUILD_TARGET; do @@ -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

53
Tools/scripts/run_flake8.py

@ -0,0 +1,53 @@ @@ -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())
Loading…
Cancel
Save