summaryrefslogtreecommitdiffstats
path: root/tools/lint/license/__init__.py
blob: e49b3b3dea9e056040393edae017edda449c4d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 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 https://mozilla.org/MPL/2.0/.

import os
from glob import glob
from html.parser import HTMLParser

from mozlint import result
from mozlint.pathutils import expand_exclusions

here = os.path.abspath(os.path.dirname(__file__))
topsrcdir = os.path.join(here, "..", "..", "..")

# 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 https://mozilla.org/MPL/2.0/.
    """.strip().splitlines(),
    "public_domain_license": """
    Any copyright is dedicated to the public domain.
    https://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
    """
    with open(license_list) as f:
        l = f.readlines()
        # Remove the empty lines
        return list(filter(bool, [x.replace("\n", "") for x in l]))


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
    """
    with open(filename, "r", errors="replace") as myfile:
        contents = myfile.read()
        # Empty files don't need a license.
        if not contents:
            return True

        for l in licenses:
            if l.lower().strip() in contents.lower():
                return True
    return False


def add_header(log, filename, header):
    """
    Add the header to the top of the file
    """
    header.append("\n")
    with open(filename, "r+") as f:
        # lines in list format
        try:
            lines = f.readlines()
        except UnicodeDecodeError as e:
            log.debug("Could not read file '{}'".format(f))
            log.debug("Error: {}".format(e))
            return

        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?
    """
    if "lint/test/" in f or "lint_license_test_tmp_file.js" in f:
        # For the unit tests
        return False
    return (
        "/tests/" in f
        or "/test/" in f
        or "/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(log, 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",
        ".java",
        ".js",
        ".jsm",
        ".jsx",
        ".mjs",
        ".css",
        ".idl",
        ".webidl",
    ]:
        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(log, filename, license)
        return True

    if ext in [".py", ".ftl", ".properties"]:
        for l in license_template:
            license.append("# " + l.strip() + "\n")
        add_header(log, filename, license)
        return True

    if ext in [".xml", ".html", ".xhtml", ".dtd", ".svg"]:
        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)
            if ext != ".svg" or not end:
                # When dealing with an svg, we should not have a space between
                # the license and the content
                license.append("\n")
        add_header(log, filename, license)
        return True

    # In case we don't know how to handle a specific format.
    return False


class HTMLParseError(Exception):
    def __init__(self, msg, pos):
        super().__init__(msg, *pos)


class LicenseHTMLParser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.in_code = False
        self.invalid_paths = []

    def handle_starttag(self, tag, attrs):
        if tag == "code":
            if self.in_code:
                raise HTMLParseError("nested code tag", self.getpos())
            self.in_code = True

    def handle_endtag(self, tag):
        if tag == "code":
            if not self.in_code:
                raise HTMLParseError("not started code tag", self.getpos())
            self.in_code = False

    def handle_data(self, data):
        if self.in_code:
            path = data.strip()
            abspath = os.path.join(topsrcdir, path)
            if not glob(abspath):
                self.invalid_paths.append((path, self.getpos()))


def lint_license_html(path):
    parser = LicenseHTMLParser()
    with open(path) as fd:
        content = fd.read()
        parser.feed(content)
    return parser.invalid_paths


def is_html_licence_summary(path):
    license_html = os.path.join(topsrcdir, "toolkit", "content", "license.html")
    return os.path.samefile(path, license_html)


def lint(paths, config, fix=None, **lintargs):
    results = []
    log = lintargs["log"]
    files = list(expand_exclusions(paths, config, lintargs["root"]))
    fixed = 0

    licenses = load_valid_license()
    for f in files:
        if is_test(f):
            # For now, do not do anything with test (too many)
            continue

        if not is_valid_license(licenses, f):
            if fix and fix_me(log, f):
                fixed += 1
            else:
                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 is_html_licence_summary(f):
            try:
                for invalid_path, (lineno, column) in lint_license_html(f):
                    res = {
                        "path": f,
                        "message": "references unknown path {}".format(invalid_path),
                        "level": "error",
                        "lineno": lineno,
                        "column": column,
                    }
                    results.append(result.from_config(config, **res))
            except HTMLParseError as err:
                res = {
                    "path": f,
                    "message": err.args[0],
                    "level": "error",
                    "lineno": err.args[1],
                    "column": err.args[2],
                }
                results.append(result.from_config(config, **res))

    return {"results": results, "fixed": fixed}