aboutsummaryrefslogtreecommitdiffstats
path: root/python/tidy.py
blob: 04743433dbfa71090d017119b5d5704478900923 (plain) (blame)
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
# Copyright 2013 The Servo Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

#!/usr/bin/env python

import os
from licenseck import check_license

# FIXME(#3242): Don't use globals
err = 0


def report_error_name_no(name, no, s):
    global err
    print("%s:%d: %s" % (name, no, s))
    err = 1


def do_license_check(name, contents):
    if not check_license(name, contents):
        report_error_name_no(name, 1, "incorrect license")


def do_whitespace_check(name, contents):
    for idx, line in enumerate(contents):
        if line[-1] == "\n":
            line = line[:-1]
        else:
            report_error_name_no(name, idx + 1, "No newline at EOF")

        if line.endswith(' '):
            report_error_name_no(name, idx + 1, "trailing whitespace")

        if '\t' in line:
            report_error_name_no(name, idx + 1, "tab on line")

        if '\r' in line:
            report_error_name_no(name, idx + 1, "CR on line")


exceptions = [
    # Upstream
    "support",
    "tests/wpt/web-platform-tests",

    # Generated and upstream code combined with our own. Could use cleanup
    "components/script/dom/bindings/codegen",
    "components/style/properties/mod.rs",
]


def should_check(name):
    if ".#" in name:
        return False
    if not (name.endswith(".rs")
            or name.endswith(".rc")
            or name.endswith(".cpp")
            or name.endswith(".c")
            or name.endswith(".h")
            or name.endswith(".py")):
        return False
    for exception in exceptions:
        if exception in name:
            return False
    return True


def scan(start_path):
    global err
    err = 0

    file_names = []
    for root, dirs, files in os.walk(start_path):
        for myfile in files:
            file_name = root + "/" + myfile
            if should_check(file_name):
                file_names.append(file_name)

    for path in file_names:
        with open(path, "r") as fp:
            lines = fp.readlines()
            do_license_check(path, "".join(lines))
            do_whitespace_check(path, lines)

    return err