blob: 0749c1a446e033e79748b4391e07fc31c2ee0dc7 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001# 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
30See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31for more details about the presubmit API built into gcl.
32"""
33
34import sys
35
36
37def _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
42def _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(),
John Emau90d02622019-07-03 08:34:26 +000082 '--no-eslintrc', '--fix', '--env=es6', '--parser-options=ecmaVersion:9',
83 '--rule={"curly": [2, "multi-or-nest", "consistent"], "semi": 2}'
Blink Reformat4c46d092018-04-07 15:32:37 +000084 ] + affected_files)
85 eslint_process.communicate()
86
87 # Need to run clang-format again to align the braces
88 popen(format_args).communicate()
89
90 return [
Erik Luo66e332c2018-04-09 18:00:14 +000091 output_api.PresubmitError("ERROR: Found formatting violations in third_party/blink/renderer/devtools.\n"
Blink Reformat4c46d092018-04-07 15:32:37 +000092 "Ran clang-format on diff\n"
93 "Use git status to check the formatting changes"),
94 output_api.PresubmitError(format_out),
95 ]
96
97
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000098def _CheckDevtoolsWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Mandy Chen465b4f72019-03-21 22:52:54 +000099 affected_front_end_files = _getAffectedFrontEndFiles(input_api)
100 if len(affected_front_end_files) == 0:
101 return []
102 else:
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000103 if script_arguments is None:
104 script_arguments = []
105 return _checkWithNodeScript(input_api, output_api, script_path, script_arguments)
106
107
108def _CheckDevtoolsLocalizableResources(input_api, output_api): # pylint: disable=invalid-name
109 affected_front_end_files = _getAffectedFrontEndFiles(input_api)
110 if len(affected_front_end_files) == 0:
111 return []
112 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "check_localizable_resources.js")
113 args = ['--autofix']
114 return _CheckDevtoolsWithNodeScript(input_api, output_api, script_path, args)
115
116
117def _CheckDevtoolsLocalization(input_api, output_api): # pylint: disable=invalid-name
118 affected_front_end_files = [
119 input_api.os_path.join(input_api.PresubmitLocalPath(), file_path) for file_path in _getAffectedFrontEndFiles(input_api)
120 ]
121 if len(affected_front_end_files) == 0:
122 return []
123 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "check_localizability.js")
124 return _checkWithNodeScript(input_api, output_api, script_path, affected_front_end_files)
Mandy Chen465b4f72019-03-21 22:52:54 +0000125
126
Blink Reformat4c46d092018-04-07 15:32:37 +0000127def _CheckDevtoolsStyle(input_api, output_api):
128 affected_front_end_files = _getAffectedFrontEndFiles(input_api)
129 if len(affected_front_end_files) > 0:
130 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "lint_javascript.py")
131 process = input_api.subprocess.Popen(
132 [input_api.python_executable, lint_path] + affected_front_end_files,
133 stdout=input_api.subprocess.PIPE,
134 stderr=input_api.subprocess.STDOUT)
135 out, _ = process.communicate()
136 if process.returncode != 0:
137 return [output_api.PresubmitError(out)]
138 return [output_api.PresubmitNotifyResult(out)]
139 return []
140
141
142def _CompileDevtoolsFrontend(input_api, output_api):
143 compile_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts", "compile_frontend.py")
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000144 out, _ = input_api.subprocess.Popen([input_api.python_executable, compile_path],
145 stdout=input_api.subprocess.PIPE,
146 stderr=input_api.subprocess.STDOUT).communicate()
Blink Reformat4c46d092018-04-07 15:32:37 +0000147 if "ERROR" in out or "WARNING" in out:
148 return [output_api.PresubmitError(out)]
149 if "NOTE" in out:
150 return [output_api.PresubmitPromptWarning(out)]
151 return []
152
153
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000154def _CheckOptimizeSVGHashes(input_api, output_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000155 if not input_api.platform.startswith('linux'):
156 return []
157
158 original_sys_path = sys.path
159 try:
160 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
161 import devtools_file_hashes
162 finally:
163 sys.path = original_sys_path
164
165 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
166 images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src")
167 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")]
168 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000169 hashes_file_name = "optimize_svg.hashes"
Blink Reformat4c46d092018-04-07 15:32:37 +0000170 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
171 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
172 if len(invalid_hash_file_paths) == 0:
173 return []
174 invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
175 file_paths_str = ", ".join(invalid_hash_file_names)
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000176 error_message = "The following SVG files should be optimized using optimize_svg_images script before uploading: \n - %s" % file_paths_str
Blink Reformat4c46d092018-04-07 15:32:37 +0000177 return [output_api.PresubmitError(error_message)]
178
179
180def _CheckCSSViolations(input_api, output_api):
181 results = []
182 for f in input_api.AffectedFiles(include_deletes=False):
183 if not f.LocalPath().endswith(".css"):
184 continue
185 for line_number, line in f.ChangedContents():
186 if "/deep/" in line:
187 results.append(output_api.PresubmitError(("%s:%d uses /deep/ selector") % (f.LocalPath(), line_number)))
188 if "::shadow" in line:
189 results.append(output_api.PresubmitError(("%s:%d uses ::shadow selector") % (f.LocalPath(), line_number)))
190 return results
191
192
193def CheckChangeOnUpload(input_api, output_api):
194 results = []
195 results.extend(_CheckBuildGN(input_api, output_api))
196 results.extend(_CheckFormat(input_api, output_api))
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000197 results.extend(_CheckDevtoolsLocalizableResources(input_api, output_api))
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000198 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000199 results.extend(_CheckDevtoolsStyle(input_api, output_api))
200 results.extend(_CompileDevtoolsFrontend(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000201 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000202 results.extend(_CheckCSSViolations(input_api, output_api))
203 return results
204
205
206def CheckChangeOnCommit(input_api, output_api):
207 return []
208
209
210def _getAffectedFrontEndFiles(input_api):
211 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f.Action() != "D"]
212 devtools_root = input_api.PresubmitLocalPath()
213 devtools_front_end = input_api.os_path.join(devtools_root, "front_end")
214 affected_front_end_files = [
215 file_name for file_name in local_paths if devtools_front_end in file_name and file_name.endswith(".js")
216 ]
217 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files]
218
219
220def _getAffectedJSFiles(input_api):
221 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f.Action() != "D"]
222 devtools_root = input_api.PresubmitLocalPath()
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 = [
226 file_name for file_name in local_paths
227 if (devtools_front_end in file_name or devtools_scripts in file_name) and file_name.endswith(".js")
228 ]
229 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files]
230
231
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000232def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000233 original_sys_path = sys.path
234 try:
235 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), "scripts")]
236 import local_node
237 finally:
238 sys.path = original_sys_path
239
240 node_path = local_node.node_path()
241
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000242 if script_arguments is None:
243 script_arguments = []
Mandy Chen465b4f72019-03-21 22:52:54 +0000244
Blink Reformat4c46d092018-04-07 15:32:37 +0000245 process = input_api.subprocess.Popen(
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000246 [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Blink Reformat4c46d092018-04-07 15:32:37 +0000247 out, _ = process.communicate()
248
249 if process.returncode != 0:
250 return [output_api.PresubmitError(out)]
251 return [output_api.PresubmitNotifyResult(out)]