Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | # 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 Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 28 | """ |
| 29 | DevTools presubmit script |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | |
Benedikt Meurer | bc9da61 | 2024-08-19 10:44:49 | [diff] [blame] | 31 | See http://goo.gle/devtools-testing-guide#Presubmit-checks for more how to |
| 32 | run presubmit checks in DevTools. |
| 33 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 35 | for more details about the presubmit API built into gcl. |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 36 | |
| 37 | `git cl presubmit -v -v` to debug presubmit checks. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | """ |
| 39 | |
| 40 | import sys |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 41 | import six |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 42 | import time |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 43 | |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame] | 44 | from pathlib import Path |
| 45 | |
Liviu Rau | fd2e321 | 2019-12-18 15:38:20 | [diff] [blame] | 46 | AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com" |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 47 | USE_PYTHON3 = True |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 48 | PRESUBMIT_VERSION = '2.0.0' |
Mathias Bynens | a0a6e29 | 2019-12-17 12:24:08 | [diff] [blame] | 49 | |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 50 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 51 | def _ExecuteSubProcess(input_api, |
| 52 | output_api, |
| 53 | script_path, |
Benedikt Meurer | 21ef4f1 | 2025-06-12 09:46:51 | [diff] [blame] | 54 | script_arguments=None, |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 55 | message=None): |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 56 | if isinstance(script_path, six.string_types): |
Philip Pfaffe | f4320aa | 2022-07-21 11:33:24 | [diff] [blame] | 57 | script_path = [input_api.python3_executable, script_path] |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 58 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 59 | start_time = time.time() |
Benedikt Meurer | 21ef4f1 | 2025-06-12 09:46:51 | [diff] [blame] | 60 | process = input_api.subprocess.Popen(script_path + |
| 61 | (script_arguments or []), |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 62 | stdout=input_api.subprocess.PIPE, |
| 63 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 64 | out, _ = process.communicate() |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 65 | end_time = time.time() |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 66 | time_difference = end_time - start_time |
Benedikt Meurer | 21ef4f1 | 2025-06-12 09:46:51 | [diff] [blame] | 67 | 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 Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 73 | |
| 74 | |
Gavin Mak | 4a41e48 | 2024-07-31 17:16:44 | [diff] [blame] | 75 | def _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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 90 | def _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 | |
| 110 | def _CheckWithNodeScript(input_api, |
| 111 | output_api, |
| 112 | script_path, |
Benedikt Meurer | 21ef4f1 | 2025-06-12 09:46:51 | [diff] [blame] | 113 | script_arguments=None, |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 114 | 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 | |
| 138 | def _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 | |
| 163 | def _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 Meurer | 21ef4f1 | 2025-06-12 09:46:51 | [diff] [blame] | 170 | return _ExecuteSubProcess(input_api, |
| 171 | output_api, ['git', 'cl', 'format', '--js'], |
| 172 | message='Format') |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def CheckBugAssociationOnCommit(input_api, output_api): |
| 176 | results = [] |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 177 | 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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 188 | results.append(output_api.PresubmitError(message)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 189 | |
| 190 | return results |
| 191 | |
| 192 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 193 | def CheckExperimentTelemetry(input_api, output_api): |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 194 | experiment_telemetry_files = [ |
| 195 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 196 | 'entrypoints', 'main', 'MainImpl.ts'), |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 197 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Tim van der Lippe | e024731 | 2021-04-01 14:25:30 | [diff] [blame] | 198 | 'core', 'host', 'UserMetrics.ts') |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 199 | ] |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 200 | affected_main_files = _GetAffectedFiles(input_api, |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 201 | experiment_telemetry_files, [], |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 202 | ['.ts']) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 203 | if len(affected_main_files) == 0: |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 204 | return [] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 205 | |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 206 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 207 | 'scripts', 'check_experiments.js') |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 208 | return _CheckWithNodeScript(input_api, |
| 209 | output_api, |
| 210 | script_path, |
| 211 | message='Experiment telemetry') |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 212 | |
| 213 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 214 | def CheckESBuildVersion(input_api, output_api): |
Jack Franklin | b5a6309 | 2022-11-30 14:32:36 | [diff] [blame] | 215 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 216 | 'scripts', |
| 217 | 'check_esbuild_versions.js') |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 218 | return _CheckWithNodeScript(input_api, |
| 219 | output_api, |
| 220 | script_path, |
| 221 | message='ESBuild version') |
Jack Franklin | b5a6309 | 2022-11-30 14:32:36 | [diff] [blame] | 222 | |
| 223 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 224 | def CheckDevToolsLint(input_api, output_api): |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 225 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
Nikolay Vitkov | 2a1b3b3 | 2025-01-03 10:18:46 | [diff] [blame] | 226 | 'scripts', 'test', 'run_lint_check.mjs') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 227 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 228 | front_end_directory = input_api.os_path.join( |
| 229 | input_api.PresubmitLocalPath(), 'front_end') |
Nikolay Vitkov | 77ba8df | 2025-03-27 15:06:08 | [diff] [blame] | 230 | |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 231 | inspector_overlay_directory = input_api.os_path.join( |
| 232 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 233 | 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 Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 237 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 238 | default_linted_directories = [ |
Benedikt Meurer | 6dd23b6 | 2024-08-20 12:08:43 | [diff] [blame] | 239 | front_end_directory, |
| 240 | test_directory, |
| 241 | scripts_directory, |
| 242 | inspector_overlay_directory, |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 243 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 244 | |
Benedikt Meurer | 6dd23b6 | 2024-08-20 12:08:43 | [diff] [blame] | 245 | lint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 246 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 247 | 'eslint.config.mjs'), |
Benedikt Meurer | 6dd23b6 | 2024-08-20 12:08:43 | [diff] [blame] | 248 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 249 | '.stylelintrc.json'), |
| 250 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 251 | '.stylelintignore'), |
Nikolay Vitkov | 5b2bcff | 2025-01-29 19:21:12 | [diff] [blame] | 252 | # This file includes the LitAnalyzer rules |
| 253 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 254 | 'tsconfig.json'), |
Nikolay Vitkov | 2a1b3b3 | 2025-01-03 10:18:46 | [diff] [blame] | 255 | input_api.os_path.join(scripts_directory, 'test', |
| 256 | 'run_lint_check.mjs'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 257 | ] |
| 258 | |
Nikolay Vitkov | e2e4402 | 2025-04-01 17:29:41 | [diff] [blame] | 259 | 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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 269 | lint_config_files = _GetAffectedFiles( |
Nikolay Vitkov | e2e4402 | 2025-04-01 17:29:41 | [diff] [blame] | 270 | input_api, lint_related_directories, |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 271 | [], [".js", ".mjs", ".ts"]) + _GetAffectedFiles( |
Nikolay Vitkov | e2e4402 | 2025-04-01 17:29:41 | [diff] [blame] | 272 | input_api, lint_related_files, [], []) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 273 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 274 | should_bail_out, files_to_lint = _GetFilesToLint( |
| 275 | input_api, lint_config_files, default_linted_directories, |
| 276 | ['.css', '.mjs', '.js', '.ts']) |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 277 | if should_bail_out: |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 278 | return [] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 279 | |
Brandon Goddard | e34e94f | 2021-04-12 17:58:26 | [diff] [blame] | 280 | # 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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 285 | results = [] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 286 | results.extend( |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 287 | _CheckWithNodeScript(input_api, |
Nikolay Vitkov | c917875 | 2025-04-02 13:37:07 | [diff] [blame] | 288 | output_api, |
| 289 | lint_path, |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 290 | script_arguments=files_to_lint, |
| 291 | allow_typescript=True, |
| 292 | message="Lint")) |
| 293 | |
| 294 | results.extend(_CheckFormat(input_api, output_api)) |
Tim van der Lippe | 9813224 | 2020-04-14 16:16:54 | [diff] [blame] | 295 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | |
Nikolay Vitkov | 55adf67 | 2025-01-02 12:07:33 | [diff] [blame] | 297 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 298 | def CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api): |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 299 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 300 | 'scripts', 'test', |
| 301 | 'run_header_check_non_js_files.js') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 302 | |
| 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 Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 311 | config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 312 | 'config') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 313 | |
| 314 | default_linted_directories = [ |
| 315 | front_end_directory, test_directory, scripts_directory, |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 316 | inspector_overlay_directory, config_directory |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 317 | ] |
| 318 | |
| 319 | check_related_files = [lint_path] |
| 320 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 321 | lint_config_files = _GetAffectedFiles(input_api, check_related_files, [], |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 322 | ['.js']) |
| 323 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 324 | should_bail_out, files_to_lint = _GetFilesToLint( |
| 325 | input_api, lint_config_files, default_linted_directories, |
| 326 | ['BUILD.gn', '.gni', '.css']) |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 327 | if should_bail_out: |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 328 | return [] |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 329 | |
| 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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 335 | return _CheckWithNodeScript(input_api, |
| 336 | output_api, |
| 337 | lint_path, |
| 338 | files_to_lint, |
| 339 | message='License headers') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 340 | |
| 341 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 342 | def CheckGeneratedFiles(input_api, output_api): |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 343 | 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 Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 347 | 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 Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 351 | |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 352 | 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Ă¼nd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 356 | generated_deprecation_path = input_api.os_path.join( |
| 357 | scripts_build_path, 'generate_deprecations.py') |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 358 | generated_protocol_path = input_api.os_path.join( |
| 359 | scripts_build_path, 'code_generator_frontend.py') |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 360 | generated_protocol_typescript_path = input_api.os_path.join( |
| 361 | input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 362 | concatenate_protocols_path = input_api.os_path.join( |
| 363 | input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', |
| 364 | 'concatenate_protocols.py') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 365 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 366 | affected_files = _GetAffectedFiles(input_api, [ |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 367 | v8_directory_path, |
| 368 | blink_directory_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 369 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', |
| 370 | 'pyjson5'), |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 371 | generated_aria_path, |
| 372 | generated_supported_css_path, |
Simon ZĂ¼nd | 2ce6754 | 2023-02-07 10:15:14 | [diff] [blame] | 373 | generated_deprecation_path, |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 374 | concatenate_protocols_path, |
| 375 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 376 | scripts_generated_output_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 377 | generated_protocol_typescript_path, |
| 378 | ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 379 | |
| 380 | if len(affected_files) == 0: |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 381 | return [] |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 382 | |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 383 | generate_protocol_resources_path = input_api.os_path.join( |
| 384 | input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 385 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 386 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 387 | return _ExecuteSubProcess(input_api, |
| 388 | output_api, |
| 389 | generate_protocol_resources_path, |
| 390 | message='Generated files') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 391 | |
| 392 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 393 | def CheckL10nStrings(input_api, output_api): |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 394 | devtools_root = input_api.PresubmitLocalPath() |
| 395 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 396 | script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', |
Simon ZĂ¼nd | 9ff4da6 | 2022-11-22 09:25:59 | [diff] [blame] | 397 | 'check-strings.js') |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 398 | affected_front_end_files = _GetAffectedFiles( |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 399 | input_api, [devtools_front_end, script_path], [], ['.js', '.ts']) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 400 | if len(affected_front_end_files) == 0: |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 401 | return [] |
| 402 | return _CheckWithNodeScript(input_api, |
| 403 | output_api, |
| 404 | script_path, [devtools_front_end], |
| 405 | message='l10n strings') |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 406 | |
| 407 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 408 | def CheckForTooLargeFiles(input_api, output_api): |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 409 | """Avoid large files, especially binary files, in the repository since |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 410 | 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 Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 413 | # 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 Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 427 | '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 Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 435 | return [] |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 436 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 437 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 438 | def CheckObsoleteScreenshotGoldens(input_api, output_api): |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 439 | 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 Olivares | 205bf68 | 2023-02-01 10:47:13 | [diff] [blame] | 447 | |
| 448 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 449 | def CheckNodeModules(input_api, output_api): |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame] | 450 | files = ['.clang-format', 'OWNERS', 'README.chromium'] |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame] | 451 | 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 Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 456 | results.extend([ |
Alex Rudenko | 4a7a324 | 2024-04-18 10:36:50 | [diff] [blame] | 457 | output_api.PresubmitError( |
| 458 | "node_modules/%s is missing. Use npm run install-deps to re-create it." |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 459 | % file) |
| 460 | ]) |
Alex Rudenko | 537c631 | 2024-07-19 06:22:05 | [diff] [blame] | 461 | return results |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 462 | |
| 463 | |
| 464 | def 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 Rudenko | 2704528 | 2025-04-30 13:15:57 | [diff] [blame] | 490 | |
| 491 | def _TextFilesOnlyFilter(file): |
Benedikt Meurer | 13f52f2 | 2025-07-09 12:41:43 | [diff] [blame] | 492 | """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 Rudenko | 2704528 | 2025-04-30 13:15:57 | [diff] [blame] | 497 | 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 Rudenko | 2704528 | 2025-04-30 13:15:57 | [diff] [blame] | 515 | def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api): |
| 516 | return input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 517 | input_api, output_api, source_file_filter=_TextFilesOnlyFilter) |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 518 | |
| 519 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 520 | def CheckGenderNeutral(input_api, output_api): |
| 521 | return input_api.canned_checks.CheckGenderNeutral(input_api, output_api) |
| 522 | |
| 523 | |
Alex Rudenko | d723dba | 2025-04-29 08:35:58 | [diff] [blame] | 524 | |
| 525 | def CheckAuthorizedAuthor(input_api, output_api): |
| 526 | return input_api.canned_checks.CheckAuthorizedAuthor( |
| 527 | input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT]) |
Benedikt Meurer | 13f52f2 | 2025-07-09 12:41:43 | [diff] [blame] | 528 | |
| 529 | |
| 530 | def CheckPanProjectChecksOnCommit(input_api, output_api): |
| 531 | return input_api.canned_checks.PanProjectChecks(input_api, |
| 532 | output_api, |
| 533 | maxlen=120) |