Bug 1562645 - Extend mozlint to have license check r=ahal

The goal is to have something simple and not relying on other tools.
I tried with licensecheck and licensee but, with if we use a different
wording to declare "public domain" (as example), they might not
catch it. Requiring to contribute upstream, etc

Instead, I just create a list of line of license to catch it.
From my trials, it works well enough and it is trivial python.

Differential Revision: https://phabricator.services.mozilla.com/D37082

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sylvestre Ledru 2019-07-09 10:07:37 +00:00
Родитель 1e8901d20d
Коммит d013672392
3 изменённых файлов: 100 добавлений и 0 удалений

28
tools/lint/license.yml Normal file
Просмотреть файл

@ -0,0 +1,28 @@
---
license:
description: License Check
include:
- .
exclude:
- config/external/nspr/_pl_bld.h
- config/external/nspr/_pr_bld.h
- gfx/2d/MMIHelpers.h
- gfx/2d/ShadersD2D1.h
- gfx/layers/protobuf/LayerScopePacket.pb.cc
- gfx/layers/protobuf/LayerScopePacket.pb.h
- gfx/thebes/CJKCompatSVS.cpp
- js/src/builtin/intl/TimeZoneDataGenerated.h
- js/src/irregexp/RegExpCharacters-inl.h
- js/src/irregexp/RegExpCharacters.cpp
- media/libdav1d/vcs_version.h
- mozglue/android/NativeCrypto.h
- toolkit/components/reputationservice/chromium/chrome/common/safe_browsing/csd.pb.cc
- 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']
support-files:
- 'tools/lint/license/**'
type: external
payload: license:lint

Просмотреть файл

@ -0,0 +1,54 @@
# 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/.
from __future__ import absolute_import, print_function
import os
from mozlint import result
from mozlint.pathutils import expand_exclusions
here = os.path.abspath(os.path.dirname(__file__))
results = []
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()
def is_valid_license(licenses, filename):
"""
From a given file, check if we can find the license patterns
in the X first lines of the file
"""
nb_lines = 10
with open(filename) as myfile:
head = myfile.readlines(nb_lines)
for l in licenses:
if l.lower().strip() in ''.join(head).lower():
return True
return False
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
continue
if not is_valid_license(licenses, f):
res = {'path': f,
'message': "No matching license strings found in tools/lint/license/valid-licenses.txt", # noqa
'level': 'error'
}
results.append(result.from_config(config, **res))
return results

Просмотреть файл

@ -0,0 +1,18 @@
mozilla.org/MPL/
Licensed under the Apache License, Version 2.0
copyright is dedicated to the Public Domain.
Licensed under the MIT License
Redistributions of source code must retain the above copyright
Use of this source code is governed by a BSD-style license
The author hereby disclaims copyright to this source code
Use, Modification and Redistribution (including distribution of any
author grants irrevocable permission to anyone to use, modify,
THIS FILE IS AUTO-GENERATED
Permission is hereby granted, free of charge, to any person obtaining
Permission to use, copy, modify, distribute, and sell this software
Permission to use, copy, modify, distribute and sell this software
This file is dual licensed under the MIT
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