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 | |
| 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 | [diff] [blame] | 36 | import six |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 37 | import time |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | |
Liviu Rau | fd2e321 | 2019-12-18 15:38:20 | [diff] [blame] | 39 | 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] | 40 | USE_PYTHON3 = True |
Mathias Bynens | a0a6e29 | 2019-12-17 12:24:08 | [diff] [blame] | 41 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 42 | |
| 43 | def _ExecuteSubProcess(input_api, output_api, script_path, args, results): |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 44 | if isinstance(script_path, six.string_types): |
| 45 | script_path = [input_api.python_executable, script_path] |
| 46 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 47 | start_time = time.time() |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 48 | process = input_api.subprocess.Popen(script_path + args, |
| 49 | stdout=input_api.subprocess.PIPE, |
| 50 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 51 | out, _ = process.communicate() |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 52 | end_time = time.time() |
| 53 | |
| 54 | time_difference = end_time - start_time |
| 55 | time_info = "Script execution time was %.1fs seconds\n" % (time_difference) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 56 | if process.returncode != 0: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 57 | results.append( |
| 58 | output_api.PresubmitError(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 59 | else: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 60 | results.append( |
| 61 | output_api.PresubmitNotifyResult(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 62 | return results |
| 63 | |
| 64 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 65 | def _CheckChangesAreExclusiveToDirectory(input_api, output_api): |
Tim van der Lippe | bc42a63 | 2019-11-28 14:22:55 | [diff] [blame] | 66 | if input_api.change.DISABLE_THIRD_PARTY_CHECK != None: |
| 67 | return [] |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 68 | results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')] |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 69 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 70 | def IsParentDir(file, dir): |
| 71 | while file != '': |
| 72 | if file == dir: |
| 73 | return True |
| 74 | file = input_api.os_path.dirname(file) |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 75 | return False |
| 76 | |
| 77 | def FileIsInDir(file, dirs): |
| 78 | for dir in dirs: |
| 79 | if IsParentDir(file, dir): |
| 80 | return True |
| 81 | |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 82 | EXCLUSIVE_CHANGE_DIRECTORIES = [ |
| 83 | [ |
| 84 | 'third_party', 'v8', |
| 85 | input_api.os_path.join('front_end', 'generated') |
| 86 | ], |
| 87 | [ |
| 88 | 'node_modules', |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 89 | 'package-lock.json', |
| 90 | input_api.os_path.join('scripts', 'deps', 'manage_node_deps.py'), |
| 91 | ], |
| 92 | ['OWNERS', input_api.os_path.join('config', 'owner')], |
| 93 | ] |
| 94 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 95 | affected_files = input_api.LocalPaths() |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 96 | num_affected = len(affected_files) |
| 97 | for dirs in EXCLUSIVE_CHANGE_DIRECTORIES: |
Paul Lewis | 14effba | 2019-12-02 14:56:40 | [diff] [blame] | 98 | dir_list = ', '.join(dirs) |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 99 | affected_in_dir = [ |
| 100 | file for file in affected_files if FileIsInDir(file, dirs) |
| 101 | ] |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 102 | num_in_dir = len(affected_in_dir) |
| 103 | if num_in_dir == 0: |
| 104 | continue |
Tim van der Lippe | ebb94a9 | 2019-11-19 17:07:53 | [diff] [blame] | 105 | # Addition of new third_party folders must have a new entry in `.gitignore` |
| 106 | if '.gitignore' in affected_files: |
| 107 | num_in_dir = num_in_dir + 1 |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 108 | if num_in_dir < num_affected: |
Tim van der Lippe | 239963b | 2021-04-09 09:43:38 | [diff] [blame] | 109 | unexpected_files = [ |
| 110 | file for file in affected_files if file not in affected_in_dir |
| 111 | ] |
| 112 | results.append( |
| 113 | output_api.PresubmitError( |
| 114 | ('CLs that affect files in "%s" should be limited to these files/directories.' |
| 115 | % dir_list) + |
| 116 | ('\nUnexpected files: %s.' % unexpected_files) + |
| 117 | '\nYou can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message' |
| 118 | )) |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 119 | break |
| 120 | |
| 121 | return results |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 122 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 123 | |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 124 | def _CheckBugAssociation(input_api, output_api, is_committing): |
| 125 | results = [output_api.PresubmitNotifyResult('Bug Association Check:')] |
| 126 | bugs = input_api.change.BugsFromDescription() |
| 127 | message = ( |
| 128 | "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n" |
| 129 | "the footer of the commit description. If you explicitly don\'t want to\n" |
| 130 | "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n" |
| 131 | "Note: The footer of the commit description is the last block of lines in\n" |
| 132 | "the commit description that doesn't contain empty lines. This means that\n" |
| 133 | "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n" |
| 134 | "line are not detected by this presubmit check.") |
| 135 | |
| 136 | if not bugs: |
| 137 | if is_committing: |
| 138 | results.append(output_api.PresubmitError(message)) |
| 139 | else: |
| 140 | results.append(output_api.PresubmitNotifyResult(message)) |
| 141 | |
| 142 | for bug in bugs: |
| 143 | results.append(output_api.PresubmitNotifyResult(('%s') % bug)) |
| 144 | |
| 145 | return results |
| 146 | |
| 147 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | def _CheckBuildGN(input_api, output_api): |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 149 | results = [output_api.PresubmitNotifyResult('Running BUILD.GN check:')] |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 150 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 151 | 'scripts', 'check_gn.js') |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 152 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 153 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | |
| 155 | |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 156 | def _CheckExperimentTelemetry(input_api, output_api): |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 157 | experiment_telemetry_files = [ |
| 158 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame^] | 159 | 'entrypoints', 'main', 'MainImpl.ts'), |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 160 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Tim van der Lippe | e024731 | 2021-04-01 14:25:30 | [diff] [blame] | 161 | 'core', 'host', 'UserMetrics.ts') |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 162 | ] |
| 163 | affected_main_files = _getAffectedFiles(input_api, |
| 164 | experiment_telemetry_files, [], |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame^] | 165 | ['.ts']) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 166 | if len(affected_main_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 167 | return [ |
| 168 | output_api.PresubmitNotifyResult( |
| 169 | 'No affected files for telemetry check') |
| 170 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 171 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 172 | results = [ |
| 173 | output_api.PresubmitNotifyResult('Running Experiment Telemetry check:') |
| 174 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 175 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 176 | 'scripts', 'check_experiments.js') |
| 177 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 178 | return results |
| 179 | |
| 180 | |
Tim van der Lippe | e4bdd74 | 2019-12-17 15:40:16 | [diff] [blame] | 181 | def _CheckJSON(input_api, output_api): |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 182 | results = [output_api.PresubmitNotifyResult('Running JSON Validator:')] |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 183 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 184 | 'scripts', 'json_validator', |
| 185 | 'validate_module_json.js') |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 186 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 187 | return results |
Tim van der Lippe | e4bdd74 | 2019-12-17 15:40:16 | [diff] [blame] | 188 | |
| 189 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 190 | def _CheckFormat(input_api, output_api): |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 191 | node_modules_affected_files = _getAffectedFiles(input_api, [ |
| 192 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules') |
| 193 | ], [], []) |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 194 | |
| 195 | # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs. |
| 196 | if (len(node_modules_affected_files) > 0): |
| 197 | return [output_api.PresubmitNotifyResult('Skipping Format Checks because `node_modules` files are affected.')] |
| 198 | |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 199 | results = [output_api.PresubmitNotifyResult('Running Format Checks:')] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 201 | return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 202 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 203 | def _CheckDevToolsStyleJS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 204 | results = [output_api.PresubmitNotifyResult('JS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 205 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 206 | 'scripts', 'test', |
| 207 | 'run_lint_check_js.js') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 208 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 209 | front_end_directory = input_api.os_path.join( |
| 210 | input_api.PresubmitLocalPath(), 'front_end') |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 211 | component_docs_directory = input_api.os_path.join(front_end_directory, |
Tim van der Lippe | e622f55 | 2021-04-14 14:15:18 | [diff] [blame] | 212 | 'ui', 'components', |
| 213 | 'docs') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 214 | inspector_overlay_directory = input_api.os_path.join( |
| 215 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 216 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 217 | 'test') |
| 218 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 219 | 'scripts') |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 220 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 221 | default_linted_directories = [ |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 222 | front_end_directory, test_directory, scripts_directory, |
| 223 | inspector_overlay_directory |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 224 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 225 | |
| 226 | eslint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 227 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 228 | 'eslint'), |
Tim van der Lippe | cf4ab40 | 2021-02-12 14:30:58 | [diff] [blame] | 229 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 230 | '@typescript-eslint'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 231 | input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 232 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 233 | '.eslintignore'), |
Tim van der Lippe | 33543ac | 2020-12-14 14:37:45 | [diff] [blame] | 234 | input_api.os_path.join(front_end_directory, '.eslintrc.js'), |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 235 | input_api.os_path.join(component_docs_directory, '.eslintrc.js'), |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 236 | input_api.os_path.join(test_directory, '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 237 | input_api.os_path.join(scripts_directory, 'test', |
| 238 | 'run_lint_check_js.py'), |
| 239 | input_api.os_path.join(scripts_directory, 'test', |
| 240 | 'run_lint_check_js.js'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 241 | input_api.os_path.join(scripts_directory, '.eslintrc.js'), |
| 242 | input_api.os_path.join(scripts_directory, 'eslint_rules'), |
| 243 | ] |
| 244 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 245 | lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [], |
| 246 | ['.js', '.py', '.eslintignore']) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 247 | |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 248 | should_bail_out, files_to_lint = _getFilesToLint( |
| 249 | input_api, output_api, lint_config_files, default_linted_directories, |
| 250 | ['.js', '.ts'], results) |
| 251 | if should_bail_out: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 252 | return results |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 253 | |
Brandon Goddard | e34e94f | 2021-04-12 17:58:26 | [diff] [blame] | 254 | # If there are more than 50 files to check, don't bother and check |
| 255 | # everything, so as to not run into command line length limits on Windows. |
| 256 | if len(files_to_lint) > 50: |
| 257 | files_to_lint = [] |
| 258 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 259 | results.extend( |
| 260 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
Tim van der Lippe | 9813224 | 2020-04-14 16:16:54 | [diff] [blame] | 261 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 262 | |
| 263 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 264 | def _CheckDevToolsStyleCSS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 265 | results = [output_api.PresubmitNotifyResult('CSS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 266 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 267 | 'scripts', 'test', |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 268 | 'run_lint_check_css.js') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 269 | |
| 270 | front_end_directory = input_api.os_path.join( |
| 271 | input_api.PresubmitLocalPath(), 'front_end') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 272 | inspector_overlay_directory = input_api.os_path.join( |
| 273 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 274 | default_linted_directories = [ |
| 275 | front_end_directory, inspector_overlay_directory |
| 276 | ] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 277 | |
| 278 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 279 | 'scripts') |
| 280 | |
| 281 | stylelint_related_files = [ |
| 282 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 283 | 'stylelint'), |
| 284 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 285 | '.stylelintrc.json'), |
| 286 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 287 | '.stylelintignore'), |
| 288 | input_api.os_path.join(scripts_directory, 'test', |
Sigurd Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 289 | 'run_lint_check_css.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 290 | ] |
| 291 | |
| 292 | lint_config_files = _getAffectedFiles(input_api, stylelint_related_files, |
Sigurd Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 293 | [], []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 294 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 295 | css_should_bail_out, css_files_to_lint = _getFilesToLint( |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 296 | input_api, output_api, lint_config_files, default_linted_directories, |
| 297 | ['.css'], results) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 298 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 299 | ts_should_bail_out, ts_files_to_lint = _getFilesToLint( |
| 300 | input_api, output_api, lint_config_files, default_linted_directories, |
| 301 | ['.ts'], results) |
| 302 | |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 303 | # If there are more than 50 files to check, don't bother and check |
| 304 | # everything, so as to not run into command line length limits on Windows. |
| 305 | if not css_should_bail_out: |
| 306 | if len(css_files_to_lint) < 50: |
| 307 | script_args = ["--files"] + css_files_to_lint |
| 308 | else: |
| 309 | script_args = [] # The defaults check all CSS files. |
| 310 | results.extend( |
| 311 | _checkWithNodeScript(input_api, output_api, lint_path, |
| 312 | script_args)) |
| 313 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 314 | if not ts_should_bail_out: |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 315 | script_args = ["--syntax", "html"] |
| 316 | if len(ts_files_to_lint) < 50: |
| 317 | script_args += ["--files"] + ts_files_to_lint |
| 318 | else: |
| 319 | script_args += ["--glob", "front_end/**/*.ts"] |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 320 | results.extend( |
| 321 | _checkWithNodeScript(input_api, output_api, lint_path, |
| 322 | script_args)) |
| 323 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 324 | return results |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 325 | |
| 326 | |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 327 | def _CheckDevToolsPythonLikeFileLicenseHeaders(input_api, output_api): |
| 328 | results = [ |
| 329 | output_api.PresubmitNotifyResult( |
| 330 | 'Python-like file license header check:') |
| 331 | ] |
| 332 | lint_path = input_api.os_path.join( |
| 333 | input_api.PresubmitLocalPath(), 'scripts', 'test', |
| 334 | 'run_header_check_python_like_files.js') |
| 335 | |
| 336 | front_end_directory = input_api.os_path.join( |
| 337 | input_api.PresubmitLocalPath(), 'front_end') |
| 338 | inspector_overlay_directory = input_api.os_path.join( |
| 339 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 340 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 341 | 'test') |
| 342 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 343 | 'scripts') |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 344 | config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 345 | 'config') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 346 | |
| 347 | default_linted_directories = [ |
| 348 | front_end_directory, test_directory, scripts_directory, |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 349 | inspector_overlay_directory, config_directory |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 350 | ] |
| 351 | |
| 352 | check_related_files = [lint_path] |
| 353 | |
| 354 | lint_config_files = _getAffectedFiles(input_api, check_related_files, [], |
| 355 | ['.js']) |
| 356 | |
| 357 | should_bail_out, files_to_lint = _getFilesToLint( |
| 358 | input_api, output_api, lint_config_files, default_linted_directories, |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 359 | ['BUILD.gn', '.gni'], results) |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 360 | if should_bail_out: |
| 361 | return results |
| 362 | |
| 363 | # If there are more than 50 files to check, don't bother and check |
| 364 | # everything, so as to not run into command line length limits on Windows. |
| 365 | if len(files_to_lint) > 50: |
| 366 | files_to_lint = [] |
| 367 | |
| 368 | results.extend( |
| 369 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
| 370 | return results |
| 371 | |
| 372 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 373 | def _CheckGeneratedFiles(input_api, output_api): |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 374 | v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8') |
| 375 | blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink') |
| 376 | protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol') |
| 377 | scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build') |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 378 | 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 | [diff] [blame] | 379 | |
| 380 | generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py') |
| 381 | generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py') |
| 382 | generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py') |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 383 | generated_protocol_typescript_path = input_api.os_path.join( |
| 384 | input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 385 | concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', |
| 386 | 'concatenate_protocols.py') |
| 387 | |
| 388 | affected_files = _getAffectedFiles(input_api, [ |
| 389 | v8_directory_path, |
| 390 | blink_directory_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 391 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', |
| 392 | 'pyjson5'), |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 393 | generated_aria_path, |
| 394 | generated_supported_css_path, |
| 395 | concatenate_protocols_path, |
| 396 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 397 | scripts_generated_output_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 398 | generated_protocol_typescript_path, |
| 399 | ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 400 | |
| 401 | if len(affected_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 402 | return [ |
| 403 | output_api.PresubmitNotifyResult( |
| 404 | 'No affected files for generated files check') |
| 405 | ] |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 406 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 407 | results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')] |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 408 | generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 409 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 410 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 411 | return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 412 | |
| 413 | |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 414 | def _CollectStrings(input_api, output_api): |
| 415 | devtools_root = input_api.PresubmitLocalPath() |
| 416 | 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] | 417 | script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', |
| 418 | 'collect-strings.js') |
| 419 | affected_front_end_files = _getAffectedFiles( |
| 420 | input_api, [devtools_front_end, script_path], [], ['.js', '.ts']) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 421 | if len(affected_front_end_files) == 0: |
| 422 | return [ |
| 423 | output_api.PresubmitNotifyResult( |
| 424 | 'No affected files to run collect-strings') |
| 425 | ] |
| 426 | |
| 427 | results = [ |
| 428 | output_api.PresubmitNotifyResult('Collecting strings from front_end:') |
| 429 | ] |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 430 | results.extend( |
| 431 | _checkWithNodeScript(input_api, output_api, script_path, |
| 432 | [devtools_front_end])) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 433 | results.append( |
| 434 | output_api.PresubmitNotifyResult( |
Peter Marshall | d67e9f1 | 2021-02-08 09:34:35 | [diff] [blame] | 435 | 'Please commit en-US.json/en-XL.json if changes are generated.')) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 436 | return results |
| 437 | |
| 438 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 439 | def _CheckNoUncheckedFiles(input_api, output_api): |
| 440 | results = [] |
| 441 | process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'], |
| 442 | stdout=input_api.subprocess.PIPE, |
| 443 | stderr=input_api.subprocess.STDOUT) |
| 444 | out, _ = process.communicate() |
| 445 | if process.returncode != 0: |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 446 | files_changed_process = input_api.subprocess.Popen( |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 447 | ['git', 'diff'], |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 448 | stdout=input_api.subprocess.PIPE, |
| 449 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 450 | files_changed, _ = files_changed_process.communicate() |
| 451 | |
| 452 | return [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 453 | output_api.PresubmitError( |
| 454 | 'You have changed files that need to be committed:'), |
| 455 | output_api.PresubmitError(files_changed.decode('utf-8')) |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 456 | ] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 457 | return [] |
| 458 | |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 459 | def _CheckForTooLargeFiles(input_api, output_api): |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 460 | """Avoid large files, especially binary files, in the repository since |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 461 | git doesn't scale well for those. They will be in everyone's repo |
| 462 | clones forever, forever making Chromium slower to clone and work |
| 463 | with.""" |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 464 | # Uploading files to cloud storage is not trivial so we don't want |
| 465 | # to set the limit too low, but the upper limit for "normal" large |
| 466 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 467 | # anything over 20 MB is exceptional. |
| 468 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB |
| 469 | too_large_files = [] |
| 470 | for f in input_api.AffectedFiles(): |
| 471 | # Check both added and modified files (but not deleted files). |
| 472 | if f.Action() in ('A', 'M'): |
| 473 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
| 474 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
| 475 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
| 476 | if too_large_files: |
| 477 | message = ( |
| 478 | 'Do not commit large files to git since git scales badly for those.\n' + |
| 479 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 480 | 'fetch them.\n' + '\n'.join(too_large_files) |
| 481 | ) |
| 482 | return [output_api.PresubmitError( |
| 483 | 'Too large files found in commit', long_text=message + '\n')] |
| 484 | else: |
| 485 | return [] |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 486 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 487 | |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 488 | def _RunCannedChecks(input_api, output_api): |
| 489 | results = [] |
| 490 | results.extend( |
| 491 | input_api.canned_checks.CheckOwnersFormat(input_api, output_api)) |
| 492 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
| 493 | results.extend( |
| 494 | input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 495 | input_api, output_api)) |
| 496 | results.extend( |
| 497 | input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
Tim van der Lippe | 6977538 | 2021-05-27 16:06:12 | [diff] [blame] | 498 | input_api, |
| 499 | output_api, |
| 500 | source_file_filter=lambda file: not file.LocalPath().startswith( |
| 501 | 'node_modules'))) |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 502 | results.extend( |
| 503 | input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
| 504 | return results |
| 505 | |
| 506 | |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 507 | def _CommonChecks(input_api, output_api): |
Mathias Bynens | 032591d | 2019-10-21 09:51:31 | [diff] [blame] | 508 | """Checks common to both upload and commit.""" |
| 509 | results = [] |
Mathias Bynens | 011b007 | 2020-08-05 08:17:35 | [diff] [blame] | 510 | results.extend( |
| 511 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 512 | input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT])) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | results.extend(_CheckBuildGN(input_api, output_api)) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 514 | results.extend(_CheckExperimentTelemetry(input_api, output_api)) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 515 | results.extend(_CheckGeneratedFiles(input_api, output_api)) |
Tim van der Lippe | e4bdd74 | 2019-12-17 15:40:16 | [diff] [blame] | 516 | results.extend(_CheckJSON(input_api, output_api)) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 517 | results.extend(_CheckDevToolsStyleJS(input_api, output_api)) |
| 518 | results.extend(_CheckDevToolsStyleCSS(input_api, output_api)) |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 519 | results.extend( |
| 520 | _CheckDevToolsPythonLikeFileLicenseHeaders(input_api, output_api)) |
Jack Franklin | b10193f | 2021-03-19 10:25:08 | [diff] [blame] | 521 | |
Tim van der Lippe | 5497d48 | 2020-01-14 15:27:30 | [diff] [blame] | 522 | results.extend(_CheckFormat(input_api, output_api)) |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 523 | results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api)) |
Peter Marshall | cd84551 | 2021-01-28 14:29:21 | [diff] [blame] | 524 | results.extend(_CheckI18nWasBundled(input_api, output_api)) |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 525 | # Run the canned checks from `depot_tools` after the custom DevTools checks. |
| 526 | # The canned checks for example check that lines have line endings. The |
| 527 | # DevTools presubmit checks automatically fix these issues. If we would run |
| 528 | # the canned checks before the DevTools checks, they would erroneously conclude |
| 529 | # that there are issues in the code. Since the canned checks are allowed to be |
| 530 | # ignored, a confusing message is shown that asks if the failed presubmit can |
| 531 | # be continued regardless. By fixing the issues before we reach the canned checks, |
| 532 | # we don't show the message to suppress these errors, which would otherwise be |
| 533 | # causing CQ to fail. |
| 534 | results.extend(_RunCannedChecks(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 535 | return results |
| 536 | |
| 537 | |
| 538 | def _SideEffectChecks(input_api, output_api): |
| 539 | """Check side effects caused by other checks""" |
| 540 | results = [] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 541 | results.extend(_CheckNoUncheckedFiles(input_api, output_api)) |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 542 | results.extend(_CheckForTooLargeFiles(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 543 | return results |
| 544 | |
| 545 | |
Liviu Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 546 | def CheckChangeOnUpload(input_api, output_api): |
| 547 | results = [] |
| 548 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 549 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 550 | # Run checks that rely on output from other DevTool checks |
| 551 | results.extend(_SideEffectChecks(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 552 | results.extend(_CheckBugAssociation(input_api, output_api, False)) |
Liviu Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 553 | return results |
| 554 | |
| 555 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | def CheckChangeOnCommit(input_api, output_api): |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 | [diff] [blame] | 557 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 558 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 559 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 560 | # Run checks that rely on output from other DevTool checks |
| 561 | results.extend(_SideEffectChecks(input_api, output_api)) |
Mathias Bynens | 032591d | 2019-10-21 09:51:31 | [diff] [blame] | 562 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 563 | results.extend(_CheckBugAssociation(input_api, output_api, True)) |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 | [diff] [blame] | 564 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 565 | |
| 566 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 567 | def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 568 | """Return absolute file paths of affected files (not due to an excluded action) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 569 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 570 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 571 | local_paths = [ |
| 572 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions) |
| 573 | ] |
| 574 | affected_files = [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 575 | file_name for file_name in local_paths |
| 576 | if any(parent_directory in file_name |
| 577 | for parent_directory in parent_directories) and ( |
| 578 | len(accepted_endings) == 0 or any( |
| 579 | file_name.endswith(accepted_ending) |
| 580 | for accepted_ending in accepted_endings)) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 581 | ] |
| 582 | return affected_files |
| 583 | |
| 584 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 585 | def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 586 | original_sys_path = sys.path |
| 587 | try: |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 588 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')] |
Yang Guo | d817698 | 2019-10-04 20:30:35 | [diff] [blame] | 589 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 590 | finally: |
| 591 | sys.path = original_sys_path |
| 592 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 593 | return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 594 | |
| 595 | |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 596 | def _checkWithTypeScript(input_api, |
| 597 | output_api, |
| 598 | tsc_arguments, |
| 599 | script_path, |
| 600 | script_arguments=[]): # pylint: disable=invalid-name |
| 601 | original_sys_path = sys.path |
| 602 | try: |
| 603 | sys.path = sys.path + [ |
| 604 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') |
| 605 | ] |
| 606 | import devtools_paths |
| 607 | finally: |
| 608 | sys.path = original_sys_path |
| 609 | |
| 610 | # First run tsc to compile the TS script that we then run in the _ExecuteSubProcess call |
| 611 | tsc_compiler_process = input_api.subprocess.Popen( |
| 612 | [ |
| 613 | devtools_paths.node_path(), |
| 614 | devtools_paths.typescript_compiler_path() |
| 615 | ] + tsc_arguments, |
| 616 | stdout=input_api.subprocess.PIPE, |
| 617 | stderr=input_api.subprocess.STDOUT) |
| 618 | |
| 619 | out, _ = tsc_compiler_process.communicate() |
| 620 | if tsc_compiler_process.returncode != 0: |
| 621 | return [ |
| 622 | output_api.PresubmitError('Error compiling briges regenerator:\n' + |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 623 | out.decode('utf-8')) |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 624 | ] |
| 625 | |
| 626 | return _checkWithNodeScript(input_api, output_api, script_path, |
| 627 | script_arguments) |
| 628 | |
| 629 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 630 | def _getFilesToLint(input_api, output_api, lint_config_files, |
| 631 | default_linted_directories, accepted_endings, results): |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 632 | run_full_check = False |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 633 | files_to_lint = [] |
| 634 | |
| 635 | # We are changing the lint configuration; run the full check. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 636 | if len(lint_config_files) != 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 637 | results.append( |
| 638 | output_api.PresubmitNotifyResult('Running full lint check')) |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 639 | run_full_check = True |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 640 | else: |
| 641 | # Only run the linter on files that are relevant, to save PRESUBMIT time. |
| 642 | files_to_lint = _getAffectedFiles(input_api, |
| 643 | default_linted_directories, ['D'], |
| 644 | accepted_endings) |
| 645 | |
Paul Lewis | 2b9224f | 2020-09-08 17:13:10 | [diff] [blame] | 646 | # Exclude front_end/third_party files. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 647 | files_to_lint = [ |
| 648 | file for file in files_to_lint if "third_party" not in file |
| 649 | ] |
Paul Lewis | 2b9224f | 2020-09-08 17:13:10 | [diff] [blame] | 650 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 651 | if len(files_to_lint) == 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 652 | results.append( |
| 653 | output_api.PresubmitNotifyResult( |
| 654 | 'No affected files for lint check')) |
| 655 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 656 | should_bail_out = len(files_to_lint) == 0 and not run_full_check |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 657 | return should_bail_out, files_to_lint |
Peter Marshall | cd84551 | 2021-01-28 14:29:21 | [diff] [blame] | 658 | |
| 659 | |
| 660 | def _CheckI18nWasBundled(input_api, output_api): |
| 661 | affected_files = _getAffectedFiles(input_api, [ |
| 662 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
| 663 | 'third_party', 'i18n', 'lib') |
| 664 | ], [], ['.js']) |
| 665 | |
| 666 | if len(affected_files) == 0: |
| 667 | return [ |
| 668 | output_api.PresubmitNotifyResult( |
| 669 | 'No affected files for i18n bundle check') |
| 670 | ] |
| 671 | |
| 672 | results = [output_api.PresubmitNotifyResult('Running buildi18nBundle.js:')] |
| 673 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 674 | 'scripts', 'localizationV2', |
| 675 | 'buildi18nBundle.js') |
| 676 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 677 | return results |