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.
This commit is contained in:
edunham 2016-04-19 13:57:09 -07:00 committed by UK992
parent 1f4dd8765f
commit 48ace17b54
7 changed files with 73 additions and 102 deletions

View file

@ -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.")