blob: 2cec174c5b25c7618f845772ac153d892583a65b [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371# Copyright (C) 2014 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7# * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13# * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Yang Guo75beda92019-10-28 07:29:2528"""
29DevTools presubmit script
Blink Reformat4c46d092018-04-07 15:32:3730
Benedikt Meurerbc9da612024-08-19 10:44:4931See http://goo.gle/devtools-testing-guide#Presubmit-checks for more how to
32run presubmit checks in DevTools.
33
Blink Reformat4c46d092018-04-07 15:32:3734See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
35for more details about the presubmit API built into gcl.
Alex Rudenkod723dba2025-04-29 08:35:5836
37`git cl presubmit -v -v` to debug presubmit checks.
Blink Reformat4c46d092018-04-07 15:32:3738"""
39
40import sys
Tim van der Lippef515fdc2020-03-06 16:18:2541import six
Tim van der Lippefb023462020-08-21 13:10:0642import time
Blink Reformat4c46d092018-04-07 15:32:3743
Alex Rudenko4a7a3242024-04-18 10:36:5044from pathlib import Path
45
Liviu Raufd2e3212019-12-18 15:38:2046AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com"
Tim van der Lippefb1dc172021-05-11 15:40:2647USE_PYTHON3 = True
Alex Rudenkod723dba2025-04-29 08:35:5848PRESUBMIT_VERSION = '2.0.0'
Mathias Bynensa0a6e292019-12-17 12:24:0849
Alex Rudenko537c6312024-07-19 06:22:0550
Alex Rudenkod723dba2025-04-29 08:35:5851def _ExecuteSubProcess(input_api,
52 output_api,
53 script_path,
Benedikt Meurer21ef4f12025-06-12 09:46:5154 script_arguments=None,
Alex Rudenkod723dba2025-04-29 08:35:5855 message=None):
Tim van der Lippef515fdc2020-03-06 16:18:2556 if isinstance(script_path, six.string_types):
Philip Pfaffef4320aa2022-07-21 11:33:2457 script_path = [input_api.python3_executable, script_path]
Tim van der Lippef515fdc2020-03-06 16:18:2558
Tim van der Lippefb023462020-08-21 13:10:0659 start_time = time.time()
Benedikt Meurer21ef4f12025-06-12 09:46:5160 process = input_api.subprocess.Popen(script_path +
61 (script_arguments or []),
Sigurd Schneiderf3a1ecd2021-03-02 14:46:0362 stdout=input_api.subprocess.PIPE,
63 stderr=input_api.subprocess.STDOUT)
Tim van der Lippe4d004ec2020-03-03 18:32:0164 out, _ = process.communicate()
Tim van der Lippefb023462020-08-21 13:10:0665 end_time = time.time()
Tim van der Lippefb023462020-08-21 13:10:0666 time_difference = end_time - start_time
Benedikt Meurer21ef4f12025-06-12 09:46:5167 return [
68 output_api.PresubmitError(
69 "%s (%.1fs): %s" %
70 (message if message is not None else script_path, time_difference,
71 out.decode('utf-8').strip()))
72 ] if process.returncode != 0 else []
Tim van der Lippe4d004ec2020-03-03 18:32:0173
74
Gavin Mak4a41e482024-07-31 17:16:4475def _IsEnvCog(input_api):
76 old_sys_path = sys.path[:]
77 devtools_root = input_api.PresubmitLocalPath()
78 depot_tools = input_api.os_path.join(devtools_root, 'third_party',
79 'depot_tools')
80 try:
81 sys.path.append(depot_tools)
82 from gclient_utils import IsEnvCog
83 if IsEnvCog():
84 return True
85 finally:
86 sys.path = old_sys_path
87 return False
88
89
Alex Rudenkod723dba2025-04-29 08:35:5890def _GetAffectedFiles(input_api, parent_directories, excluded_actions,
91 accepted_endings):
92 """Return absolute file paths of affected files (not due to an excluded action)
93 under a parent directory with an accepted file ending.
94 """
95 local_paths = [
96 f.AbsoluteLocalPath() for f in input_api.AffectedFiles()
97 if all(f.Action() != action for action in excluded_actions)
98 ]
99 affected_files = [
100 file_name for file_name in local_paths
101 if any(parent_directory in file_name
102 for parent_directory in parent_directories) and (
103 len(accepted_endings) == 0 or any(
104 file_name.endswith(accepted_ending)
105 for accepted_ending in accepted_endings))
106 ]
107 return affected_files
108
109
110def _CheckWithNodeScript(input_api,
111 output_api,
112 script_path,
Benedikt Meurer21ef4f12025-06-12 09:46:51113 script_arguments=None,
Alex Rudenkod723dba2025-04-29 08:35:58114 allow_typescript=False,
115 message=None):
116 original_sys_path = sys.path
117 try:
118 sys.path = sys.path + [
119 input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')
120 ]
121 import devtools_paths
122 finally:
123 sys.path = original_sys_path
124
125 process = [devtools_paths.node_path(), script_path]
126
127 if allow_typescript:
128 process.insert(1, '--no-warnings=ExperimentalWarning')
129 process.insert(1, '--experimental-strip-types')
130
131 return _ExecuteSubProcess(input_api,
132 output_api,
133 process,
134 script_arguments=script_arguments,
135 message=message)
136
137
138def _GetFilesToLint(input_api, lint_config_files, default_linted_directories,
139 accepted_endings):
140 run_full_check = False
141 files_to_lint = []
142
143 # We are changing the lint configuration; run the full check.
144 if len(lint_config_files) != 0:
145 run_full_check = True
146 else:
147 # Only run the linter on files that are relevant, to save PRESUBMIT time.
148 files_to_lint = _GetAffectedFiles(input_api,
149 default_linted_directories, ['D'],
150 accepted_endings)
151
152 # Exclude front_end/third_party and front_end/generated files.
153 files_to_lint = [
154 file for file in files_to_lint
155 if "front_end/third_party" not in file
156 and "front_end/generated" not in file
157 ]
158
159 should_bail_out = len(files_to_lint) == 0 and not run_full_check
160 return should_bail_out, files_to_lint
161
162
163def _CheckFormat(input_api, output_api):
164 if _IsEnvCog(input_api):
165 return [
166 output_api.PresubmitPromptWarning(
167 'Non-git environment detected, skipping _CheckFormat.')
168 ]
169
Benedikt Meurer21ef4f12025-06-12 09:46:51170 return _ExecuteSubProcess(input_api,
171 output_api, ['git', 'cl', 'format', '--js'],
172 message='Format')
Alex Rudenkod723dba2025-04-29 08:35:58173
174
175def CheckBugAssociationOnCommit(input_api, output_api):
176 results = []
Sigurd Schneider5c9b4f92021-01-22 10:09:55177 bugs = input_api.change.BugsFromDescription()
178 message = (
179 "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n"
180 "the footer of the commit description. If you explicitly don\'t want to\n"
181 "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n"
182 "Note: The footer of the commit description is the last block of lines in\n"
183 "the commit description that doesn't contain empty lines. This means that\n"
184 "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n"
185 "line are not detected by this presubmit check.")
186
187 if not bugs:
Alex Rudenkod723dba2025-04-29 08:35:58188 results.append(output_api.PresubmitError(message))
Sigurd Schneider5c9b4f92021-01-22 10:09:55189
190 return results
191
192
Alex Rudenkod723dba2025-04-29 08:35:58193def CheckExperimentTelemetry(input_api, output_api):
Brandon Goddard33104372020-08-13 15:49:23194 experiment_telemetry_files = [
195 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Christy Chenab9a44d2021-07-02 19:54:30196 'entrypoints', 'main', 'MainImpl.ts'),
Brandon Goddard33104372020-08-13 15:49:23197 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Tim van der Lippee0247312021-04-01 14:25:30198 'core', 'host', 'UserMetrics.ts')
Brandon Goddard33104372020-08-13 15:49:23199 ]
Alex Rudenkod723dba2025-04-29 08:35:58200 affected_main_files = _GetAffectedFiles(input_api,
Brandon Goddard33104372020-08-13 15:49:23201 experiment_telemetry_files, [],
Christy Chenab9a44d2021-07-02 19:54:30202 ['.ts'])
Brandon Goddard33104372020-08-13 15:49:23203 if len(affected_main_files) == 0:
Alex Rudenkod723dba2025-04-29 08:35:58204 return []
Brandon Goddard33104372020-08-13 15:49:23205
Brandon Goddard33104372020-08-13 15:49:23206 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
207 'scripts', 'check_experiments.js')
Alex Rudenkod723dba2025-04-29 08:35:58208 return _CheckWithNodeScript(input_api,
209 output_api,
210 script_path,
211 message='Experiment telemetry')
Brandon Goddard33104372020-08-13 15:49:23212
213
Alex Rudenkod723dba2025-04-29 08:35:58214def CheckESBuildVersion(input_api, output_api):
Jack Franklinb5a63092022-11-30 14:32:36215 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
216 'scripts',
217 'check_esbuild_versions.js')
Alex Rudenkod723dba2025-04-29 08:35:58218 return _CheckWithNodeScript(input_api,
219 output_api,
220 script_path,
221 message='ESBuild version')
Jack Franklinb5a63092022-11-30 14:32:36222
223
Alex Rudenkod723dba2025-04-29 08:35:58224def CheckDevToolsLint(input_api, output_api):
Mathias Bynens1b2c5e42020-06-18 06:29:21225 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
Nikolay Vitkov2a1b3b32025-01-03 10:18:46226 'scripts', 'test', 'run_lint_check.mjs')
Tim van der Lippe4d004ec2020-03-03 18:32:01227
Mathias Bynens1b2c5e42020-06-18 06:29:21228 front_end_directory = input_api.os_path.join(
229 input_api.PresubmitLocalPath(), 'front_end')
Nikolay Vitkov77ba8df2025-03-27 15:06:08230
Alex Rudenko5556a902020-09-29 09:37:23231 inspector_overlay_directory = input_api.os_path.join(
232 input_api.PresubmitLocalPath(), 'inspector_overlay')
Mathias Bynens1b2c5e42020-06-18 06:29:21233 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
234 'test')
235 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
236 'scripts')
Tim van der Lippe2a4ae2b2020-03-11 17:28:06237
Mathias Bynens1b2c5e42020-06-18 06:29:21238 default_linted_directories = [
Benedikt Meurer6dd23b62024-08-20 12:08:43239 front_end_directory,
240 test_directory,
241 scripts_directory,
242 inspector_overlay_directory,
Mathias Bynens1b2c5e42020-06-18 06:29:21243 ]
Tim van der Lippe2a4ae2b2020-03-11 17:28:06244
Benedikt Meurer6dd23b62024-08-20 12:08:43245 lint_related_files = [
Mathias Bynens1b2c5e42020-06-18 06:29:21246 input_api.os_path.join(input_api.PresubmitLocalPath(),
Nikolay Vitkov55adf672025-01-02 12:07:33247 'eslint.config.mjs'),
Benedikt Meurer6dd23b62024-08-20 12:08:43248 input_api.os_path.join(input_api.PresubmitLocalPath(),
249 '.stylelintrc.json'),
250 input_api.os_path.join(input_api.PresubmitLocalPath(),
251 '.stylelintignore'),
Nikolay Vitkov5b2bcff2025-01-29 19:21:12252 # This file includes the LitAnalyzer rules
253 input_api.os_path.join(input_api.PresubmitLocalPath(),
254 'tsconfig.json'),
Nikolay Vitkov2a1b3b32025-01-03 10:18:46255 input_api.os_path.join(scripts_directory, 'test',
256 'run_lint_check.mjs'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06257 ]
258
Nikolay Vitkove2e44022025-04-01 17:29:41259 lint_related_directories = [
260 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
261 'eslint'),
262 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
263 'stylelint'),
264 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
265 '@typescript-eslint'),
266 input_api.os_path.join(scripts_directory, 'eslint_rules'),
267 ]
268
Alex Rudenkod723dba2025-04-29 08:35:58269 lint_config_files = _GetAffectedFiles(
Nikolay Vitkove2e44022025-04-01 17:29:41270 input_api, lint_related_directories,
Alex Rudenkod723dba2025-04-29 08:35:58271 [], [".js", ".mjs", ".ts"]) + _GetAffectedFiles(
Nikolay Vitkove2e44022025-04-01 17:29:41272 input_api, lint_related_files, [], [])
Tim van der Lippe2a4ae2b2020-03-11 17:28:06273
Alex Rudenkod723dba2025-04-29 08:35:58274 should_bail_out, files_to_lint = _GetFilesToLint(
275 input_api, lint_config_files, default_linted_directories,
276 ['.css', '.mjs', '.js', '.ts'])
Mathias Bynens0ec56612020-06-19 07:14:03277 if should_bail_out:
Alex Rudenkod723dba2025-04-29 08:35:58278 return []
Tim van der Lippe2a4ae2b2020-03-11 17:28:06279
Brandon Goddarde34e94f2021-04-12 17:58:26280 # If there are more than 50 files to check, don't bother and check
281 # everything, so as to not run into command line length limits on Windows.
282 if len(files_to_lint) > 50:
283 files_to_lint = []
284
Alex Rudenkod723dba2025-04-29 08:35:58285 results = []
Mathias Bynens1b2c5e42020-06-18 06:29:21286 results.extend(
Alex Rudenkod723dba2025-04-29 08:35:58287 _CheckWithNodeScript(input_api,
Nikolay Vitkovc9178752025-04-02 13:37:07288 output_api,
289 lint_path,
Alex Rudenkod723dba2025-04-29 08:35:58290 script_arguments=files_to_lint,
291 allow_typescript=True,
292 message="Lint"))
293
294 results.extend(_CheckFormat(input_api, output_api))
Tim van der Lippe98132242020-04-14 16:16:54295 return results
Blink Reformat4c46d092018-04-07 15:32:37296
Nikolay Vitkov55adf672025-01-02 12:07:33297
Alex Rudenkod723dba2025-04-29 08:35:58298def CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api):
Tim van der Lippea53672d2021-07-08 14:52:35299 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
300 'scripts', 'test',
301 'run_header_check_non_js_files.js')
Tim van der Lippe81752502021-05-26 14:38:12302
303 front_end_directory = input_api.os_path.join(
304 input_api.PresubmitLocalPath(), 'front_end')
305 inspector_overlay_directory = input_api.os_path.join(
306 input_api.PresubmitLocalPath(), 'inspector_overlay')
307 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
308 'test')
309 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
310 'scripts')
Tim van der Lippe8b929542021-05-26 14:54:20311 config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
312 'config')
Tim van der Lippe81752502021-05-26 14:38:12313
314 default_linted_directories = [
315 front_end_directory, test_directory, scripts_directory,
Tim van der Lippe8b929542021-05-26 14:54:20316 inspector_overlay_directory, config_directory
Tim van der Lippe81752502021-05-26 14:38:12317 ]
318
319 check_related_files = [lint_path]
320
Alex Rudenkod723dba2025-04-29 08:35:58321 lint_config_files = _GetAffectedFiles(input_api, check_related_files, [],
Tim van der Lippe81752502021-05-26 14:38:12322 ['.js'])
323
Alex Rudenkod723dba2025-04-29 08:35:58324 should_bail_out, files_to_lint = _GetFilesToLint(
325 input_api, lint_config_files, default_linted_directories,
326 ['BUILD.gn', '.gni', '.css'])
Tim van der Lippe81752502021-05-26 14:38:12327 if should_bail_out:
Alex Rudenkod723dba2025-04-29 08:35:58328 return []
Tim van der Lippe81752502021-05-26 14:38:12329
330 # If there are more than 50 files to check, don't bother and check
331 # everything, so as to not run into command line length limits on Windows.
332 if len(files_to_lint) > 50:
333 files_to_lint = []
334
Alex Rudenkod723dba2025-04-29 08:35:58335 return _CheckWithNodeScript(input_api,
336 output_api,
337 lint_path,
338 files_to_lint,
339 message='License headers')
Tim van der Lippe81752502021-05-26 14:38:12340
341
Alex Rudenkod723dba2025-04-29 08:35:58342def CheckGeneratedFiles(input_api, output_api):
Alex Rudenko537c6312024-07-19 06:22:05343 v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
344 'v8')
345 blink_directory_path = input_api.os_path.join(
346 input_api.PresubmitLocalPath(), 'third_party', 'blink')
Alex Rudenko537c6312024-07-19 06:22:05347 scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
348 'scripts', 'build')
349 scripts_generated_output_path = input_api.os_path.join(
350 input_api.PresubmitLocalPath(), 'front_end', 'generated')
Tim van der Lippeb3b90762020-03-04 15:21:52351
Alex Rudenko537c6312024-07-19 06:22:05352 generated_aria_path = input_api.os_path.join(scripts_build_path,
353 'generate_aria.py')
354 generated_supported_css_path = input_api.os_path.join(
355 scripts_build_path, 'generate_supported_css.py')
Simon ZĂ¼nd2ce67542023-02-07 10:15:14356 generated_deprecation_path = input_api.os_path.join(
357 scripts_build_path, 'generate_deprecations.py')
Alex Rudenko537c6312024-07-19 06:22:05358 generated_protocol_path = input_api.os_path.join(
359 scripts_build_path, 'code_generator_frontend.py')
Tim van der Lippe2a1eac22021-05-13 15:19:29360 generated_protocol_typescript_path = input_api.os_path.join(
361 input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript')
Alex Rudenko537c6312024-07-19 06:22:05362 concatenate_protocols_path = input_api.os_path.join(
363 input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol',
364 'concatenate_protocols.py')
Tim van der Lippeb3b90762020-03-04 15:21:52365
Alex Rudenkod723dba2025-04-29 08:35:58366 affected_files = _GetAffectedFiles(input_api, [
Tim van der Lippeb3b90762020-03-04 15:21:52367 v8_directory_path,
368 blink_directory_path,
Tim van der Lippe2a1eac22021-05-13 15:19:29369 input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party',
370 'pyjson5'),
Tim van der Lippeb3b90762020-03-04 15:21:52371 generated_aria_path,
372 generated_supported_css_path,
Simon ZĂ¼nd2ce67542023-02-07 10:15:14373 generated_deprecation_path,
Tim van der Lippeb3b90762020-03-04 15:21:52374 concatenate_protocols_path,
375 generated_protocol_path,
Tim van der Lippe5d2d79b2020-03-23 11:45:04376 scripts_generated_output_path,
Tim van der Lippe2a1eac22021-05-13 15:19:29377 generated_protocol_typescript_path,
378 ], [], ['.pdl', '.json5', '.py', '.js', '.ts'])
Tim van der Lippeb3b90762020-03-04 15:21:52379
380 if len(affected_files) == 0:
Alex Rudenkod723dba2025-04-29 08:35:58381 return []
Tim van der Lippeb3b90762020-03-04 15:21:52382
Alex Rudenko537c6312024-07-19 06:22:05383 generate_protocol_resources_path = input_api.os_path.join(
384 input_api.PresubmitLocalPath(), 'scripts', 'deps',
385 'generate_protocol_resources.py')
Tim van der Lippe4d004ec2020-03-03 18:32:01386
Alex Rudenkod723dba2025-04-29 08:35:58387 return _ExecuteSubProcess(input_api,
388 output_api,
389 generate_protocol_resources_path,
390 message='Generated files')
Tim van der Lippe4d004ec2020-03-03 18:32:01391
392
Alex Rudenkod723dba2025-04-29 08:35:58393def CheckL10nStrings(input_api, output_api):
Christy Chen2d6d9a62020-09-22 16:04:09394 devtools_root = input_api.PresubmitLocalPath()
395 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
Tim van der Lippe25f11082021-06-24 15:28:08396 script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n',
Simon ZĂ¼nd9ff4da62022-11-22 09:25:59397 'check-strings.js')
Alex Rudenkod723dba2025-04-29 08:35:58398 affected_front_end_files = _GetAffectedFiles(
Tim van der Lippe25f11082021-06-24 15:28:08399 input_api, [devtools_front_end, script_path], [], ['.js', '.ts'])
Christy Chen2d6d9a62020-09-22 16:04:09400 if len(affected_front_end_files) == 0:
Alex Rudenkod723dba2025-04-29 08:35:58401 return []
402 return _CheckWithNodeScript(input_api,
403 output_api,
404 script_path, [devtools_front_end],
405 message='l10n strings')
Christy Chen2d6d9a62020-09-22 16:04:09406
407
Alex Rudenkod723dba2025-04-29 08:35:58408def CheckForTooLargeFiles(input_api, output_api):
Christy Chen1ab87e02020-01-31 00:32:16409 """Avoid large files, especially binary files, in the repository since
Tim van der Lippe8fdda112020-01-27 11:27:06410 git doesn't scale well for those. They will be in everyone's repo
411 clones forever, forever making Chromium slower to clone and work
412 with."""
Christy Chen1ab87e02020-01-31 00:32:16413 # Uploading files to cloud storage is not trivial so we don't want
414 # to set the limit too low, but the upper limit for "normal" large
415 # files seems to be 1-2 MB, with a handful around 5-8 MB, so
416 # anything over 20 MB is exceptional.
417 TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
418 too_large_files = []
419 for f in input_api.AffectedFiles():
420 # Check both added and modified files (but not deleted files).
421 if f.Action() in ('A', 'M'):
422 size = input_api.os_path.getsize(f.AbsoluteLocalPath())
423 if size > TOO_LARGE_FILE_SIZE_LIMIT:
424 too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
425 if too_large_files:
426 message = (
Alex Rudenko537c6312024-07-19 06:22:05427 'Do not commit large files to git since git scales badly for those.\n'
428 +
429 'Instead put the large files in cloud storage and use DEPS to\n' +
430 'fetch them.\n' + '\n'.join(too_large_files))
431 return [
432 output_api.PresubmitError('Too large files found in commit',
433 long_text=message + '\n')
434 ]
Alex Rudenkod723dba2025-04-29 08:35:58435 return []
Tim van der Lippe8fdda112020-01-27 11:27:06436
Tim van der Lippe5279f842020-01-14 16:26:38437
Alex Rudenkod723dba2025-04-29 08:35:58438def CheckObsoleteScreenshotGoldens(input_api, output_api):
Alex Rudenkod723dba2025-04-29 08:35:58439 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
440 'scripts', 'test',
441 'check_obsolete_goldens.js')
442 return _CheckWithNodeScript(input_api,
443 output_api,
444 script_path,
445 script_arguments=[],
446 message='Obsolete screenshot images')
Andrés Olivares205bf682023-02-01 10:47:13447
448
Alex Rudenkod723dba2025-04-29 08:35:58449def CheckNodeModules(input_api, output_api):
Alex Rudenko4a7a3242024-04-18 10:36:50450 files = ['.clang-format', 'OWNERS', 'README.chromium']
Alex Rudenko4a7a3242024-04-18 10:36:50451 results = []
452 for file in files:
453 file_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
454 'node_modules', file)
455 if not Path(file_path).is_file():
Alex Rudenko537c6312024-07-19 06:22:05456 results.extend([
Alex Rudenko4a7a3242024-04-18 10:36:50457 output_api.PresubmitError(
458 "node_modules/%s is missing. Use npm run install-deps to re-create it."
Alex Rudenko537c6312024-07-19 06:22:05459 % file)
460 ])
Alex Rudenko537c6312024-07-19 06:22:05461 return results
Alex Rudenkod723dba2025-04-29 08:35:58462
463
464def CheckNoUncheckedFiles(input_api, output_api):
465 if _IsEnvCog(input_api):
466 return []
467
468 process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'],
469 stdout=input_api.subprocess.PIPE,
470 stderr=input_api.subprocess.STDOUT)
471 out, _ = process.communicate()
472 if process.returncode != 0:
473 files_changed_process = input_api.subprocess.Popen(
474 ['git', 'diff', '--name-only'],
475 stdout=input_api.subprocess.PIPE,
476 stderr=input_api.subprocess.STDOUT)
477 files_changed, _ = files_changed_process.communicate()
478
479 return [
480 output_api.PresubmitError(
481 'You have changed files that need to be committed:\n%s' %
482 (files_changed.decode('utf-8').strip()))
483 ]
484
485 return []
486
487
488# Canned check wrappers below.
489
Alex Rudenko27045282025-04-30 13:15:57490
491def _TextFilesOnlyFilter(file):
Benedikt Meurer13f52f22025-07-09 12:41:43492 """Filter that yields only text files.
493
494 Filters files based on prefixes and extensions that should not be treated as
495 text files for the canned checks below.
496 """
Alex Rudenko27045282025-04-30 13:15:57497 excluded_prefixes = [
498 'node_modules',
499 'third_party',
500 'front_end/third_party',
501 'extensions/cxx_debugging/third_party',
502 ]
503 excluded_extensions = [
504 '.png', '.webm', '.svg', '.avif', '.rawresponse', '.gz'
505 ]
506
507 if any(file.LocalPath().startswith(prefix)
508 for prefix in excluded_prefixes):
509 return False
510 if any(file.LocalPath().endswith(ext) for ext in excluded_extensions):
511 return False
512 return True
513
514
Alex Rudenko27045282025-04-30 13:15:57515def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api):
516 return input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
517 input_api, output_api, source_file_filter=_TextFilesOnlyFilter)
Alex Rudenkod723dba2025-04-29 08:35:58518
519
Alex Rudenkod723dba2025-04-29 08:35:58520def CheckGenderNeutral(input_api, output_api):
521 return input_api.canned_checks.CheckGenderNeutral(input_api, output_api)
522
523
Alex Rudenkod723dba2025-04-29 08:35:58524
525def CheckAuthorizedAuthor(input_api, output_api):
526 return input_api.canned_checks.CheckAuthorizedAuthor(
527 input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT])
Benedikt Meurer13f52f22025-07-09 12:41:43528
529
530def CheckPanProjectChecksOnCommit(input_api, output_api):
531 return input_api.canned_checks.PanProjectChecks(input_api,
532 output_api,
533 maxlen=120)