2013-04-05 00:50:04 +00:00
|
|
|
# Copyright 2013 The Servo Project Developers. See the COPYRIGHT
|
|
|
|
# file at the top-level directory of this distribution.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
# option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
2014-07-11 11:57:48 +00:00
|
|
|
import os
|
2014-09-10 04:46:34 +00:00
|
|
|
import fnmatch
|
2015-03-07 17:48:51 +00:00
|
|
|
import itertools
|
2015-04-21 17:59:39 +00:00
|
|
|
import re
|
2015-06-04 15:41:36 +00:00
|
|
|
import sys
|
2014-09-10 04:46:34 +00:00
|
|
|
from licenseck import licenses
|
2013-04-05 00:50:04 +00:00
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
filetypes_to_check = [".rs", ".rc", ".cpp", ".c", ".h", ".py"]
|
2015-03-07 17:48:51 +00:00
|
|
|
reftest_directories = ["tests/ref"]
|
|
|
|
reftest_filetype = ".list"
|
2015-06-04 15:41:36 +00:00
|
|
|
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",
|
|
|
|
]
|
2013-04-05 00:50:04 +00:00
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
ignored_files = [
|
|
|
|
# Upstream
|
|
|
|
"support/*",
|
2015-04-08 02:16:49 +00:00
|
|
|
"tests/wpt/*",
|
2015-06-03 13:04:11 +00:00
|
|
|
"python/mach/*",
|
|
|
|
"python/mozdebug/*",
|
|
|
|
"python/mozinfo/*",
|
|
|
|
"python/mozlog/*",
|
|
|
|
"python/toml/*",
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
# Generated and upstream code combined with our own. Could use cleanup
|
|
|
|
"components/script/dom/bindings/codegen/*",
|
|
|
|
"components/style/properties/mod.rs",
|
2015-06-15 19:08:14 +00:00
|
|
|
"target/*",
|
2015-02-28 21:09:48 +00:00
|
|
|
"ports/gonk/src/native_window_glue.cpp",
|
2015-03-31 18:42:56 +00:00
|
|
|
"ports/cef/*",
|
2015-01-08 15:00:57 +00:00
|
|
|
|
|
|
|
# MIT license
|
|
|
|
"components/util/deque/mod.rs",
|
2015-06-03 16:12:34 +00:00
|
|
|
|
|
|
|
# Hidden files/directories
|
|
|
|
".*",
|
2014-09-10 04:46:34 +00:00
|
|
|
]
|
2013-04-05 00:50:04 +00:00
|
|
|
|
|
|
|
|
2015-06-03 16:12:34 +00:00
|
|
|
def collect_file_names(top_directories=None):
|
|
|
|
if top_directories is None:
|
|
|
|
top_directories = os.listdir(".")
|
2014-09-10 04:46:34 +00:00
|
|
|
for top_directory in top_directories:
|
|
|
|
for dirname, dirs, files in os.walk(top_directory):
|
|
|
|
for basename in files:
|
|
|
|
yield os.path.join(dirname, basename)
|
2013-04-05 00:50:04 +00:00
|
|
|
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
def should_check(file_name):
|
|
|
|
if ".#" in file_name:
|
|
|
|
return False
|
|
|
|
if os.path.splitext(file_name)[1] not in filetypes_to_check:
|
|
|
|
return False
|
|
|
|
for pattern in ignored_files:
|
|
|
|
if fnmatch.fnmatch(file_name, pattern):
|
|
|
|
return False
|
|
|
|
return True
|
2014-07-11 11:57:48 +00:00
|
|
|
|
|
|
|
|
2015-03-07 17:48:51 +00:00
|
|
|
def should_check_reftest(file_name):
|
|
|
|
return file_name.endswith(reftest_filetype)
|
|
|
|
|
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
def check_license(contents):
|
|
|
|
valid_license = any(contents.startswith(license) for license in licenses)
|
|
|
|
acknowledged_bad_license = "xfail-license" in contents[:100]
|
|
|
|
if not (valid_license or acknowledged_bad_license):
|
|
|
|
yield (1, "incorrect license")
|
2014-07-11 11:57:48 +00:00
|
|
|
|
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
def check_length(idx, line):
|
2015-05-24 23:27:26 +00:00
|
|
|
if len(line) >= 120:
|
2015-04-21 17:59:39 +00:00
|
|
|
yield (idx + 1, "(much) overlong line")
|
2015-01-19 15:33:48 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
def check_whatwg_url(idx, line):
|
2015-05-06 10:25:28 +00:00
|
|
|
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))
|
2015-01-19 15:33:48 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
def check_whitespace(idx, line):
|
|
|
|
if line[-1] == "\n":
|
|
|
|
line = line[:-1]
|
|
|
|
else:
|
|
|
|
yield (idx + 1, "no newline at EOF")
|
|
|
|
|
|
|
|
if line.endswith(" "):
|
|
|
|
yield (idx + 1, "trailing whitespace")
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
if "\t" in line:
|
|
|
|
yield (idx + 1, "tab on line")
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
if "\r" in line:
|
|
|
|
yield (idx + 1, "CR on line")
|
2013-04-05 00:50:04 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
def check_by_line(contents):
|
|
|
|
lines = contents.splitlines(True)
|
|
|
|
for idx, line in enumerate(lines):
|
2015-06-03 13:04:11 +00:00
|
|
|
errors = itertools.chain(
|
|
|
|
check_length(idx, line),
|
|
|
|
check_whitespace(idx, line),
|
|
|
|
check_whatwg_url(idx, line),
|
|
|
|
)
|
|
|
|
for error in errors:
|
2015-04-21 17:59:39 +00:00
|
|
|
yield error
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2013-04-05 00:50:04 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
def collect_errors_for_files(files_to_check, checking_functions):
|
|
|
|
for file_name in files_to_check:
|
|
|
|
with open(file_name, "r") as fp:
|
|
|
|
contents = fp.read()
|
|
|
|
for check in checking_functions:
|
|
|
|
for error in check(contents):
|
|
|
|
# filename, line, message
|
|
|
|
yield (file_name, error[0], error[1])
|
2014-07-11 11:57:48 +00:00
|
|
|
|
2014-09-09 14:18:18 +00:00
|
|
|
|
2015-03-07 17:48:51 +00:00
|
|
|
def check_reftest_order(files_to_check):
|
|
|
|
for file_name in files_to_check:
|
|
|
|
with open(file_name, "r") as fp:
|
|
|
|
split_lines = fp.read().splitlines()
|
|
|
|
lines = filter(lambda l: len(l) > 0 and l[0] != '#', split_lines)
|
|
|
|
for idx, line in enumerate(lines[:-1]):
|
2015-06-04 15:41:36 +00:00
|
|
|
next_line = lines[idx + 1]
|
2015-03-07 17:48:51 +00:00
|
|
|
current = get_reftest_names(line)
|
|
|
|
next = get_reftest_names(next_line)
|
|
|
|
if current is not None and next is not None and current > next:
|
|
|
|
yield (file_name, split_lines.index(next_line) + 1, "line not in alphabetical order")
|
|
|
|
|
|
|
|
|
|
|
|
def get_reftest_names(line):
|
|
|
|
tokens = line.split()
|
|
|
|
if (len(tokens) == 3):
|
|
|
|
return tokens[1] + tokens[2]
|
|
|
|
if (len(tokens) == 4):
|
|
|
|
return tokens[2] + tokens[3]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-09-10 04:46:34 +00:00
|
|
|
def scan():
|
2015-06-04 15:41:36 +00:00
|
|
|
sys.path += python_dependencies
|
|
|
|
|
2015-06-03 16:12:34 +00:00
|
|
|
all_files = collect_file_names()
|
2014-09-10 04:46:34 +00:00
|
|
|
files_to_check = filter(should_check, all_files)
|
2014-09-09 14:18:18 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
num_flake8_errors = check_flake8(files_to_check)
|
|
|
|
|
2015-04-21 17:59:39 +00:00
|
|
|
checking_functions = [check_license, check_by_line]
|
2014-09-10 04:46:34 +00:00
|
|
|
errors = collect_errors_for_files(files_to_check, checking_functions)
|
2015-03-07 17:48:51 +00:00
|
|
|
|
|
|
|
reftest_files = collect_file_names(reftest_directories)
|
|
|
|
reftest_to_check = filter(should_check_reftest, reftest_files)
|
|
|
|
r_errors = check_reftest_order(reftest_to_check)
|
|
|
|
|
|
|
|
errors = list(itertools.chain(errors, r_errors))
|
2014-09-09 14:18:18 +00:00
|
|
|
|
2015-06-04 15:41:36 +00:00
|
|
|
if errors or num_flake8_errors:
|
2014-09-10 04:46:34 +00:00
|
|
|
for error in errors:
|
|
|
|
print("{}:{}: {}".format(*error))
|
|
|
|
return 1
|
|
|
|
else:
|
2015-03-07 17:48:51 +00:00
|
|
|
print("tidy reported no errors.")
|
2014-09-10 04:46:34 +00:00
|
|
|
return 0
|