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 |
| 36 | |
| 37 | |
| 38 | def _CheckBuildGN(input_api, output_api): |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 39 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 40 | return _checkWithNodeScript(input_api, output_api, script_path) |
| 41 | |
| 42 | |
| 43 | def _CheckFormat(input_api, output_api): |
| 44 | |
| 45 | def popen(args): |
| 46 | return input_api.subprocess.Popen(args=args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT) |
| 47 | |
| 48 | affected_files = _getAffectedJSFiles(input_api) |
| 49 | if len(affected_files) == 0: |
| 50 | return [] |
| 51 | original_sys_path = sys.path |
| 52 | try: |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 53 | 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] | 54 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 55 | finally: |
| 56 | sys.path = original_sys_path |
| 57 | |
| 58 | ignore_files = [] |
| 59 | eslint_ignore_path = input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintignore') |
| 60 | with open(eslint_ignore_path, 'r') as ignore_manifest: |
| 61 | for line in ignore_manifest: |
Ingvar Stepanyan | b532f3f | 2019-10-22 12:32:59 +0100 | [diff] [blame] | 62 | ignore_files.append(input_api.os_path.normpath(line.strip())) |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 63 | formattable_files = [ |
| 64 | affected_file for affected_file in affected_files if all(ignore_file not in affected_file for ignore_file in ignore_files) |
| 65 | ] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 66 | if len(formattable_files) == 0: |
| 67 | return [] |
| 68 | |
| 69 | check_formatting_process = popen(['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files) |
| 70 | check_formatting_process.communicate() |
| 71 | if check_formatting_process.returncode == 0: |
| 72 | return [] |
| 73 | |
| 74 | format_args = ['git', 'cl', 'format', '--js'] + formattable_files |
| 75 | format_process = popen(format_args) |
| 76 | format_out, _ = format_process.communicate() |
| 77 | if format_process.returncode != 0: |
| 78 | return [output_api.PresubmitError(format_out)] |
| 79 | |
| 80 | # Use eslint to autofix the braces. |
| 81 | # Also fix semicolon to avoid confusing clang-format. |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 82 | eslint_process = popen( |
| 83 | [devtools_paths.node_path(), devtools_paths.eslint_path(), '--config', '.eslintrc.js', '--fix'] + affected_files) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 84 | eslint_process.communicate() |
| 85 | |
| 86 | # Need to run clang-format again to align the braces |
| 87 | popen(format_args).communicate() |
| 88 | |
| 89 | return [ |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 90 | output_api.PresubmitError('ERROR: Found formatting violations in third_party/blink/renderer/devtools.\n' |
| 91 | 'Ran clang-format on diff\n' |
| 92 | 'Use git status to check the formatting changes'), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 93 | output_api.PresubmitError(format_out), |
| 94 | ] |
| 95 | |
| 96 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 97 | def _CheckDevtoolsLocalizableResources(input_api, output_api): # pylint: disable=invalid-name |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 98 | devtools_root = input_api.PresubmitLocalPath() |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 99 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
| 100 | affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], [], ['.js', 'module.json', '.grd', '.grdp']) |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 101 | if len(affected_front_end_files) == 0: |
| 102 | return [] |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 103 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_localizable_resources.js') |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 104 | args = ['--autofix'] |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 105 | return _checkWithNodeScript(input_api, output_api, script_path, args) |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def _CheckDevtoolsLocalization(input_api, output_api): # pylint: disable=invalid-name |
Mandy Chen | e997da7 | 2019-08-22 23:50:19 +0000 | [diff] [blame] | 109 | devtools_root = input_api.PresubmitLocalPath() |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 110 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
| 111 | affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js', '.grdp']) |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 112 | if len(affected_front_end_files) == 0: |
| 113 | return [] |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 114 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_localizability.js') |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 115 | return _checkWithNodeScript(input_api, output_api, script_path, affected_front_end_files) |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 116 | |
| 117 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 118 | def _CheckDevtoolsStyle(input_api, output_api): |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 119 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'test', 'run_lint_check.py') |
Tim van der Lippe | c85c312 | 2019-09-19 11:27:04 +0000 | [diff] [blame] | 120 | process = input_api.subprocess.Popen([input_api.python_executable, lint_path], |
| 121 | stdout=input_api.subprocess.PIPE, |
| 122 | stderr=input_api.subprocess.STDOUT) |
| 123 | out, _ = process.communicate() |
| 124 | if process.returncode != 0: |
| 125 | return [output_api.PresubmitError(out)] |
| 126 | return [output_api.PresubmitNotifyResult(out)] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 127 | |
| 128 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 +0000 | [diff] [blame] | 129 | def _CheckOptimizeSVGHashes(input_api, output_api): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 130 | if not input_api.platform.startswith('linux'): |
| 131 | return [] |
| 132 | |
| 133 | original_sys_path = sys.path |
| 134 | try: |
| 135 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')] |
| 136 | import devtools_file_hashes |
| 137 | finally: |
| 138 | sys.path = original_sys_path |
| 139 | |
| 140 | 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^] | 141 | images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src') |
| 142 | image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')] |
| 143 | image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src') |
| 144 | hashes_file_name = 'optimize_svg.hashes' |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 145 | hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name) |
| 146 | invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths) |
| 147 | if len(invalid_hash_file_paths) == 0: |
| 148 | return [] |
| 149 | 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^] | 150 | file_paths_str = ', '.join(invalid_hash_file_names) |
| 151 | error_message = 'The following SVG files should be optimized using optimize_svg_images script before uploading: \n - %s' % file_paths_str |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 152 | return [output_api.PresubmitError(error_message)] |
| 153 | |
| 154 | |
| 155 | def _CheckCSSViolations(input_api, output_api): |
| 156 | results = [] |
| 157 | for f in input_api.AffectedFiles(include_deletes=False): |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 158 | if not f.LocalPath().endswith('.css'): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 159 | continue |
| 160 | for line_number, line in f.ChangedContents(): |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 161 | if '/deep/' in line: |
| 162 | results.append(output_api.PresubmitError(('%s:%d uses /deep/ selector') % (f.LocalPath(), line_number))) |
| 163 | if '::shadow' in line: |
| 164 | results.append(output_api.PresubmitError(('%s:%d uses ::shadow selector') % (f.LocalPath(), line_number))) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 165 | return results |
| 166 | |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 167 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 168 | def _CommonChecks(input_api, output_api): |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 169 | """Checks common to both upload and commit.""" |
| 170 | results = [] |
| 171 | results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api)) |
| 172 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
| 173 | results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api)) |
| 174 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api)) |
| 175 | results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
| 176 | results.extend(_CheckDevtoolsLocalizableResources(input_api, output_api)) |
| 177 | results.extend(_CheckDevtoolsLocalization(input_api, output_api)) |
| 178 | return results |
| 179 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 180 | |
| 181 | def CheckChangeOnUpload(input_api, output_api): |
| 182 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 183 | results.extend(_CommonChecks(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 184 | results.extend(_CheckBuildGN(input_api, output_api)) |
| 185 | results.extend(_CheckFormat(input_api, output_api)) |
| 186 | results.extend(_CheckDevtoolsStyle(input_api, output_api)) |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 +0000 | [diff] [blame] | 187 | results.extend(_CheckOptimizeSVGHashes(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 188 | results.extend(_CheckCSSViolations(input_api, output_api)) |
| 189 | return results |
| 190 | |
| 191 | |
| 192 | def CheckChangeOnCommit(input_api, output_api): |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 193 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 194 | results.extend(_CommonChecks(input_api, output_api)) |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 195 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 196 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 197 | |
| 198 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 199 | 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^] | 200 | """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] | 201 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 202 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 203 | local_paths = [ |
| 204 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions) |
| 205 | ] |
| 206 | affected_files = [ |
| 207 | file_name for file_name in local_paths |
| 208 | if any(parent_directory in file_name for parent_directory in parent_directories) and any( |
| 209 | file_name.endswith(accepted_ending) for accepted_ending in accepted_endings) |
| 210 | ] |
| 211 | return affected_files |
| 212 | |
| 213 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 214 | def _getAffectedFrontEndFiles(input_api): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 215 | devtools_root = input_api.PresubmitLocalPath() |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 216 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
| 217 | affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js']) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 218 | return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files] |
| 219 | |
| 220 | |
| 221 | def _getAffectedJSFiles(input_api): |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 222 | devtools_root = input_api.PresubmitLocalPath() |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 223 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
| 224 | devtools_scripts = input_api.os_path.join(devtools_root, 'scripts') |
| 225 | affected_js_files = _getAffectedFiles(input_api, [devtools_front_end, devtools_scripts], ['D'], ['.js']) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 226 | return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files] |
| 227 | |
| 228 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 229 | def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 230 | original_sys_path = sys.path |
| 231 | try: |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 232 | 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] | 233 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 234 | finally: |
| 235 | sys.path = original_sys_path |
| 236 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 237 | node_path = devtools_paths.node_path() |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 238 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 239 | if script_arguments is None: |
| 240 | script_arguments = [] |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 241 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 242 | process = input_api.subprocess.Popen( |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 243 | [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 244 | out, _ = process.communicate() |
| 245 | |
| 246 | if process.returncode != 0: |
| 247 | return [output_api.PresubmitError(out)] |
| 248 | return [output_api.PresubmitNotifyResult(out)] |