blob: f511244f4695aacda5e3e6252887cf0c85f37bf7 [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.
Yang Guo75beda92019-10-28 08:29:25 +010028"""
29DevTools presubmit script
Blink Reformat4c46d092018-04-07 15:32:37 +000030
31See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32for more details about the presubmit API built into gcl.
33"""
34
35import sys
36
37
38def _CheckBuildGN(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +010039 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js')
Blink Reformat4c46d092018-04-07 15:32:37 +000040 return _checkWithNodeScript(input_api, output_api, script_path)
41
42
43def _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 Guo75beda92019-10-28 08:29:25 +010053 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +000054 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +000055 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 Stepanyanb532f3f2019-10-22 12:32:59 +010062 ignore_files.append(input_api.os_path.normpath(line.strip()))
Mathias Bynens032591d2019-10-21 11:51:31 +020063 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 Reformat4c46d092018-04-07 15:32:37 +000066 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 Bynens032591d2019-10-21 11:51:31 +020082 eslint_process = popen(
83 [devtools_paths.node_path(), devtools_paths.eslint_path(), '--config', '.eslintrc.js', '--fix'] + affected_files)
Blink Reformat4c46d092018-04-07 15:32:37 +000084 eslint_process.communicate()
85
86 # Need to run clang-format again to align the braces
87 popen(format_args).communicate()
88
89 return [
Yang Guo75beda92019-10-28 08:29:25 +010090 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 Reformat4c46d092018-04-07 15:32:37 +000093 output_api.PresubmitError(format_out),
94 ]
95
96
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -070097def _CheckDevtoolsLocalizableResources(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Mandy Chena6be46a2019-07-09 17:06:27 +000098 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +010099 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'])
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700101 if len(affected_front_end_files) == 0 and check_all_files == False:
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000102 return []
Yang Guo75beda92019-10-28 08:29:25 +0100103 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_localizable_resources.js')
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000104 args = ['--autofix']
Mandy Chena6be46a2019-07-09 17:06:27 +0000105 return _checkWithNodeScript(input_api, output_api, script_path, args)
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000106
107
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700108def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Mandy Chene997da72019-08-22 23:50:19 +0000109 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100110 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'])
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700112 if len(affected_front_end_files) == 0 and check_all_files == False:
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000113 return []
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700114
Yang Guo75beda92019-10-28 08:29:25 +0100115 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_localizability.js')
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700116 if check_all_files == True:
117 return _checkWithNodeScript(input_api, output_api, script_path, ['-a'])
118 else:
119 return _checkWithNodeScript(input_api, output_api, script_path, affected_front_end_files)
Mandy Chen465b4f72019-03-21 22:52:54 +0000120
121
Blink Reformat4c46d092018-04-07 15:32:37 +0000122def _CheckDevtoolsStyle(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +0100123 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'test', 'run_lint_check.py')
Tim van der Lippec85c3122019-09-19 11:27:04 +0000124 process = input_api.subprocess.Popen([input_api.python_executable, lint_path],
125 stdout=input_api.subprocess.PIPE,
126 stderr=input_api.subprocess.STDOUT)
127 out, _ = process.communicate()
128 if process.returncode != 0:
129 return [output_api.PresubmitError(out)]
130 return [output_api.PresubmitNotifyResult(out)]
Blink Reformat4c46d092018-04-07 15:32:37 +0000131
132
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000133def _CheckOptimizeSVGHashes(input_api, output_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000134 if not input_api.platform.startswith('linux'):
135 return []
136
137 original_sys_path = sys.path
138 try:
139 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
140 import devtools_file_hashes
141 finally:
142 sys.path = original_sys_path
143
144 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 08:29:25 +0100145 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
146 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
147 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
148 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37 +0000149 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
150 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
151 if len(invalid_hash_file_paths) == 0:
152 return []
153 invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
Yang Guo75beda92019-10-28 08:29:25 +0100154 file_paths_str = ', '.join(invalid_hash_file_names)
155 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 +0000156 return [output_api.PresubmitError(error_message)]
157
158
159def _CheckCSSViolations(input_api, output_api):
160 results = []
161 for f in input_api.AffectedFiles(include_deletes=False):
Yang Guo75beda92019-10-28 08:29:25 +0100162 if not f.LocalPath().endswith('.css'):
Blink Reformat4c46d092018-04-07 15:32:37 +0000163 continue
164 for line_number, line in f.ChangedContents():
Yang Guo75beda92019-10-28 08:29:25 +0100165 if '/deep/' in line:
166 results.append(output_api.PresubmitError(('%s:%d uses /deep/ selector') % (f.LocalPath(), line_number)))
167 if '::shadow' in line:
168 results.append(output_api.PresubmitError(('%s:%d uses ::shadow selector') % (f.LocalPath(), line_number)))
Blink Reformat4c46d092018-04-07 15:32:37 +0000169 return results
170
Mathias Bynens032591d2019-10-21 11:51:31 +0200171
Yang Guo4fd355c2019-09-19 10:59:03 +0200172def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 11:51:31 +0200173 """Checks common to both upload and commit."""
174 results = []
175 results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
176 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
177 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api))
178 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api))
179 results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200180 return results
181
Blink Reformat4c46d092018-04-07 15:32:37 +0000182
183def CheckChangeOnUpload(input_api, output_api):
184 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200185 results.extend(_CommonChecks(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000186 results.extend(_CheckBuildGN(input_api, output_api))
187 results.extend(_CheckFormat(input_api, output_api))
188 results.extend(_CheckDevtoolsStyle(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000189 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000190 results.extend(_CheckCSSViolations(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700191 results.extend(_CheckDevtoolsLocalizableResources(input_api, output_api))
192 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000193 return results
194
195
196def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000197 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200198 results.extend(_CommonChecks(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700199 results.extend(_CheckDevtoolsLocalizableResources(input_api, output_api, True))
200 results.extend(_CheckDevtoolsLocalization(input_api, output_api, True))
Mathias Bynens032591d2019-10-21 11:51:31 +0200201 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000202 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000203
204
Mandy Chena6be46a2019-07-09 17:06:27 +0000205def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 08:29:25 +0100206 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27 +0000207 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 08:29:25 +0100208 """
Mandy Chena6be46a2019-07-09 17:06:27 +0000209 local_paths = [
210 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
211 ]
212 affected_files = [
213 file_name for file_name in local_paths
214 if any(parent_directory in file_name for parent_directory in parent_directories) and any(
215 file_name.endswith(accepted_ending) for accepted_ending in accepted_endings)
216 ]
217 return affected_files
218
219
Blink Reformat4c46d092018-04-07 15:32:37 +0000220def _getAffectedFrontEndFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000221 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100222 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
223 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files]
225
226
227def _getAffectedJSFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000228 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100229 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
230 devtools_scripts = input_api.os_path.join(devtools_root, 'scripts')
231 affected_js_files = _getAffectedFiles(input_api, [devtools_front_end, devtools_scripts], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000232 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files]
233
234
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000235def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000236 original_sys_path = sys.path
237 try:
Yang Guo75beda92019-10-28 08:29:25 +0100238 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000239 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000240 finally:
241 sys.path = original_sys_path
242
Yang Guod8176982019-10-04 20:30:35 +0000243 node_path = devtools_paths.node_path()
Blink Reformat4c46d092018-04-07 15:32:37 +0000244
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000245 if script_arguments is None:
246 script_arguments = []
Mandy Chen465b4f72019-03-21 22:52:54 +0000247
Blink Reformat4c46d092018-04-07 15:32:37 +0000248 process = input_api.subprocess.Popen(
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000249 [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Blink Reformat4c46d092018-04-07 15:32:37 +0000250 out, _ = process.communicate()
251
252 if process.returncode != 0:
253 return [output_api.PresubmitError(out)]
254 return [output_api.PresubmitNotifyResult(out)]