Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [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 08:29:25 +0100 | [diff] [blame] | 28 | """ |
| 29 | DevTools presubmit script |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 30 | |
| 31 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 32 | for more details about the presubmit API built into gcl. |
| 33 | """ |
| 34 | |
| 35 | import sys |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 +0000 | [diff] [blame] | 36 | import six |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 37 | |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 38 | EXCLUSIVE_CHANGE_DIRECTORIES = [ |
| 39 | [ 'third_party', 'v8' ], |
| 40 | [ 'node_modules' ], |
| 41 | [ 'OWNERS' ], |
| 42 | ] |
| 43 | |
Liviu Rau | fd2e321 | 2019-12-18 16:38:20 +0100 | [diff] [blame] | 44 | AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com" |
Mathias Bynens | a0a6e29 | 2019-12-17 13:24:08 +0100 | [diff] [blame] | 45 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 46 | |
| 47 | def _ExecuteSubProcess(input_api, output_api, script_path, args, results): |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 +0000 | [diff] [blame] | 48 | if isinstance(script_path, six.string_types): |
| 49 | script_path = [input_api.python_executable, script_path] |
| 50 | |
| 51 | process = input_api.subprocess.Popen(script_path + args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 52 | out, _ = process.communicate() |
| 53 | if process.returncode != 0: |
| 54 | results.append(output_api.PresubmitError(out)) |
| 55 | else: |
| 56 | results.append(output_api.PresubmitNotifyResult(out)) |
| 57 | return results |
| 58 | |
| 59 | |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 60 | def _CheckChangesAreExclusiveToDirectory(input_api, output_api): |
Tim van der Lippe | bc42a63 | 2019-11-28 14:22:55 +0000 | [diff] [blame] | 61 | if input_api.change.DISABLE_THIRD_PARTY_CHECK != None: |
| 62 | return [] |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 63 | results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')] |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 64 | def IsParentDir(file, dir): |
| 65 | while file != '': |
| 66 | if file == dir: |
| 67 | return True |
| 68 | file = input_api.os_path.dirname(file) |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 69 | return False |
| 70 | |
| 71 | def FileIsInDir(file, dirs): |
| 72 | for dir in dirs: |
| 73 | if IsParentDir(file, dir): |
| 74 | return True |
| 75 | |
| 76 | affected_files = input_api.LocalPaths() |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 77 | num_affected = len(affected_files) |
| 78 | for dirs in EXCLUSIVE_CHANGE_DIRECTORIES: |
Paul Lewis | 14effba | 2019-12-02 14:56:40 +0000 | [diff] [blame] | 79 | dir_list = ', '.join(dirs) |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 80 | affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files) |
| 81 | num_in_dir = len(affected_in_dir) |
| 82 | if num_in_dir == 0: |
| 83 | continue |
Tim van der Lippe | ebb94a9 | 2019-11-19 17:07:53 +0000 | [diff] [blame] | 84 | # Addition of new third_party folders must have a new entry in `.gitignore` |
| 85 | if '.gitignore' in affected_files: |
| 86 | num_in_dir = num_in_dir + 1 |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 87 | if num_in_dir < num_affected: |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 88 | results.append(output_api |
Paul Lewis | 14effba | 2019-12-02 14:56:40 +0000 | [diff] [blame] | 89 | .PresubmitError(('CLs that affect files in "%s" should be limited to these files/directories.' % dir_list) + |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 90 | ' You can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message')) |
| 91 | break |
| 92 | |
| 93 | return results |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 94 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 95 | |
| 96 | def _CheckBuildGN(input_api, output_api): |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 97 | results = [output_api.PresubmitNotifyResult('Running BUILD.GN check:')] |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 98 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js') |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 99 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 100 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 101 | |
| 102 | |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 103 | def _CheckExperimentTelemetry(input_api, output_api): |
| 104 | results = [ |
| 105 | output_api.PresubmitNotifyResult('Running Experiment Telemetry check:') |
| 106 | ] |
| 107 | |
| 108 | experiment_telemetry_files = [ |
| 109 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
| 110 | 'main', 'MainImpl.js'), |
| 111 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
| 112 | 'host', 'UserMetrics.js') |
| 113 | ] |
| 114 | affected_main_files = _getAffectedFiles(input_api, |
| 115 | experiment_telemetry_files, [], |
| 116 | ['.js']) |
| 117 | if len(affected_main_files) == 0: |
| 118 | return results |
| 119 | |
| 120 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 121 | 'scripts', 'check_experiments.js') |
| 122 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 123 | return results |
| 124 | |
| 125 | |
Tim van der Lippe | e4bdd74 | 2019-12-17 16:40:16 +0100 | [diff] [blame] | 126 | def _CheckJSON(input_api, output_api): |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 127 | results = [output_api.PresubmitNotifyResult('Running JSON Validator:')] |
Tim van der Lippe | e4bdd74 | 2019-12-17 16:40:16 +0100 | [diff] [blame] | 128 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'json_validator', 'validate_module_json.js') |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 129 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 130 | return results |
Tim van der Lippe | e4bdd74 | 2019-12-17 16:40:16 +0100 | [diff] [blame] | 131 | |
| 132 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 133 | def _CheckFormat(input_api, output_api): |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 134 | node_modules_affected_files = _getAffectedFiles(input_api, [input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules')], [], []) |
| 135 | |
| 136 | # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs. |
| 137 | if (len(node_modules_affected_files) > 0): |
| 138 | return [output_api.PresubmitNotifyResult('Skipping Format Checks because `node_modules` files are affected.')] |
| 139 | |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 140 | results = [output_api.PresubmitNotifyResult('Running Format Checks:')] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 141 | |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 +0000 | [diff] [blame] | 142 | return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 143 | |
| 144 | |
e52a82bdfb5106bd658c2c5ea465e200 | 594be1e | 2019-10-29 16:02:46 -0700 | [diff] [blame] | 145 | def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 146 | results = [output_api.PresubmitNotifyResult('Running Localization Checks:')] |
Mandy Chen | e997da7 | 2019-08-22 23:50:19 +0000 | [diff] [blame] | 147 | devtools_root = input_api.PresubmitLocalPath() |
vidorteg | 2b675b0 | 2019-11-25 09:51:28 -0800 | [diff] [blame] | 148 | script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', 'run_localization_check.py') |
Paul Lewis | 954a5a9 | 2019-11-20 15:33:49 +0000 | [diff] [blame] | 149 | if check_all_files == True: |
vidorteg | 2b675b0 | 2019-11-25 09:51:28 -0800 | [diff] [blame] | 150 | # Scan all files and fix any errors |
vidorteg | 75c025e | 2019-11-25 09:52:43 -0800 | [diff] [blame] | 151 | args = ['--autofix', '--all'] |
Paul Lewis | 954a5a9 | 2019-11-20 15:33:49 +0000 | [diff] [blame] | 152 | else: |
vidorteg | 2b675b0 | 2019-11-25 09:51:28 -0800 | [diff] [blame] | 153 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
| 154 | affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], |
| 155 | ['.js', '.grdp', '.grd', 'module.json']) |
| 156 | |
| 157 | if len(affected_front_end_files) == 0: |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 158 | return results |
Christy Chen | 1ab87e0 | 2020-01-30 16:32:16 -0800 | [diff] [blame] | 159 | |
| 160 | with input_api.CreateTemporaryFile() as file_list: |
| 161 | for affected_file in affected_front_end_files: |
| 162 | file_list.write(affected_file + '\n') |
| 163 | file_list.close() |
| 164 | |
vidorteg | 2b675b0 | 2019-11-25 09:51:28 -0800 | [diff] [blame] | 165 | # Scan only added or modified files with specific extensions. |
Christy Chen | 1ab87e0 | 2020-01-30 16:32:16 -0800 | [diff] [blame] | 166 | args = ['--autofix', '--file-list', file_list.name] |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 167 | |
| 168 | return _ExecuteSubProcess(input_api, output_api, script_path, args, results) |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 169 | |
| 170 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 171 | def _CheckDevToolsStyleJS(input_api, output_api): |
| 172 | results = [ |
| 173 | output_api.PresubmitNotifyResult('Running DevTools JS style check:') |
| 174 | ] |
| 175 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 176 | 'scripts', 'test', |
| 177 | 'run_lint_check_js.js') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 178 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 179 | front_end_directory = input_api.os_path.join( |
| 180 | input_api.PresubmitLocalPath(), 'front_end') |
| 181 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 182 | 'test') |
| 183 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 184 | 'scripts') |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 185 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 186 | default_linted_directories = [ |
| 187 | front_end_directory, test_directory, scripts_directory |
| 188 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 189 | |
| 190 | eslint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 191 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 192 | 'eslint'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 193 | input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 194 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 195 | '.eslintignore'), |
| 196 | input_api.os_path.join(scripts_directory, 'test', |
| 197 | 'run_lint_check_js.py'), |
| 198 | input_api.os_path.join(scripts_directory, 'test', |
| 199 | 'run_lint_check_js.js'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 200 | input_api.os_path.join(scripts_directory, '.eslintrc.js'), |
| 201 | input_api.os_path.join(scripts_directory, 'eslint_rules'), |
| 202 | ] |
| 203 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 204 | lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [], |
| 205 | ['.js', '.py', '.eslintignore']) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 206 | |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 207 | should_bail_out, files_to_lint = _getFilesToLint( |
| 208 | input_api, output_api, lint_config_files, default_linted_directories, |
| 209 | ['.js', '.ts'], results) |
| 210 | if should_bail_out: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 211 | return results |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 212 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 213 | results.extend( |
| 214 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
Tim van der Lippe | 9813224 | 2020-04-14 17:16:54 +0100 | [diff] [blame] | 215 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 216 | |
| 217 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 218 | def _CheckDevToolsStyleCSS(input_api, output_api): |
| 219 | results = [ |
| 220 | output_api.PresubmitNotifyResult('Running DevTools CSS style check:') |
| 221 | ] |
| 222 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 223 | 'scripts', 'test', |
| 224 | 'run_lint_check_css.py') |
| 225 | |
| 226 | front_end_directory = input_api.os_path.join( |
| 227 | input_api.PresubmitLocalPath(), 'front_end') |
| 228 | default_linted_directories = [front_end_directory] |
| 229 | |
| 230 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 231 | 'scripts') |
| 232 | |
| 233 | stylelint_related_files = [ |
| 234 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 235 | 'stylelint'), |
| 236 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 237 | '.stylelintrc.json'), |
| 238 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 239 | '.stylelintignore'), |
| 240 | input_api.os_path.join(scripts_directory, 'test', |
| 241 | 'run_lint_check_css.py'), |
| 242 | ] |
| 243 | |
| 244 | lint_config_files = _getAffectedFiles(input_api, stylelint_related_files, |
| 245 | [], |
| 246 | ['.json', '.py', '.stylelintignore']) |
| 247 | |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 248 | should_bail_out, files_to_lint = _getFilesToLint( |
| 249 | input_api, output_api, lint_config_files, default_linted_directories, |
| 250 | ['.css'], results) |
| 251 | if should_bail_out: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 252 | return results |
| 253 | |
| 254 | return _ExecuteSubProcess(input_api, output_api, lint_path, files_to_lint, |
| 255 | results) |
| 256 | |
| 257 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 +0000 | [diff] [blame] | 258 | def _CheckOptimizeSVGHashes(input_api, output_api): |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 259 | results = [ |
| 260 | output_api.PresubmitNotifyResult('Running SVG optimization check:') |
| 261 | ] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 262 | if not input_api.platform.startswith('linux'): |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 263 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 264 | |
| 265 | original_sys_path = sys.path |
| 266 | try: |
| 267 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')] |
| 268 | import devtools_file_hashes |
| 269 | finally: |
| 270 | sys.path = original_sys_path |
| 271 | |
| 272 | absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)] |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 273 | images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src') |
| 274 | image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')] |
| 275 | image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src') |
| 276 | hashes_file_name = 'optimize_svg.hashes' |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 277 | hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name) |
| 278 | invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths) |
| 279 | if len(invalid_hash_file_paths) == 0: |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 280 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 281 | invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths] |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 282 | file_paths_str = ', '.join(invalid_hash_file_names) |
| 283 | error_message = 'The following SVG files should be optimized using optimize_svg_images script before uploading: \n - %s' % file_paths_str |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 284 | results.append(output_api.PresubmitError(error_message)) |
| 285 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 286 | |
| 287 | |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 288 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 289 | def _CheckGeneratedFiles(input_api, output_api): |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 290 | v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8') |
| 291 | blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink') |
| 292 | protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol') |
| 293 | scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build') |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 +0000 | [diff] [blame] | 294 | scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 295 | |
| 296 | generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py') |
| 297 | generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py') |
| 298 | generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py') |
| 299 | concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', |
| 300 | 'concatenate_protocols.py') |
| 301 | |
| 302 | affected_files = _getAffectedFiles(input_api, [ |
| 303 | v8_directory_path, |
| 304 | blink_directory_path, |
| 305 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'pyjson5'), |
| 306 | generated_aria_path, |
| 307 | generated_supported_css_path, |
| 308 | concatenate_protocols_path, |
| 309 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 +0000 | [diff] [blame] | 310 | scripts_generated_output_path, |
| 311 | ], [], ['.pdl', '.json5', '.py', '.js']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 312 | |
| 313 | if len(affected_files) == 0: |
| 314 | return [] |
| 315 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 316 | results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')] |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 +0000 | [diff] [blame] | 317 | generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 318 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 319 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 +0000 | [diff] [blame] | 320 | return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 321 | |
| 322 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 +0000 | [diff] [blame] | 323 | def _CheckNoUncheckedFiles(input_api, output_api): |
| 324 | results = [] |
| 325 | process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'], |
| 326 | stdout=input_api.subprocess.PIPE, |
| 327 | stderr=input_api.subprocess.STDOUT) |
| 328 | out, _ = process.communicate() |
| 329 | if process.returncode != 0: |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 +0000 | [diff] [blame] | 330 | files_changed_process = input_api.subprocess.Popen(['git', 'diff', '--name-only'], |
| 331 | stdout=input_api.subprocess.PIPE, |
| 332 | stderr=input_api.subprocess.STDOUT) |
| 333 | files_changed, _ = files_changed_process.communicate() |
| 334 | |
| 335 | return [ |
| 336 | output_api.PresubmitError('You have changed files that need to be committed:'), |
| 337 | output_api.PresubmitError(files_changed) |
| 338 | ] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 +0000 | [diff] [blame] | 339 | return [] |
| 340 | |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 +0000 | [diff] [blame] | 341 | def _CheckForTooLargeFiles(input_api, output_api): |
Christy Chen | 1ab87e0 | 2020-01-30 16:32:16 -0800 | [diff] [blame] | 342 | """Avoid large files, especially binary files, in the repository since |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 +0000 | [diff] [blame] | 343 | git doesn't scale well for those. They will be in everyone's repo |
| 344 | clones forever, forever making Chromium slower to clone and work |
| 345 | with.""" |
Christy Chen | 1ab87e0 | 2020-01-30 16:32:16 -0800 | [diff] [blame] | 346 | # Uploading files to cloud storage is not trivial so we don't want |
| 347 | # to set the limit too low, but the upper limit for "normal" large |
| 348 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 349 | # anything over 20 MB is exceptional. |
| 350 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB |
| 351 | too_large_files = [] |
| 352 | for f in input_api.AffectedFiles(): |
| 353 | # Check both added and modified files (but not deleted files). |
| 354 | if f.Action() in ('A', 'M'): |
| 355 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
| 356 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
| 357 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
| 358 | if too_large_files: |
| 359 | message = ( |
| 360 | 'Do not commit large files to git since git scales badly for those.\n' + |
| 361 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 362 | 'fetch them.\n' + '\n'.join(too_large_files) |
| 363 | ) |
| 364 | return [output_api.PresubmitError( |
| 365 | 'Too large files found in commit', long_text=message + '\n')] |
| 366 | else: |
| 367 | return [] |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 +0000 | [diff] [blame] | 368 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 +0000 | [diff] [blame] | 369 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 370 | def _CommonChecks(input_api, output_api): |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 371 | """Checks common to both upload and commit.""" |
| 372 | results = [] |
Mathias Bynens | 011b007 | 2020-08-05 10:17:35 +0200 | [diff] [blame] | 373 | results.extend( |
| 374 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 375 | input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT])) |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 376 | results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api)) |
| 377 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
| 378 | results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api)) |
| 379 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api)) |
| 380 | results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 381 | results.extend(_CheckBuildGN(input_api, output_api)) |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 382 | results.extend(_CheckExperimentTelemetry(input_api, output_api)) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 383 | results.extend(_CheckGeneratedFiles(input_api, output_api)) |
Tim van der Lippe | e4bdd74 | 2019-12-17 16:40:16 +0100 | [diff] [blame] | 384 | results.extend(_CheckJSON(input_api, output_api)) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 385 | results.extend(_CheckDevToolsStyleJS(input_api, output_api)) |
| 386 | results.extend(_CheckDevToolsStyleCSS(input_api, output_api)) |
Tim van der Lippe | 5497d48 | 2020-01-14 15:27:30 +0000 | [diff] [blame] | 387 | results.extend(_CheckFormat(input_api, output_api)) |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 +0000 | [diff] [blame] | 388 | results.extend(_CheckOptimizeSVGHashes(input_api, output_api)) |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 389 | results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api)) |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 +0000 | [diff] [blame] | 390 | results.extend(_CheckNoUncheckedFiles(input_api, output_api)) |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 +0000 | [diff] [blame] | 391 | results.extend(_CheckForTooLargeFiles(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 392 | return results |
| 393 | |
| 394 | |
Liviu Rau | d614e09 | 2020-01-08 10:56:33 +0100 | [diff] [blame] | 395 | def CheckChangeOnUpload(input_api, output_api): |
| 396 | results = [] |
| 397 | results.extend(_CommonChecks(input_api, output_api)) |
| 398 | results.extend(_CheckDevtoolsLocalization(input_api, output_api)) |
| 399 | return results |
| 400 | |
| 401 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 402 | def CheckChangeOnCommit(input_api, output_api): |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 403 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 404 | results.extend(_CommonChecks(input_api, output_api)) |
e52a82bdfb5106bd658c2c5ea465e200 | 594be1e | 2019-10-29 16:02:46 -0700 | [diff] [blame] | 405 | results.extend(_CheckDevtoolsLocalization(input_api, output_api, True)) |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 406 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 407 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 408 | |
| 409 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 410 | def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 411 | """Return absolute file paths of affected files (not due to an excluded action) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 412 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 413 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 414 | local_paths = [ |
| 415 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions) |
| 416 | ] |
| 417 | affected_files = [ |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 418 | file_name for file_name in local_paths if any(parent_directory in file_name for parent_directory in parent_directories) and |
| 419 | (len(accepted_endings) is 0 or any(file_name.endswith(accepted_ending) for accepted_ending in accepted_endings)) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 420 | ] |
| 421 | return affected_files |
| 422 | |
| 423 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 +0000 | [diff] [blame] | 424 | def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 425 | original_sys_path = sys.path |
| 426 | try: |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 427 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')] |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 428 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 429 | finally: |
| 430 | sys.path = original_sys_path |
| 431 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 +0000 | [diff] [blame] | 432 | return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 433 | |
| 434 | |
| 435 | def _getFilesToLint(input_api, output_api, lint_config_files, |
| 436 | default_linted_directories, accepted_endings, results): |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 437 | run_full_check = False |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 438 | files_to_lint = [] |
| 439 | |
| 440 | # We are changing the lint configuration; run the full check. |
| 441 | if len(lint_config_files) is not 0: |
| 442 | results.append( |
| 443 | output_api.PresubmitNotifyResult('Running full lint check')) |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 444 | run_full_check = True |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 445 | else: |
| 446 | # Only run the linter on files that are relevant, to save PRESUBMIT time. |
| 447 | files_to_lint = _getAffectedFiles(input_api, |
| 448 | default_linted_directories, ['D'], |
| 449 | accepted_endings) |
| 450 | |
| 451 | if len(files_to_lint) is 0: |
| 452 | results.append( |
| 453 | output_api.PresubmitNotifyResult( |
| 454 | 'No affected files for lint check')) |
| 455 | |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 456 | should_bail_out = len(files_to_lint) is 0 and not run_full_check |
| 457 | return should_bail_out, files_to_lint |