summaryrefslogtreecommitdiff
path: root/tools/cpep8.py
diff options
context:
space:
mode:
authorDave Hibberd <d@vehibberd.com>2023-10-16 21:37:27 +0100
committerDave Hibberd <d@vehibberd.com>2023-10-16 21:37:27 +0100
commit91dbf844ad923e3c4bca1252dd104d8fc8ce5475 (patch)
tree93c4db659daf278db034b304e4cd910454504ec1 /tools/cpep8.py
parent6f19923a918f7fb63a444b0c99950fdbe9d2f74c (diff)
New upstream version 20231016upstream/20231016
Diffstat (limited to 'tools/cpep8.py')
-rwxr-xr-xtools/cpep8.py82
1 files changed, 45 insertions, 37 deletions
diff --git a/tools/cpep8.py b/tools/cpep8.py
index b073f48..678281f 100755
--- a/tools/cpep8.py
+++ b/tools/cpep8.py
@@ -17,16 +17,24 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import warnings
+
import os
import sys
-import logging
import argparse
-import pep8
+import subprocess
+
+# pep8 has a FutureWarning about nested sets. This isn't our problem, so
+# squelch it here during import.
+with warnings.catch_warnings():
+ warnings.simplefilter('ignore')
+ import pep8
+
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--all", action="store_true",
help="Check all files, ignoring blacklist")
-parser.add_argument("-d", "--dir", action="store", default=".",
+parser.add_argument("-d", "--dir", action="append", default=['chirp', 'tests'],
help="Root directory of source tree")
parser.add_argument("-s", "--stats", action="store_true",
help="Only show statistics")
@@ -48,7 +56,7 @@ def file_to_lines(name):
lines = fh.read().split("\n")
lines.pop()
fh.close()
- return lines
+ return [x for x in lines if not x.startswith('#')]
scriptdir = os.path.dirname(sys.argv[0])
@@ -56,20 +64,21 @@ manifest_filename = os.path.join(scriptdir, "cpep8.manifest")
blacklist_filename = os.path.join(scriptdir, "cpep8.blacklist")
exceptions_filename = os.path.join(scriptdir, "cpep8.exceptions")
-manifest = []
-if args.scan:
- for root, dirs, files in os.walk(args.dir):
+cpep8_manifest = set(file_to_lines(manifest_filename))
+flake8_manifest = set()
+for src_dir in [os.path.join('.', d) for d in args.dir]:
+ for root, dirs, files in os.walk(src_dir):
for f in files:
filename = os.path.join(root, f)
+ if filename.replace('\\', '/') in cpep8_manifest:
+ continue
if f.endswith('.py'):
- manifest.append(filename)
+ flake8_manifest.add(filename)
continue
- with open(filename, "r") as fh:
+ with open(filename, "rb") as fh:
shebang = fh.readline()
- if shebang.startswith("#!/usr/bin/env python"):
- manifest.append(filename)
-else:
- manifest += file_to_lines(manifest_filename)
+ if shebang.startswith(b"#!/usr/bin/env python"):
+ flake8_manifest.add(filename)
# unless we are being --strict, load per-file style exceptions
@@ -89,37 +98,25 @@ def get_exceptions(f):
ignore = None
return ignore
-if args.update:
- print("Starting update of %d files" % len(manifest))
- bad = []
- for f in manifest:
- checker = pep8.StyleGuide(quiet=True, ignore=get_exceptions(f))
- results = checker.check_files([f])
- if results.total_errors:
- bad.append(f)
- print("%s: %s" % (results.total_errors and "FAIL" or "PASS", f))
-
- with open(blacklist_filename, "w") as fh:
- fh.write("""\
-# cpep8.blacklist: The list of files that do not meet PEP8 standards.
-# DO NOT ADD NEW FILES!! Instead, fix the code to be compliant.
-# Over time, this list should shrink and (eventually) be eliminated.""")
- fh.write("\n".join(sorted(bad)))
-
- if args.scan:
- with open(manifest_filename, "w") as fh:
- fh.write("\n".join(sorted(manifest)))
- sys.exit(0)
if args.files:
- manifest = args.files
+ old_files = cpep8_manifest
+ cpep8_manifest = []
+ flake8_manifest = []
+ for fn in args.files:
+ if not fn.startswith('./'):
+ fn = './' + fn
+ if fn in old_files:
+ cpep8_manifest.append(fn)
+ else:
+ flake8_manifest.append(fn)
# read the blacklisted source files
blacklist = file_to_lines(blacklist_filename)
check_list = []
-for f in manifest:
- if args.all or f not in blacklist:
+for f in cpep8_manifest:
+ if args.all or f.replace('\\', '/') not in blacklist:
check_list.append(f)
check_list = sorted(check_list)
@@ -134,4 +131,15 @@ for f in check_list:
checker.report.print_statistics()
total_errors += results
+flake8_manifest = [f for f in flake8_manifest
+ if f.replace('\\', '/') not in blacklist]
+
+for i in range(0, len(flake8_manifest), 10):
+ files = flake8_manifest[i:i + 10]
+ if args.verbose:
+ print('Checking %s' % files)
+ r = subprocess.call(['flake8', '--builtins=_'] + files)
+ if r != 0:
+ total_errors += r
+
sys.exit(total_errors and 1 or 0)