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