blob: 7e019b76b60b6980ce5fcfd4e28428e2301972d3 [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 Harrison6d27f232021-07-20 17:16:31 +000021USE_PYTHON3 = True
22
Ryan Harrisone76f4da2020-06-03 13:41:41 +000023
24def _LicenseHeader(input_api):
25 """Returns the license header regexp."""
Ryan Harrison7d60a4c2020-06-03 16:12:09 +000026 # Accept any year number from 2019 to the current year
Ryan Harrisone76f4da2020-06-03 13:41:41 +000027 current_year = int(input_api.time.strftime('%Y'))
28 allowed_years = (str(s) for s in reversed(xrange(2019, current_year + 1)))
29 years_re = '(' + '|'.join(allowed_years) + ')'
30 license_header = (
31 r'.*? Copyright( \(c\))? %(year)s The Tint [Aa]uthors\n '
32 r'.*?\n'
33 r'.*? Licensed under the Apache License, Version 2.0 (the "License");\n'
34 r'.*? you may not use this file except in compliance with the License.\n'
35 r'.*? You may obtain a copy of the License at\n'
36 r'.*?\n'
37 r'.*? http://www.apache.org/licenses/LICENSE-2.0\n'
38 r'.*?\n'
39 r'.*? Unless required by applicable law or agreed to in writing, software\n'
40 r'.*? distributed under the License is distributed on an "AS IS" BASIS,\n'
41 r'.*? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
42 r'.*? See the License for the specific language governing permissions and\n'
43 r'.*? limitations under the License.\n') % {
44 'year': years_re,
45 }
46 return license_header
47
48
dan sinclaird9496f32020-11-16 15:01:27 +000049REGEXES = [
50 r"(?i)black[-_]?list",
51 r"(?i)white[-_]?list",
52 r"(?i)gr[ea]y[-_]?list",
53 r"(?i)(first class citizen)",
54 r"(?i)black[-_]?hat",
55 r"(?i)white[-_]?hat",
56 r"(?i)gr[ea]y[-_]?hat",
57 r"(?i)master",
58 r"(?i)slave",
59 r"(?i)\bhim\b",
60 r"(?i)\bhis\b",
61 r"(?i)\bshe\b",
62 r"(?i)\bher\b",
63 r"(?i)\bguys\b",
64 r"(?i)\bhers\b",
65 r"(?i)\bman\b",
66 r"(?i)\bwoman\b",
67 r"(?i)\she\s",
68 r"(?i)\she$",
69 r"(?i)^he\s",
70 r"(?i)^he$",
71 r"(?i)\she['|\u2019]d\s",
72 r"(?i)\she['|\u2019]d$",
73 r"(?i)^he['|\u2019]d\s",
74 r"(?i)^he['|\u2019]d$",
75 r"(?i)\she['|\u2019]s\s",
76 r"(?i)\she['|\u2019]s$",
77 r"(?i)^he['|\u2019]s\s",
78 r"(?i)^he['|\u2019]s$",
79 r"(?i)\she['|\u2019]ll\s",
80 r"(?i)\she['|\u2019]ll$",
81 r"(?i)^he['|\u2019]ll\s",
82 r"(?i)^he['|\u2019]ll$",
83 r"(?i)grandfather",
84 r"(?i)\bmitm\b",
85 r"(?i)\bcrazy\b",
86 r"(?i)\binsane\b",
87 r"(?i)\bblind\sto\b",
88 r"(?i)\bflying\sblind\b",
89 r"(?i)\bblind\seye\b",
90 r"(?i)\bcripple\b",
91 r"(?i)\bcrippled\b",
92 r"(?i)\bdumb\b",
93 r"(?i)\bdummy\b",
94 r"(?i)\bparanoid\b",
95 r"(?i)\bsane\b",
96 r"(?i)\bsanity\b",
97 r"(?i)red[-_]?line",
98]
99
100REGEX_LIST = []
101for reg in REGEXES:
102 REGEX_LIST.append(re.compile(reg))
103
104def CheckNonInclusiveLanguage(input_api, output_api, source_file_filter=None):
Ryan Harrison6d27f232021-07-20 17:16:31 +0000105 """Checks the files for non-inclusive language."""
dan sinclaird9496f32020-11-16 15:01:27 +0000106
Ryan Harrison6d27f232021-07-20 17:16:31 +0000107 matches = []
108 for f in input_api.AffectedFiles(include_deletes=False,
109 file_filter=source_file_filter):
110 for line_num, line in f.ChangedContents():
111 for reg in REGEX_LIST:
112 match = reg.search(line)
113 if match:
114 matches.append(
115 "{} ({}): found non-inclusive language: {}".format(
116 f.LocalPath(), line_num, match.group(0)))
dan sinclaird9496f32020-11-16 15:01:27 +0000117
Ryan Harrison6d27f232021-07-20 17:16:31 +0000118 if len(matches):
119 return [
120 output_api.PresubmitPromptWarning('Non-inclusive language found:',
121 items=matches)
122 ]
dan sinclaird9496f32020-11-16 15:01:27 +0000123
Ryan Harrison6d27f232021-07-20 17:16:31 +0000124 return []
dan sinclaird9496f32020-11-16 15:01:27 +0000125
126
Ryan Harrisone76f4da2020-06-03 13:41:41 +0000127def CheckChange(input_api, output_api):
128 results = []
129
130 results += input_api.canned_checks.CheckChangeHasDescription(
131 input_api, output_api)
132 results += input_api.canned_checks.CheckPatchFormatted(input_api,
133 output_api,
134 check_python=True)
135 results += input_api.canned_checks.CheckGNFormatted(input_api, output_api)
136 results += input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
137 input_api, output_api)
138 results += input_api.canned_checks.CheckChangeHasNoTabs(
139 input_api, output_api)
140 results += input_api.canned_checks.CheckChangeTodoHasOwner(
141 input_api, output_api)
142 results += input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
143 input_api, output_api)
144 results += input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)
dan sinclaird5fd7e02020-11-03 16:26:09 +0000145 results += input_api.canned_checks.CheckChangeLintsClean(input_api,
146 output_api,
147 lint_filters="")
dan sinclaird9496f32020-11-16 15:01:27 +0000148 results += CheckNonInclusiveLanguage(input_api, output_api)
Ryan Harrisone76f4da2020-06-03 13:41:41 +0000149
150 return results
151
152
153def CheckChangeOnUpload(input_api, output_api):
154 return CheckChange(input_api, output_api)
155
156
157def CheckChangeOnCommit(input_api, output_api):
158 return CheckChange(input_api, output_api)