зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1562645 - Add an autofix to the license check r=ahal
Depends on D37082 Differential Revision: https://phabricator.services.mozilla.com/D37216 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
d79bc4f9be
Коммит
ba5e16b74e
|
@ -4,6 +4,8 @@ license:
|
|||
include:
|
||||
- .
|
||||
exclude:
|
||||
- browser/app/blocklist.xml
|
||||
- browser/components/pocket/
|
||||
- config/external/nspr/_pl_bld.h
|
||||
- config/external/nspr/_pr_bld.h
|
||||
- gfx/2d/MMIHelpers.h
|
||||
|
@ -20,8 +22,22 @@ license:
|
|||
- toolkit/components/reputationservice/chromium/chrome/common/safe_browsing/csd.pb.h
|
||||
- toolkit/mozapps/update/updater/crctable.h
|
||||
- xpcom/io/crc32c.h
|
||||
extensions: ['.cpp', '.c', '.cc', '.h', '.m', '.mm']
|
||||
# , 'js', 'jsm', 'jsx', 'xml', 'xul', 'html', 'xhtml', 'py', 'rs']
|
||||
extensions:
|
||||
- .c
|
||||
- .cc
|
||||
- .cpp
|
||||
- .h
|
||||
- .html
|
||||
- .js
|
||||
- .jsm
|
||||
- .jsx
|
||||
- .m
|
||||
- .mm
|
||||
- .py
|
||||
- .rs
|
||||
- .xhtml
|
||||
- .xml
|
||||
- .xul
|
||||
support-files:
|
||||
- 'tools/lint/license/**'
|
||||
type: external
|
||||
|
|
|
@ -13,14 +13,31 @@ here = os.path.abspath(os.path.dirname(__file__))
|
|||
|
||||
results = []
|
||||
|
||||
# Official source: https://www.mozilla.org/en-US/MPL/headers/
|
||||
TEMPLATES = {
|
||||
"mpl2_license":
|
||||
"""
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
""".strip().splitlines(),
|
||||
"public_domain_license":
|
||||
"""
|
||||
Any copyright is dedicated to the public domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/
|
||||
""".strip().splitlines(),
|
||||
}
|
||||
license_list = os.path.join(here, 'valid-licenses.txt')
|
||||
|
||||
|
||||
def load_valid_license():
|
||||
"""
|
||||
Load the list of license patterns
|
||||
"""
|
||||
license_list = os.path.join(here, 'valid-licenses.txt')
|
||||
with open(license_list) as f:
|
||||
return f.readlines()
|
||||
l = f.readlines()
|
||||
# Remove the empty lines
|
||||
return filter(bool, [x.replace('\n', '') for x in l])
|
||||
|
||||
|
||||
def is_valid_license(licenses, filename):
|
||||
|
@ -37,13 +54,96 @@ def is_valid_license(licenses, filename):
|
|||
return False
|
||||
|
||||
|
||||
def add_header(filename, header):
|
||||
"""
|
||||
Add the header to the top of the file
|
||||
"""
|
||||
header.append("\n")
|
||||
with open(filename, 'r+') as f:
|
||||
# lines in list format
|
||||
lines = f.readlines()
|
||||
|
||||
i = 0
|
||||
if lines:
|
||||
# if the file isn't empty (__init__.py can be empty files)
|
||||
if lines[0].startswith("#!") or lines[0].startswith("<?xml "):
|
||||
i = 1
|
||||
|
||||
if lines[0].startswith("/* -*- Mode"):
|
||||
i = 2
|
||||
# Insert in the top of the data structure
|
||||
lines[i:i] = header
|
||||
f.seek(0, 0)
|
||||
f.write("".join(lines))
|
||||
|
||||
|
||||
def is_test(f):
|
||||
"""
|
||||
is the file a test or not?
|
||||
"""
|
||||
return ("/test" in f or "/gtest" in f or "/crashtest" in f or "/mochitest" in f
|
||||
or "/reftest" in f or "/imptest" in f or "/androidTest" in f
|
||||
or "/jit-test/" in f or "jsapi-tests/" in f)
|
||||
|
||||
|
||||
def fix_me(filename):
|
||||
"""
|
||||
Add the copyright notice to the top of the file
|
||||
"""
|
||||
_, ext = os.path.splitext(filename)
|
||||
license = []
|
||||
|
||||
license_template = TEMPLATES['mpl2_license']
|
||||
test = False
|
||||
|
||||
if is_test(filename):
|
||||
license_template = TEMPLATES['public_domain_license']
|
||||
test = True
|
||||
|
||||
if ext in ['.cpp', '.c', '.cc', '.h', '.m', '.mm', '.rs', '.js', '.jsm', '.jsx']:
|
||||
for i, l in enumerate(license_template):
|
||||
start = " "
|
||||
end = ""
|
||||
if i == 0:
|
||||
# first line, we have the /*
|
||||
start = "/"
|
||||
if i == len(license_template) - 1:
|
||||
# Last line, we end by */
|
||||
end = " */"
|
||||
license.append(start + "* " + l.strip() + end + "\n")
|
||||
|
||||
add_header(filename, license)
|
||||
return
|
||||
|
||||
if ext in ['.py'] or filename.endswith(".inc.xul"):
|
||||
for l in license_template:
|
||||
license.append("# " + l.strip() + "\n")
|
||||
add_header(filename, license)
|
||||
return
|
||||
|
||||
if ext in ['.xml', '.xul', '.html', '.xhtml']:
|
||||
for i, l in enumerate(license_template):
|
||||
start = " - "
|
||||
end = ""
|
||||
if i == 0:
|
||||
# first line, we have the <!--
|
||||
start = "<!-- "
|
||||
if i == 2 or (i == 1 and test):
|
||||
# Last line, we end by -->
|
||||
end = " -->"
|
||||
license.append(start + l.strip() + end + "\n")
|
||||
add_header(filename, license)
|
||||
return
|
||||
|
||||
|
||||
def lint(paths, config, fix=None, **lintargs):
|
||||
files = list(expand_exclusions(paths, config, lintargs['root']))
|
||||
|
||||
licenses = load_valid_license()
|
||||
|
||||
for f in files:
|
||||
if "/test" in f or "/gtest" in f:
|
||||
# We don't require license for test
|
||||
if is_test(f):
|
||||
# For now, do not do anything with test (too many)
|
||||
continue
|
||||
if not is_valid_license(licenses, f):
|
||||
res = {'path': f,
|
||||
|
@ -51,4 +151,6 @@ def lint(paths, config, fix=None, **lintargs):
|
|||
'level': 'error'
|
||||
}
|
||||
results.append(result.from_config(config, **res))
|
||||
if fix:
|
||||
fix_me(f)
|
||||
return results
|
||||
|
|
|
@ -16,3 +16,6 @@ License: Public domain. You are free to use this code however you
|
|||
You are granted a license to use, reproduce and create derivative works
|
||||
GENERATED FILE, DO NOT EDIT
|
||||
This code is governed by the BSD license
|
||||
This Source Code Form is subject to the terms of the Apache License
|
||||
DO NOT EDIT
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче