blob: 1d40742dc03db34524417a47077ae4cba6357aec [file] [log] [blame]
Ryan Harrisone76f4da2020-06-03 13:41:41 +00001# Copyright 2020 The Tint Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Presubmit script for Tint.
15See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
16for more details about the presubmit API built into depot_tools.
17"""
18
dan sinclaird9496f32020-11-16 15:01:27 +000019import re
20
Ryan Harrisone76f4da2020-06-03 13:41:41 +000021
22def _LicenseHeader(input_api):
23 """Returns the license header regexp."""
Ryan Harrison7d60a4c2020-06-03 16:12:09 +000024 # Accept any year number from 2019 to the current year
Ryan Harrisone76f4da2020-06-03 13:41:41 +000025 current_year = int(input_api.time.strftime('%Y'))
26 allowed_years = (str(s) for s in reversed(xrange(2019, current_year + 1)))
27 years_re = '(' + '|'.join(allowed_years) + ')'
28 license_header = (
29 r'.*? Copyright( \(c\))? %(year)s The Tint [Aa]uthors\n '
30 r'.*?\n'
31 r'.*? Licensed under the Apache License, Version 2.0 (the "License");\n'
32 r'.*? you may not use this file except in compliance with the License.\n'
33 r'.*? You may obtain a copy of the License at\n'
34 r'.*?\n'
35 r'.*? http://www.apache.org/licenses/LICENSE-2.0\n'
36 r'.*?\n'
37 r'.*? Unless required by applicable law or agreed to in writing, software\n'
38 r'.*? distributed under the License is distributed on an "AS IS" BASIS,\n'
39 r'.*? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
40 r'.*? See the License for the specific language governing permissions and\n'
41 r'.*? limitations under the License.\n') % {
42 'year': years_re,
43 }
44 return license_header
45
46
dan sinclaird9496f32020-11-16 15:01:27 +000047REGEXES = [
48 r"(?i)black[-_]?list",
49 r"(?i)white[-_]?list",
50 r"(?i)gr[ea]y[-_]?list",
51 r"(?i)(first class citizen)",
52 r"(?i)black[-_]?hat",
53 r"(?i)white[-_]?hat",
54 r"(?i)gr[ea]y[-_]?hat",
55 r"(?i)master",
56 r"(?i)slave",
57 r"(?i)\bhim\b",
58 r"(?i)\bhis\b",
59 r"(?i)\bshe\b",
60 r"(?i)\bher\b",
61 r"(?i)\bguys\b",
62 r"(?i)\bhers\b",
63 r"(?i)\bman\b",
64 r"(?i)\bwoman\b",
65 r"(?i)\she\s",
66 r"(?i)\she$",
67 r"(?i)^he\s",
68 r"(?i)^he$",
69 r"(?i)\she['|\u2019]d\s",
70 r"(?i)\she['|\u2019]d$",
71 r"(?i)^he['|\u2019]d\s",
72 r"(?i)^he['|\u2019]d$",
73 r"(?i)\she['|\u2019]s\s",
74 r"(?i)\she['|\u2019]s$",
75 r"(?i)^he['|\u2019]s\s",
76 r"(?i)^he['|\u2019]s$",
77 r"(?i)\she['|\u2019]ll\s",
78 r"(?i)\she['|\u2019]ll$",
79 r"(?i)^he['|\u2019]ll\s",
80 r"(?i)^he['|\u2019]ll$",
81 r"(?i)grandfather",
82 r"(?i)\bmitm\b",
83 r"(?i)\bcrazy\b",
84 r"(?i)\binsane\b",
85 r"(?i)\bblind\sto\b",
86 r"(?i)\bflying\sblind\b",
87 r"(?i)\bblind\seye\b",
88 r"(?i)\bcripple\b",
89 r"(?i)\bcrippled\b",
90 r"(?i)\bdumb\b",
91 r"(?i)\bdummy\b",
92 r"(?i)\bparanoid\b",
93 r"(?i)\bsane\b",
94 r"(?i)\bsanity\b",
95 r"(?i)red[-_]?line",
96]
97
98REGEX_LIST = []
99for reg in REGEXES:
100 REGEX_LIST.append(re.compile(reg))
101
102def CheckNonInclusiveLanguage(input_api, output_api, source_file_filter=None):
103 """Checks the files for non-inclusive language."""
104
105 matches = []
106 for f in input_api.AffectedFiles(include_deletes=False,
107 file_filter=source_file_filter):
108 for line_num, line in f.ChangedContents():
109 for reg in REGEX_LIST:
110 match = reg.search(line)
111 if match:
112 matches.append("{} ({}): found non-inclusive language: {}".
113 format(f.LocalPath(), line_num, match.group(0)))
114
115 if len(matches):
116 return [output_api.PresubmitPromptWarning(
117 'Non-inclusive language found:', items=matches)]
118
119 return []
120
121
Ryan Harrisone76f4da2020-06-03 13:41:41 +0000122def CheckChange(input_api, output_api):
123 results = []
124
125 results += input_api.canned_checks.CheckChangeHasDescription(
126 input_api, output_api)
127 results += input_api.canned_checks.CheckPatchFormatted(input_api,
128 output_api,
129 check_python=True)
130 results += input_api.canned_checks.CheckGNFormatted(input_api, output_api)
131 results += input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
132 input_api, output_api)
133 results += input_api.canned_checks.CheckChangeHasNoTabs(
134 input_api, output_api)
135 results += input_api.canned_checks.CheckChangeTodoHasOwner(
136 input_api, output_api)
137 results += input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
138 input_api, output_api)
139 results += input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)
dan sinclaird5fd7e02020-11-03 16:26:09 +0000140 results += input_api.canned_checks.CheckChangeLintsClean(input_api,
141 output_api,
142 lint_filters="")
dan sinclaird9496f32020-11-16 15:01:27 +0000143 results += CheckNonInclusiveLanguage(input_api, output_api)
Ryan Harrisone76f4da2020-06-03 13:41:41 +0000144
145 return results
146
147
148def CheckChangeOnUpload(input_api, output_api):
149 return CheckChange(input_api, output_api)
150
151
152def CheckChangeOnCommit(input_api, output_api):
153 return CheckChange(input_api, output_api)