From 4d5e39fe081017b67b636e06da6f96c56793eedf Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 14:29:08 +0200 Subject: [PATCH 1/5] Inline the git function into all_git_paths. --- lint/lint.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index 61f900cb980302..9bb7de45f13688 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -26,21 +26,10 @@ %s:%s""" -def git(command, *args): - args = list(args) - - proc_kwargs = {"cwd": repo_root} - - command_line = ["git", command] + args - - try: - return subprocess.check_output(command_line, **proc_kwargs) - except subprocess.CalledProcessError: - raise - - def all_git_paths(): - for item in git("ls-tree", "-r", "--name-only", "HEAD").split("\n"): + command_line = ["git", "ls-tree", "-r", "--name-only", "HEAD"] + output = subprocess.check_output(command_line, cwd=repo_root) + for item in output.split("\n"): yield item From e5acaf40fa227b99495a43bb09b6cc6db92b86ab Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 14:31:34 +0200 Subject: [PATCH 2/5] Pass around repo_root explicitly in lint. --- lint/lint.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index 9bb7de45f13688..af793bf2ebe56e 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -11,7 +11,6 @@ from manifest.sourcefile import SourceFile here = os.path.abspath(os.path.split(__file__)[0]) -repo_root = localpaths.repo_root ERROR_MSG = """You must fix all errors; for details on how to fix them, see https://github.com/w3c/web-platform-tests/blob/master/docs/lint-tool.md @@ -26,14 +25,14 @@ %s:%s""" -def all_git_paths(): +def all_git_paths(repo_root): command_line = ["git", "ls-tree", "-r", "--name-only", "HEAD"] output = subprocess.check_output(command_line, cwd=repo_root) for item in output.split("\n"): yield item -def check_path_length(path): +def check_path_length(repo_root, path): if len(path) + 1 > 150: return [("PATH LENGTH", "/%s longer than maximum path length (%d > 150)" % (path, len(path) + 1), None)] return [] @@ -139,7 +138,7 @@ class PrintRegexp(Regexp): ConsoleRegexp, PrintRegexp]] -def check_regexp_line(path, f): +def check_regexp_line(repo_root, path, f): errors = [] applicable_regexps = [regexp for regexp in regexps if regexp.applies(path)] @@ -151,7 +150,7 @@ def check_regexp_line(path, f): return errors -def check_parsed(path, f): +def check_parsed(repo_root, path, f): source_file = SourceFile(repo_root, path, "/") errors = [] @@ -248,18 +247,19 @@ def parse_args(): return parser.parse_args() def main(): + repo_root = localpaths.repo_root args = parse_args() - paths = args.paths if args.paths else all_git_paths() - return lint(paths) + paths = args.paths if args.paths else all_git_paths(repo_root) + return lint(repo_root, paths) -def lint(paths): +def lint(repo_root, paths): error_count = defaultdict(int) last = None whitelist = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist")) def run_lint(path, fn, last, *args): - errors = filter_whitelist_errors(whitelist, path, fn(path, *args)) + errors = filter_whitelist_errors(whitelist, path, fn(repo_root, path, *args)) if errors: last = (errors[-1][0], path) From cb6a943b03da47d4fabfd816854c0a8160db0d2e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 22:54:56 +0200 Subject: [PATCH 3/5] Only import localpaths in main. --- lint/lint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/lint.py b/lint/lint.py index af793bf2ebe56e..de5fbb25f4723b 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -7,7 +7,6 @@ from collections import defaultdict -from .. import localpaths from manifest.sourcefile import SourceFile here = os.path.abspath(os.path.split(__file__)[0]) @@ -247,6 +246,7 @@ def parse_args(): return parser.parse_args() def main(): + from .. import localpaths repo_root = localpaths.repo_root args = parse_args() paths = args.paths if args.paths else all_git_paths(repo_root) From 798d6fe242e554309af8e6dee2680cf72e652137 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 23:02:38 +0200 Subject: [PATCH 4/5] Improve python 3 compatibility in lint. --- lint/lint.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index de5fbb25f4723b..b89c6c4841d4a6 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -1,3 +1,5 @@ +from __future__ import print_function, unicode_literals + import argparse import fnmatch import os @@ -7,6 +9,9 @@ from collections import defaultdict +from six import iteritems +from six.moves import range + from manifest.sourcefile import SourceFile here = os.path.abspath(os.path.split(__file__)[0]) @@ -68,9 +73,9 @@ def filter_whitelist_errors(data, path, errors): Filter out those errors that are whitelisted in `data`. """ - whitelisted = [False for item in xrange(len(errors))] + whitelisted = [False for item in range(len(errors))] - for file_match, whitelist_errors in data.iteritems(): + for file_match, whitelist_errors in iteritems(data): if fnmatch.fnmatch(path, file_match): for i, (error_type, msg, line) in enumerate(errors): if "*" in whitelist_errors: @@ -99,32 +104,32 @@ def search(self, line): return self._re.search(line) class TrailingWhitespaceRegexp(Regexp): - pattern = "[ \t\f\v]$" + pattern = b"[ \t\f\v]$" error = "TRAILING WHITESPACE" class TabsRegexp(Regexp): - pattern = "^\t" + pattern = b"^\t" error = "INDENT TABS" class CRRegexp(Regexp): - pattern = "\r$" + pattern = b"\r$" error = "CR AT EOL" class W3CTestOrgRegexp(Regexp): - pattern = "w3c\-test\.org" + pattern = b"w3c\-test\.org" error = "W3C-TEST.ORG" class Webidl2Regexp(Regexp): - pattern = "webidl2\.js" + pattern = b"webidl2\.js" error = "WEBIDL2.JS" class ConsoleRegexp(Regexp): - pattern = "console\.[a-zA-Z]+\s*\(" + pattern = b"console\.[a-zA-Z]+\s*\(" error = "CONSOLE" file_extensions = [".html", ".htm", ".js", ".xht", ".html", ".svg"] class PrintRegexp(Regexp): - pattern = "print(?:\s|\s*\()" + pattern = b"print(?:\s|\s*\()" error = "PRINT STATEMENT" file_extensions = [".py"] @@ -200,7 +205,7 @@ def check_parsed(repo_root, path, f): "testharnessreport": False} required_elements = [key for key, value in {"testharness": True, "testharnessreport": len(testharnessreport_nodes) > 0, - "timeout": len(source_file.timeout_nodes) > 0}.iteritems() + "timeout": len(source_file.timeout_nodes) > 0}.items() if value] for elem in source_file.root.iter(): @@ -226,18 +231,18 @@ def check_parsed(repo_root, path, f): def output_errors(errors): for error_type, error, line_number in errors: - print "%s: %s" % (error_type, error) + print("%s: %s" % (error_type, error)) def output_error_count(error_count): if not error_count: return - by_type = " ".join("%s: %d" % item for item in error_count.iteritems()) + by_type = " ".join("%s: %d" % item for item in error_count.items()) count = sum(error_count.values()) if count == 1: - print "There was 1 error (%s)" % (by_type,) + print("There was 1 error (%s)" % (by_type,)) else: - print "There were %d errors (%s)" % (count, by_type) + print("There were %d errors (%s)" % (count, by_type)) def parse_args(): parser = argparse.ArgumentParser() @@ -283,7 +288,7 @@ def run_lint(path, fn, last, *args): output_error_count(error_count) if error_count: - print ERROR_MSG % (last[0], last[1], last[0], last[1]) + print(ERROR_MSG % (last[0], last[1], last[0], last[1])) return sum(error_count.itervalues()) path_lints = [check_path_length] From 14f2fbb62df758ee33ddbd4ff79714e13af982e3 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 22:53:48 +0200 Subject: [PATCH 5/5] Add a simple test for lint. --- .travis.yml | 2 +- lint/__init__.py | 2 +- lint/tests/__init__.py | 0 lint/tests/test_lint.py | 5 +++++ 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 lint/tests/__init__.py create mode 100644 lint/tests/test_lint.py diff --git a/.travis.yml b/.travis.yml index e73d1dc380cce0..23a679badcbc6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,5 +8,5 @@ install: - pip install ./html5lib - pip install pytest-travis-fold script: - - py.test manifest + - py.test lint manifest - flake8 diff --git a/lint/__init__.py b/lint/__init__.py index 1595338cd6d9d9..e5eb5e12d5f89f 100644 --- a/lint/__init__.py +++ b/lint/__init__.py @@ -1 +1 @@ -import lint +from . import lint diff --git a/lint/tests/__init__.py b/lint/tests/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/lint/tests/test_lint.py b/lint/tests/test_lint.py new file mode 100644 index 00000000000000..59f92085cb1383 --- /dev/null +++ b/lint/tests/test_lint.py @@ -0,0 +1,5 @@ +from lint.lint import filter_whitelist_errors + +def test_lint(): + filtered = filter_whitelist_errors({}, '', []) + assert filtered == []