Skip to content

Instantly share code, notes, and snippets.

@ahal
Last active July 17, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahal/533b0fc7db7b52cf6dfce90b01aa0f30 to your computer and use it in GitHub Desktop.
Save ahal/533b0fc7db7b52cf6dfce90b01aa0f30 to your computer and use it in GitHub Desktop.
diff --git a/tools/lint/license.yml b/tools/lint/license.yml
--- a/tools/lint/license.yml
+++ b/tools/lint/license.yml
@@ -1,28 +1,44 @@
---
license:
description: License Check
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
- 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']
+ extensions:
+ - .c
+ - .cc
+ - .cpp
+ - .h
+ - .html
+ - .js
+ - .jsm
+ - .jsx
+ - .m
+ - .mm
+ - .py
+ - .rs
+ - .xhtml
+ - .xml
+ - .xul
support-files:
- 'tools/lint/license/**'
type: external
payload: license:lint
diff --git a/tools/lint/license/__init__.py b/tools/lint/license/__init__.py
--- a/tools/lint/license/__init__.py
+++ b/tools/lint/license/__init__.py
@@ -1,54 +1,135 @@
# 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 = []
+mpl2_license_template = [
+ "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/."
+]
+
+public_domain_license = [
+ "Any copyright is dedicated to the public domain.",
+ "http://creativecommons.org/publicdomain/zero/1.0/"
+]
+
+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()
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 add_header(filename, header):
+ """
+ Add the header to the top of the file
+ """
+ with open(filename, 'r+') as f:
+ content = f.read()
+ f.seek(0, 0)
+ f.write('\n'.join(header) + '\n\n' + content)
+
+
+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 = mpl2_license_template
+ test = False
+
+ if is_test(filename):
+ license_template = public_domain_license
+ test = True
+
+ if ext in ['.cpp', '.c', '.cc', '.h', '.m', '.mm', '.rs', '.js', '.jsm', '.jsx']:
+ license.append("/* " + license_template[0])
+ if test:
+ license.append(" * " + license_template[1] + " */")
+ else:
+ license.append(" * " + license_template[1])
+ license.append(" * " + license_template[2] + " */")
+ add_header(filename, license)
+ return
+
+ if ext in ['.py'] or filename.endswith(".inc.xul"):
+ license.append("# " + license_template[0])
+ license.append("# " + license_template[1])
+ if not test:
+ license.append("# " + license_template[2])
+ add_header(filename, license)
+ return
+
+ if ext in ['.xml', '.xul', '.html', '.xhtml']:
+ license.append("<!-- " + license_template[0])
+ if test:
+ license.append(" - " + license_template[1] + " -->")
+ else:
+ license.append(" - " + license_template[1])
+ license.append(" - " + license_template[2] + " -->")
+ add_header(filename, license)
+ return
+
+
def lint(paths, config, fix=None, **lintargs):
files = list(expand_exclusions(paths, config, lintargs['root']))
licenses = load_valid_license()
+
+ if licenses[-1] == "\n":
+ print("{} cannot end by an empty line".format(license_list))
+ return 1
+
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,
'message': "No matching license strings found in tools/lint/license/valid-licenses.txt", # noqa
'level': 'error'
}
results.append(result.from_config(config, **res))
+ if fix:
+ fix_me(f)
return results
diff --git a/tools/lint/license/valid-licenses.txt b/tools/lint/license/valid-licenses.txt
--- a/tools/lint/license/valid-licenses.txt
+++ b/tools/lint/license/valid-licenses.txt
@@ -1,18 +1,25 @@
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
+This is an autogenerated libdef stub for
+This program is free software; you can redistribute it and/or modify
+This library is free software; you can redistribute it
+This Source Code Form is subject to the terms of the Apache License
+jquery.org/license
+Licensed under MIT
+Dual licenced under the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment