blob: b6e8602e5ab82d61c90c3b47101767f150f8f24a [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
Yang Guoa7845d52019-10-31 11:30:23 +010037EXCLUSIVE_CHANGE_DIRECTORIES = [
38 [ 'third_party', 'v8' ],
39 [ 'node_modules' ],
40 [ 'OWNERS' ],
41]
42
43def _CheckChangesAreExclusiveToDirectory(input_api, output_api):
44 def IsParentDir(file, dir):
45 while file != '':
46 if file == dir:
47 return True
48 file = input_api.os_path.dirname(file)
Yang Guoa7845d52019-10-31 11:30:23 +010049 return False
50
51 def FileIsInDir(file, dirs):
52 for dir in dirs:
53 if IsParentDir(file, dir):
54 return True
55
56 affected_files = input_api.LocalPaths()
Yang Guoa7845d52019-10-31 11:30:23 +010057 num_affected = len(affected_files)
58 for dirs in EXCLUSIVE_CHANGE_DIRECTORIES:
59 affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files)
60 num_in_dir = len(affected_in_dir)
61 if num_in_dir == 0:
62 continue
63 if num_in_dir < num_affected:
64 return [
65 output_api.PresubmitError(
66 'CLs that affect files in "%s" should be limited to these files/directories.' % ', '.join(dirs))
67 ]
68 return []
69
Blink Reformat4c46d092018-04-07 15:32:37 +000070
71def _CheckBuildGN(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +010072 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js')
Blink Reformat4c46d092018-04-07 15:32:37 +000073 return _checkWithNodeScript(input_api, output_api, script_path)
74
75
76def _CheckFormat(input_api, output_api):
77
78 def popen(args):
79 return input_api.subprocess.Popen(args=args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
80
81 affected_files = _getAffectedJSFiles(input_api)
82 if len(affected_files) == 0:
83 return []
84 original_sys_path = sys.path
85 try:
Yang Guo75beda92019-10-28 08:29:25 +010086 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +000087 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +000088 finally:
89 sys.path = original_sys_path
90
91 ignore_files = []
92 eslint_ignore_path = input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintignore')
93 with open(eslint_ignore_path, 'r') as ignore_manifest:
94 for line in ignore_manifest:
Ingvar Stepanyanb532f3f2019-10-22 12:32:59 +010095 ignore_files.append(input_api.os_path.normpath(line.strip()))
Mathias Bynens032591d2019-10-21 11:51:31 +020096 formattable_files = [
97 affected_file for affected_file in affected_files if all(ignore_file not in affected_file for ignore_file in ignore_files)
98 ]
Blink Reformat4c46d092018-04-07 15:32:37 +000099 if len(formattable_files) == 0:
100 return []
101
102 check_formatting_process = popen(['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files)
103 check_formatting_process.communicate()
104 if check_formatting_process.returncode == 0:
105 return []
106
107 format_args = ['git', 'cl', 'format', '--js'] + formattable_files
108 format_process = popen(format_args)
109 format_out, _ = format_process.communicate()
110 if format_process.returncode != 0:
111 return [output_api.PresubmitError(format_out)]
112
113 # Use eslint to autofix the braces.
114 # Also fix semicolon to avoid confusing clang-format.
Mathias Bynens032591d2019-10-21 11:51:31 +0200115 eslint_process = popen(
116 [devtools_paths.node_path(), devtools_paths.eslint_path(), '--config', '.eslintrc.js', '--fix'] + affected_files)
Blink Reformat4c46d092018-04-07 15:32:37 +0000117 eslint_process.communicate()
118
119 # Need to run clang-format again to align the braces
120 popen(format_args).communicate()
121
122 return [
Yang Guo5a805ab2019-10-31 13:53:28 +0100123 output_api.PresubmitError('ERROR: Found formatting violations.\n'
Yang Guo75beda92019-10-28 08:29:25 +0100124 'Ran clang-format on diff\n'
125 'Use git status to check the formatting changes'),
Blink Reformat4c46d092018-04-07 15:32:37 +0000126 output_api.PresubmitError(format_out),
127 ]
128
129
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700130def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Mandy Chene997da72019-08-22 23:50:19 +0000131 devtools_root = input_api.PresubmitLocalPath()
e52a82bdfb5106bd658c2c5ea465e200402078e2019-11-08 10:42:01 -0800132 script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', 'run_localization_check.py')
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700133 if check_all_files == True:
e52a82bdfb5106bd658c2c5ea465e200402078e2019-11-08 10:42:01 -0800134 # Scan all files and fix any errors
135 args = ['--autofix', '--a']
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700136 else:
e52a82bdfb5106bd658c2c5ea465e200402078e2019-11-08 10:42:01 -0800137 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
138 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'],
139 ['.js', '.grdp', '.grd', 'module.json'])
140
141 if len(affected_front_end_files) == 0:
142 return []
143 # Scan only added or modified files with specific extensions.
144 args = [
145 '--autofix',
146 '--files',
147 ] + affected_front_end_files
148 process = input_api.subprocess.Popen(
149 [input_api.python_executable, script_path] + args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
150 out, _ = process.communicate()
151 if process.returncode != 0:
152 return [output_api.PresubmitError(out)]
153 return [output_api.PresubmitNotifyResult(out)]
Mandy Chen465b4f72019-03-21 22:52:54 +0000154
155
Blink Reformat4c46d092018-04-07 15:32:37 +0000156def _CheckDevtoolsStyle(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +0100157 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 +0000158 process = input_api.subprocess.Popen([input_api.python_executable, lint_path],
159 stdout=input_api.subprocess.PIPE,
160 stderr=input_api.subprocess.STDOUT)
161 out, _ = process.communicate()
162 if process.returncode != 0:
163 return [output_api.PresubmitError(out)]
164 return [output_api.PresubmitNotifyResult(out)]
Blink Reformat4c46d092018-04-07 15:32:37 +0000165
166
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000167def _CheckOptimizeSVGHashes(input_api, output_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000168 if not input_api.platform.startswith('linux'):
169 return []
170
171 original_sys_path = sys.path
172 try:
173 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
174 import devtools_file_hashes
175 finally:
176 sys.path = original_sys_path
177
178 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 08:29:25 +0100179 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
180 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
181 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
182 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37 +0000183 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
184 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
185 if len(invalid_hash_file_paths) == 0:
186 return []
187 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 +0100188 file_paths_str = ', '.join(invalid_hash_file_names)
189 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 +0000190 return [output_api.PresubmitError(error_message)]
191
192
193def _CheckCSSViolations(input_api, output_api):
194 results = []
195 for f in input_api.AffectedFiles(include_deletes=False):
Yang Guo75beda92019-10-28 08:29:25 +0100196 if not f.LocalPath().endswith('.css'):
Blink Reformat4c46d092018-04-07 15:32:37 +0000197 continue
198 for line_number, line in f.ChangedContents():
Yang Guo75beda92019-10-28 08:29:25 +0100199 if '/deep/' in line:
200 results.append(output_api.PresubmitError(('%s:%d uses /deep/ selector') % (f.LocalPath(), line_number)))
201 if '::shadow' in line:
202 results.append(output_api.PresubmitError(('%s:%d uses ::shadow selector') % (f.LocalPath(), line_number)))
Blink Reformat4c46d092018-04-07 15:32:37 +0000203 return results
204
Mathias Bynens032591d2019-10-21 11:51:31 +0200205
Yang Guo4fd355c2019-09-19 10:59:03 +0200206def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 11:51:31 +0200207 """Checks common to both upload and commit."""
208 results = []
209 results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
210 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
211 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api))
212 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api))
213 results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200214 return results
215
Blink Reformat4c46d092018-04-07 15:32:37 +0000216
217def CheckChangeOnUpload(input_api, output_api):
218 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200219 results.extend(_CommonChecks(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000220 results.extend(_CheckBuildGN(input_api, output_api))
221 results.extend(_CheckFormat(input_api, output_api))
222 results.extend(_CheckDevtoolsStyle(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000223 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 results.extend(_CheckCSSViolations(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700225 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
Yang Guoa7845d52019-10-31 11:30:23 +0100226 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000227 return results
228
229
230def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000231 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200232 results.extend(_CommonChecks(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700233 results.extend(_CheckDevtoolsLocalization(input_api, output_api, True))
Yang Guoa7845d52019-10-31 11:30:23 +0100234 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200235 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000236 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000237
238
Mandy Chena6be46a2019-07-09 17:06:27 +0000239def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 08:29:25 +0100240 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27 +0000241 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 08:29:25 +0100242 """
Mandy Chena6be46a2019-07-09 17:06:27 +0000243 local_paths = [
244 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
245 ]
246 affected_files = [
247 file_name for file_name in local_paths
248 if any(parent_directory in file_name for parent_directory in parent_directories) and any(
249 file_name.endswith(accepted_ending) for accepted_ending in accepted_endings)
250 ]
251 return affected_files
252
253
Blink Reformat4c46d092018-04-07 15:32:37 +0000254def _getAffectedFrontEndFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000255 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100256 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
257 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000258 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files]
259
260
261def _getAffectedJSFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000262 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100263 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
264 devtools_scripts = input_api.os_path.join(devtools_root, 'scripts')
265 affected_js_files = _getAffectedFiles(input_api, [devtools_front_end, devtools_scripts], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000266 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files]
267
268
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000269def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000270 original_sys_path = sys.path
271 try:
Yang Guo75beda92019-10-28 08:29:25 +0100272 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000273 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000274 finally:
275 sys.path = original_sys_path
276
Yang Guod8176982019-10-04 20:30:35 +0000277 node_path = devtools_paths.node_path()
Blink Reformat4c46d092018-04-07 15:32:37 +0000278
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000279 if script_arguments is None:
280 script_arguments = []
Mandy Chen465b4f72019-03-21 22:52:54 +0000281
Blink Reformat4c46d092018-04-07 15:32:37 +0000282 process = input_api.subprocess.Popen(
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000283 [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Blink Reformat4c46d092018-04-07 15:32:37 +0000284 out, _ = process.communicate()
285
286 if process.returncode != 0:
287 return [output_api.PresubmitError(out)]
288 return [output_api.PresubmitNotifyResult(out)]