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. |
| 28 | """DevTools JSDoc validator presubmit script |
| 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")] |
| 53 | import local_node |
| 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()) |
| 62 | formattable_files = [affected_file for affected_file in affected_files |
| 63 | if all(ignore_file not in affected_file for ignore_file in ignore_files)] |
| 64 | if len(formattable_files) == 0: |
| 65 | return [] |
| 66 | |
| 67 | check_formatting_process = popen(['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files) |
| 68 | check_formatting_process.communicate() |
| 69 | if check_formatting_process.returncode == 0: |
| 70 | return [] |
| 71 | |
| 72 | format_args = ['git', 'cl', 'format', '--js'] + formattable_files |
| 73 | format_process = popen(format_args) |
| 74 | format_out, _ = format_process.communicate() |
| 75 | if format_process.returncode != 0: |
| 76 | return [output_api.PresubmitError(format_out)] |
| 77 | |
| 78 | # Use eslint to autofix the braces. |
| 79 | # Also fix semicolon to avoid confusing clang-format. |
| 80 | eslint_process = popen([ |
| 81 | local_node.node_path(), local_node.eslint_path(), |
| 82 | '--no-eslintrc', '--fix', '--env=es6', '--rule={"curly": [2, "multi-or-nest", "consistent"], "semi": 2}' |
| 83 | ] + affected_files) |
| 84 | eslint_process.communicate() |
| 85 | |
| 86 | # Need to run clang-format again to align the braces |
| 87 | popen(format_args).communicate() |
| 88 | |
| 89 | return [ |
Erik Luo | 66e332c | 2018-04-09 18:00:14 +0000 | [diff] [blame] | 90 | 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] | 91 | "Ran clang-format on diff\n" |
| 92 | "Use git status to check the formatting changes"), |
| 93 | output_api.PresubmitError(format_out), |
| 94 | ] |
| 95 | |
| 96 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 97 | def _CheckDevtoolsWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 98 | affected_front_end_files = _getAffectedFrontEndFiles(input_api) |
| 99 | if len(affected_front_end_files) == 0: |
| 100 | return [] |
| 101 | else: |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 102 | if script_arguments is None: |
| 103 | script_arguments = [] |
| 104 | return _checkWithNodeScript(input_api, output_api, script_path, script_arguments) |
| 105 | |
| 106 | |
| 107 | def _CheckDevtoolsLocalizableResources(input_api, output_api): # pylint: disable=invalid-name |
| 108 | affected_front_end_files = _getAffectedFrontEndFiles(input_api) |
| 109 | if len(affected_front_end_files) == 0: |
| 110 | return [] |
| 111 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "check_localizable_resources.js") |
| 112 | args = ['--autofix'] |
| 113 | return _CheckDevtoolsWithNodeScript(input_api, output_api, script_path, args) |
| 114 | |
| 115 | |
| 116 | def _CheckDevtoolsLocalization(input_api, output_api): # pylint: disable=invalid-name |
| 117 | affected_front_end_files = [ |
| 118 | input_api.os_path.join(input_api.PresubmitLocalPath(), file_path) for file_path in _getAffectedFrontEndFiles(input_api) |
| 119 | ] |
| 120 | if len(affected_front_end_files) == 0: |
| 121 | return [] |
| 122 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "check_localizability.js") |
| 123 | return _checkWithNodeScript(input_api, output_api, script_path, affected_front_end_files) |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 124 | |
| 125 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 126 | def _CheckDevtoolsStyle(input_api, output_api): |
| 127 | affected_front_end_files = _getAffectedFrontEndFiles(input_api) |
| 128 | if len(affected_front_end_files) > 0: |
| 129 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "lint_javascript.py") |
| 130 | process = input_api.subprocess.Popen( |
| 131 | [input_api.python_executable, lint_path] + affected_front_end_files, |
| 132 | stdout=input_api.subprocess.PIPE, |
| 133 | stderr=input_api.subprocess.STDOUT) |
| 134 | out, _ = process.communicate() |
| 135 | if process.returncode != 0: |
| 136 | return [output_api.PresubmitError(out)] |
| 137 | return [output_api.PresubmitNotifyResult(out)] |
| 138 | return [] |
| 139 | |
| 140 | |
| 141 | def _CompileDevtoolsFrontend(input_api, output_api): |
| 142 | compile_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "compile_frontend.py") |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 143 | out, _ = input_api.subprocess.Popen([input_api.python_executable, compile_path], |
| 144 | stdout=input_api.subprocess.PIPE, |
| 145 | stderr=input_api.subprocess.STDOUT).communicate() |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 146 | if "ERROR" in out or "WARNING" in out: |
| 147 | return [output_api.PresubmitError(out)] |
| 148 | if "NOTE" in out: |
| 149 | return [output_api.PresubmitPromptWarning(out)] |
| 150 | return [] |
| 151 | |
| 152 | |
| 153 | def _CheckConvertSVGToPNGHashes(input_api, output_api): |
| 154 | if not input_api.platform.startswith('linux'): |
| 155 | return [] |
| 156 | |
| 157 | original_sys_path = sys.path |
| 158 | try: |
| 159 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')] |
| 160 | import devtools_file_hashes |
| 161 | finally: |
| 162 | sys.path = original_sys_path |
| 163 | |
| 164 | absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)] |
| 165 | images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src") |
| 166 | image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")] |
| 167 | image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src") |
| 168 | hashes_file_name = "svg2png.hashes" |
| 169 | hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name) |
| 170 | invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths) |
| 171 | if len(invalid_hash_file_paths) == 0: |
| 172 | return [] |
| 173 | invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths] |
| 174 | file_paths_str = ", ".join(invalid_hash_file_names) |
| 175 | error_message = "The following SVG files should be converted to PNG using convert_svg_images_png.py script before uploading: \n - %s" % file_paths_str |
| 176 | return [output_api.PresubmitError(error_message)] |
| 177 | |
| 178 | |
| 179 | def _CheckOptimizePNGHashes(input_api, output_api): |
| 180 | if not input_api.platform.startswith('linux'): |
| 181 | return [] |
| 182 | |
| 183 | original_sys_path = sys.path |
| 184 | try: |
| 185 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')] |
| 186 | import devtools_file_hashes |
| 187 | finally: |
| 188 | sys.path = original_sys_path |
| 189 | |
| 190 | absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)] |
| 191 | images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src") |
| 192 | image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")] |
| 193 | image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src") |
| 194 | hashes_file_name = "optimize_png.hashes" |
| 195 | hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name) |
| 196 | invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths) |
| 197 | if len(invalid_hash_file_paths) == 0: |
| 198 | return [] |
| 199 | invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths] |
| 200 | file_paths_str = ", ".join(invalid_hash_file_names) |
| 201 | error_message = "The following PNG files should be optimized using optimize_png_images.py script before uploading: \n - %s" % file_paths_str |
| 202 | return [output_api.PresubmitError(error_message)] |
| 203 | |
| 204 | |
| 205 | def _CheckCSSViolations(input_api, output_api): |
| 206 | results = [] |
| 207 | for f in input_api.AffectedFiles(include_deletes=False): |
| 208 | if not f.LocalPath().endswith(".css"): |
| 209 | continue |
| 210 | for line_number, line in f.ChangedContents(): |
| 211 | if "/deep/" in line: |
| 212 | results.append(output_api.PresubmitError(("%s:%d uses /deep/ selector") % (f.LocalPath(), line_number))) |
| 213 | if "::shadow" in line: |
| 214 | results.append(output_api.PresubmitError(("%s:%d uses ::shadow selector") % (f.LocalPath(), line_number))) |
| 215 | return results |
| 216 | |
| 217 | |
| 218 | def CheckChangeOnUpload(input_api, output_api): |
| 219 | results = [] |
| 220 | results.extend(_CheckBuildGN(input_api, output_api)) |
| 221 | results.extend(_CheckFormat(input_api, output_api)) |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 222 | results.extend(_CheckDevtoolsLocalizableResources(input_api, output_api)) |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 223 | results.extend(_CheckDevtoolsLocalization(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 224 | results.extend(_CheckDevtoolsStyle(input_api, output_api)) |
| 225 | results.extend(_CompileDevtoolsFrontend(input_api, output_api)) |
| 226 | results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api)) |
| 227 | results.extend(_CheckOptimizePNGHashes(input_api, output_api)) |
| 228 | results.extend(_CheckCSSViolations(input_api, output_api)) |
| 229 | return results |
| 230 | |
| 231 | |
| 232 | def CheckChangeOnCommit(input_api, output_api): |
| 233 | return [] |
| 234 | |
| 235 | |
| 236 | def _getAffectedFrontEndFiles(input_api): |
| 237 | local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f.Action() != "D"] |
| 238 | devtools_root = input_api.PresubmitLocalPath() |
| 239 | devtools_front_end = input_api.os_path.join(devtools_root, "front_end") |
| 240 | affected_front_end_files = [ |
| 241 | file_name for file_name in local_paths if devtools_front_end in file_name and file_name.endswith(".js") |
| 242 | ] |
| 243 | return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files] |
| 244 | |
| 245 | |
| 246 | def _getAffectedJSFiles(input_api): |
| 247 | local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f.Action() != "D"] |
| 248 | devtools_root = input_api.PresubmitLocalPath() |
| 249 | devtools_front_end = input_api.os_path.join(devtools_root, "front_end") |
| 250 | devtools_scripts = input_api.os_path.join(devtools_root, "scripts") |
| 251 | affected_js_files = [ |
| 252 | file_name for file_name in local_paths |
| 253 | if (devtools_front_end in file_name or devtools_scripts in file_name) and file_name.endswith(".js") |
| 254 | ] |
| 255 | return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files] |
| 256 | |
| 257 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 258 | 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] | 259 | original_sys_path = sys.path |
| 260 | try: |
| 261 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts")] |
| 262 | import local_node |
| 263 | finally: |
| 264 | sys.path = original_sys_path |
| 265 | |
| 266 | node_path = local_node.node_path() |
| 267 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 268 | if script_arguments is None: |
| 269 | script_arguments = [] |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 270 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 271 | process = input_api.subprocess.Popen( |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 272 | [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] | 273 | out, _ = process.communicate() |
| 274 | |
| 275 | if process.returncode != 0: |
| 276 | return [output_api.PresubmitError(out)] |
| 277 | return [output_api.PresubmitNotifyResult(out)] |