aboutsummaryrefslogtreecommitdiffstats
path: root/python/tidy.py
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2015-06-03 20:29:13 -0400
committerCorey Farwell <coreyf@rwell.org>2015-06-04 11:17:34 -0400
commit848c57653ce1b023a324fb9442059228f5372188 (patch)
treeb7ca6475822fcc2220bfb3d5f76b8d6f5775b957 /python/tidy.py
parent907c051bd1d59621449a399ccf6845b617bdff9d (diff)
downloadservo-848c57653ce1b023a324fb9442059228f5372188.tar.gz
servo-848c57653ce1b023a324fb9442059228f5372188.zip
Add flake8 to the tidy process for Python files
Fixes #6236 Also included in this commit are the changes need to make flake8 pass for the existing python file
Diffstat (limited to 'python/tidy.py')
-rw-r--r--python/tidy.py38
1 files changed, 34 insertions, 4 deletions
diff --git a/python/tidy.py b/python/tidy.py
index 2fe531da4b3..6f4dc9778e8 100644
--- a/python/tidy.py
+++ b/python/tidy.py
@@ -7,17 +7,21 @@
# option. This file may not be copied, modified, or distributed
# except according to those terms.
-#!/usr/bin/env python
-
import os
import fnmatch
import itertools
import re
+import sys
from licenseck import licenses
filetypes_to_check = [".rs", ".rc", ".cpp", ".c", ".h", ".py"]
reftest_directories = ["tests/ref"]
reftest_filetype = ".list"
+python_dependencies = [
+ "./python/dependencies/flake8-2.4.1-py2.py3-none-any.whl",
+ "./python/dependencies/pep8-1.5.7-py2.py3-none-any.whl",
+ "./python/dependencies/pyflakes-0.9.0-py2.py3-none-any.whl",
+]
ignored_files = [
# Upstream
@@ -80,12 +84,14 @@ def check_length(idx, line):
if len(line) >= 120:
yield (idx + 1, "(much) overlong line")
+
def check_whatwg_url(idx, line):
match = re.search(r"https://html\.spec\.whatwg\.org/multipage/[\w-]+\.html#([\w\:-]+)", line)
if match is not None:
preferred_link = "https://html.spec.whatwg.org/multipage/#{}".format(match.group(1))
yield (idx + 1, "link to WHATWG may break in the future, use this format instead: {}".format(preferred_link))
+
def check_whitespace(idx, line):
if line[-1] == "\n":
line = line[:-1]
@@ -101,6 +107,7 @@ def check_whitespace(idx, line):
if "\r" in line:
yield (idx + 1, "CR on line")
+
def check_by_line(contents):
lines = contents.splitlines(True)
for idx, line in enumerate(lines):
@@ -113,6 +120,25 @@ def check_by_line(contents):
yield error
+def check_flake8(file_paths):
+ from flake8.main import check_file
+
+ ignore = {
+ "W291", # trailing whitespace; the standard tidy process will enforce no trailing whitespace
+ "E501", # 80 character line length; the standard tidy process will enforce line length
+ }
+
+ num_errors = 0
+
+ for file_path in file_paths:
+ if os.path.splitext(file_path)[-1].lower() != ".py":
+ continue
+
+ num_errors += check_file(file_path, ignore=ignore)
+
+ return num_errors
+
+
def collect_errors_for_files(files_to_check, checking_functions):
for file_name in files_to_check:
with open(file_name, "r") as fp:
@@ -129,7 +155,7 @@ def check_reftest_order(files_to_check):
split_lines = fp.read().splitlines()
lines = filter(lambda l: len(l) > 0 and l[0] != '#', split_lines)
for idx, line in enumerate(lines[:-1]):
- next_line = lines[idx+1]
+ next_line = lines[idx + 1]
current = get_reftest_names(line)
next = get_reftest_names(next_line)
if current is not None and next is not None and current > next:
@@ -146,9 +172,13 @@ def get_reftest_names(line):
def scan():
+ sys.path += python_dependencies
+
all_files = collect_file_names()
files_to_check = filter(should_check, all_files)
+ num_flake8_errors = check_flake8(files_to_check)
+
checking_functions = [check_license, check_by_line]
errors = collect_errors_for_files(files_to_check, checking_functions)
@@ -158,7 +188,7 @@ def scan():
errors = list(itertools.chain(errors, r_errors))
- if errors:
+ if errors or num_flake8_errors:
for error in errors:
print("{}:{}: {}".format(*error))
return 1