From 48ace17b5419a9795a13bded286e113e79d56905 Mon Sep 17 00:00:00 2001 From: edunham Date: Tue, 19 Apr 2016 13:57:09 -0700 Subject: Improve tidy's license validation logic fixes https://github.com/servo/servo/issues/10716 I took the lazy way out and hardcoded the size of block we examine for licenses. fixes https://github.com/servo/servo/issues/10719 Includes tests for new functionality. --- python/tidy/servo_tidy/tidy.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'python/tidy/servo_tidy/tidy.py') diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 3433e8265ed..fac5f8e7063 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -17,12 +17,9 @@ import site import StringIO import subprocess import sys -from licenseck import licenses, licenses_toml, licenses_dep_toml +from licenseck import MPL, APACHE, COPYRIGHT, licenses_toml, licenses_dep_toml -# License and header checks -EMACS_HEADER = "/* -*- Mode:" -VIM_HEADER = "/* vim:" -MAX_LICENSE_LINESPAN = max(len(license.splitlines()) for license in licenses) +COMMENTS = ["// ", "# ", " *", "/* "] # File patterns to include in the non-WPT tidy check. FILE_PATTERNS_TO_CHECK = ["*.rs", "*.rc", "*.cpp", "*.c", @@ -55,7 +52,6 @@ IGNORED_FILES = [ IGNORED_DIRS = [ # Upstream os.path.join(".", "support", "android", "apk"), - os.path.join(".", "support", "rust-task_info"), os.path.join(".", "tests", "wpt", "css-tests"), os.path.join(".", "tests", "wpt", "harness"), os.path.join(".", "tests", "wpt", "update"), @@ -147,13 +143,35 @@ def filter_files(start_dir, only_changed_files, progress): yield file_name +def uncomment(line): + for c in COMMENTS: + if line.startswith(c): + if line.endswith("*/"): + return line[len(c):(len(line) - 3)].strip() + return line[len(c):].strip() + + +def licensed_mpl(header): + return MPL in header + + +def licensed_apache(header): + if APACHE in header: + return any(c in header for c in COPYRIGHT) + + def check_license(file_name, lines): if any(file_name.endswith(ext) for ext in (".toml", ".lock", ".json")): raise StopIteration - while lines and (lines[0].startswith(EMACS_HEADER) or lines[0].startswith(VIM_HEADER)): - lines = lines[1:] - contents = "".join(lines[:MAX_LICENSE_LINESPAN]) - valid_license = any(contents.startswith(license) for license in licenses) + block = min(len(lines), licenseck.MAX_LICENSE_LINESPAN) + license_block = [] + for l in lines[:block]: + l = l.rstrip('\n') + line = uncomment(l) + if line is not None: + license_block += [line] + contents = " ".join(license_block) + valid_license = licensed_mpl(contents) or licensed_apache(contents) acknowledged_bad_license = "xfail-license" in contents if not (valid_license or acknowledged_bad_license): yield (1, "incorrect license") @@ -305,8 +323,8 @@ def check_toml(file_name, lines): for idx, line in enumerate(lines): if line.find("*") != -1: yield (idx + 1, "found asterisk instead of minimum version number") - for license in licenses_toml: - ok_licensed |= (license in line) + for license_line in licenses_toml: + ok_licensed |= (license_line in line) if not ok_licensed: yield (0, ".toml file should contain a valid license.") -- cgit v1.2.3 From 9a3b61abe1ad633ab723ca6cbb7180a4992ead5a Mon Sep 17 00:00:00 2001 From: UK992 Date: Tue, 9 Aug 2016 16:14:43 +0200 Subject: Check for blank line after shebang and grab actual comment block instead of hard-coded one --- python/tidy/servo_tidy/tidy.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'python/tidy/servo_tidy/tidy.py') diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index fac5f8e7063..93b5d8f6452 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -163,13 +163,23 @@ def licensed_apache(header): def check_license(file_name, lines): if any(file_name.endswith(ext) for ext in (".toml", ".lock", ".json")): raise StopIteration - block = min(len(lines), licenseck.MAX_LICENSE_LINESPAN) + + if lines[0].startswith("#!") and lines[1].strip(): + yield (1, "missing blank line after shebang") + + blank_lines = 0 + max_blank_lines = 2 if lines[0].startswith("#!") else 1 license_block = [] - for l in lines[:block]: + + for l in lines: l = l.rstrip('\n') + if not l.strip(): + blank_lines += 1 + if blank_lines >= max_blank_lines: + break line = uncomment(l) if line is not None: - license_block += [line] + license_block.append(line) contents = " ".join(license_block) valid_license = licensed_mpl(contents) or licensed_apache(contents) acknowledged_bad_license = "xfail-license" in contents -- cgit v1.2.3 From fed0e94ec6d4ac630fa641e32644a7e055e2e6f0 Mon Sep 17 00:00:00 2001 From: UK992 Date: Sat, 13 Aug 2016 03:13:01 +0200 Subject: Add support colored text in Windows Console --- python/tidy/servo_tidy/tidy.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'python/tidy/servo_tidy/tidy.py') diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 93b5d8f6452..eb22f98a6b3 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -18,6 +18,7 @@ import StringIO import subprocess import sys from licenseck import MPL, APACHE, COPYRIGHT, licenses_toml, licenses_dep_toml +import colorama COMMENTS = ["// ", "# ", " *", "/* "] @@ -785,8 +786,10 @@ def scan(only_changed_files=False, progress=True): errors = itertools.chain(errors, dep_license_errors, wpt_lint_errors) error = None for error in errors: + colorama.init() print "\r\033[94m{}\033[0m:\033[93m{}\033[0m: \033[91m{}\033[0m".format(*error) print if error is None: + colorama.init() print "\033[92mtidy reported no errors.\033[0m" return int(error is not None) -- cgit v1.2.3