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