blob: d92978c1b24f599d983e24fd5cdd28c236126bbb [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
Mathias Bynensa0a6e292019-12-17 13:24:08 +010043# Bypass the AUTHORS check for these accounts.
44_KNOWN_ROBOTS = set('%s-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com' % s for s in ('devtools-ci')) | set(
45 '%s@appspot.gserviceaccount.com' % s for s in ('findit-for-me',))
46
47
Yang Guoa7845d52019-10-31 11:30:23 +010048def _CheckChangesAreExclusiveToDirectory(input_api, output_api):
Tim van der Lippebc42a632019-11-28 14:22:55 +000049 if input_api.change.DISABLE_THIRD_PARTY_CHECK != None:
50 return []
51
Yang Guoa7845d52019-10-31 11:30:23 +010052 def IsParentDir(file, dir):
53 while file != '':
54 if file == dir:
55 return True
56 file = input_api.os_path.dirname(file)
Yang Guoa7845d52019-10-31 11:30:23 +010057 return False
58
59 def FileIsInDir(file, dirs):
60 for dir in dirs:
61 if IsParentDir(file, dir):
62 return True
63
64 affected_files = input_api.LocalPaths()
Yang Guoa7845d52019-10-31 11:30:23 +010065 num_affected = len(affected_files)
66 for dirs in EXCLUSIVE_CHANGE_DIRECTORIES:
Paul Lewis14effba2019-12-02 14:56:40 +000067 dir_list = ', '.join(dirs)
Yang Guoa7845d52019-10-31 11:30:23 +010068 affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files)
69 num_in_dir = len(affected_in_dir)
70 if num_in_dir == 0:
71 continue
Tim van der Lippeebb94a92019-11-19 17:07:53 +000072 # Addition of new third_party folders must have a new entry in `.gitignore`
73 if '.gitignore' in affected_files:
74 num_in_dir = num_in_dir + 1
Yang Guoa7845d52019-10-31 11:30:23 +010075 if num_in_dir < num_affected:
76 return [
Tim van der Lippebc42a632019-11-28 14:22:55 +000077 output_api
Paul Lewis14effba2019-12-02 14:56:40 +000078 .PresubmitError(('CLs that affect files in "%s" should be limited to these files/directories.' % dir_list) +
79 ' You can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message')
Yang Guoa7845d52019-10-31 11:30:23 +010080 ]
81 return []
82
Blink Reformat4c46d092018-04-07 15:32:37 +000083
84def _CheckBuildGN(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +010085 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js')
Blink Reformat4c46d092018-04-07 15:32:37 +000086 return _checkWithNodeScript(input_api, output_api, script_path)
87
88
Tim van der Lippee4bdd742019-12-17 16:40:16 +010089def _CheckJSON(input_api, output_api):
90 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'json_validator', 'validate_module_json.js')
91 return _checkWithNodeScript(input_api, output_api, script_path)
92
93
Blink Reformat4c46d092018-04-07 15:32:37 +000094def _CheckFormat(input_api, output_api):
95
96 def popen(args):
97 return input_api.subprocess.Popen(args=args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
98
99 affected_files = _getAffectedJSFiles(input_api)
100 if len(affected_files) == 0:
101 return []
102 original_sys_path = sys.path
103 try:
Yang Guo75beda92019-10-28 08:29:25 +0100104 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000105 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000106 finally:
107 sys.path = original_sys_path
108
109 ignore_files = []
110 eslint_ignore_path = input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintignore')
111 with open(eslint_ignore_path, 'r') as ignore_manifest:
112 for line in ignore_manifest:
Ingvar Stepanyanb532f3f2019-10-22 12:32:59 +0100113 ignore_files.append(input_api.os_path.normpath(line.strip()))
Mathias Bynens032591d2019-10-21 11:51:31 +0200114 formattable_files = [
115 affected_file for affected_file in affected_files if all(ignore_file not in affected_file for ignore_file in ignore_files)
116 ]
Blink Reformat4c46d092018-04-07 15:32:37 +0000117 if len(formattable_files) == 0:
118 return []
119
120 check_formatting_process = popen(['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files)
121 check_formatting_process.communicate()
122 if check_formatting_process.returncode == 0:
123 return []
124
125 format_args = ['git', 'cl', 'format', '--js'] + formattable_files
126 format_process = popen(format_args)
127 format_out, _ = format_process.communicate()
128 if format_process.returncode != 0:
129 return [output_api.PresubmitError(format_out)]
130
131 # Use eslint to autofix the braces.
132 # Also fix semicolon to avoid confusing clang-format.
Mathias Bynens032591d2019-10-21 11:51:31 +0200133 eslint_process = popen(
134 [devtools_paths.node_path(), devtools_paths.eslint_path(), '--config', '.eslintrc.js', '--fix'] + affected_files)
Blink Reformat4c46d092018-04-07 15:32:37 +0000135 eslint_process.communicate()
136
137 # Need to run clang-format again to align the braces
138 popen(format_args).communicate()
139
140 return [
Yang Guo5a805ab2019-10-31 13:53:28 +0100141 output_api.PresubmitError('ERROR: Found formatting violations.\n'
Yang Guo75beda92019-10-28 08:29:25 +0100142 'Ran clang-format on diff\n'
143 'Use git status to check the formatting changes'),
Blink Reformat4c46d092018-04-07 15:32:37 +0000144 output_api.PresubmitError(format_out),
145 ]
146
147
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700148def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Mandy Chene997da72019-08-22 23:50:19 +0000149 devtools_root = input_api.PresubmitLocalPath()
vidorteg2b675b02019-11-25 09:51:28 -0800150 script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', 'run_localization_check.py')
Paul Lewis954a5a92019-11-20 15:33:49 +0000151 if check_all_files == True:
vidorteg2b675b02019-11-25 09:51:28 -0800152 # Scan all files and fix any errors
vidorteg75c025e2019-11-25 09:52:43 -0800153 args = ['--autofix', '--all']
Paul Lewis954a5a92019-11-20 15:33:49 +0000154 else:
vidorteg2b675b02019-11-25 09:51:28 -0800155 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
156 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'],
157 ['.js', '.grdp', '.grd', 'module.json'])
158
159 if len(affected_front_end_files) == 0:
160 return []
161 # Scan only added or modified files with specific extensions.
162 args = [
163 '--autofix',
164 '--files',
165 ] + affected_front_end_files
166 process = input_api.subprocess.Popen(
167 [input_api.python_executable, script_path] + args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
168 out, _ = process.communicate()
169 if process.returncode != 0:
170 return [output_api.PresubmitError(out)]
171 return [output_api.PresubmitNotifyResult(out)]
Mandy Chen465b4f72019-03-21 22:52:54 +0000172
173
Blink Reformat4c46d092018-04-07 15:32:37 +0000174def _CheckDevtoolsStyle(input_api, output_api):
Yang Guo75beda92019-10-28 08:29:25 +0100175 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 +0000176 process = input_api.subprocess.Popen([input_api.python_executable, lint_path],
177 stdout=input_api.subprocess.PIPE,
178 stderr=input_api.subprocess.STDOUT)
179 out, _ = process.communicate()
180 if process.returncode != 0:
181 return [output_api.PresubmitError(out)]
182 return [output_api.PresubmitNotifyResult(out)]
Blink Reformat4c46d092018-04-07 15:32:37 +0000183
184
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000185def _CheckOptimizeSVGHashes(input_api, output_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000186 if not input_api.platform.startswith('linux'):
187 return []
188
189 original_sys_path = sys.path
190 try:
191 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
192 import devtools_file_hashes
193 finally:
194 sys.path = original_sys_path
195
196 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 08:29:25 +0100197 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
198 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
199 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
200 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37 +0000201 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
202 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
203 if len(invalid_hash_file_paths) == 0:
204 return []
205 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 +0100206 file_paths_str = ', '.join(invalid_hash_file_names)
207 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 +0000208 return [output_api.PresubmitError(error_message)]
209
210
211def _CheckCSSViolations(input_api, output_api):
212 results = []
213 for f in input_api.AffectedFiles(include_deletes=False):
Yang Guo75beda92019-10-28 08:29:25 +0100214 if not f.LocalPath().endswith('.css'):
Blink Reformat4c46d092018-04-07 15:32:37 +0000215 continue
216 for line_number, line in f.ChangedContents():
Yang Guo75beda92019-10-28 08:29:25 +0100217 if '/deep/' in line:
218 results.append(output_api.PresubmitError(('%s:%d uses /deep/ selector') % (f.LocalPath(), line_number)))
219 if '::shadow' in line:
220 results.append(output_api.PresubmitError(('%s:%d uses ::shadow selector') % (f.LocalPath(), line_number)))
Blink Reformat4c46d092018-04-07 15:32:37 +0000221 return results
222
Mathias Bynens032591d2019-10-21 11:51:31 +0200223
Yang Guo4fd355c2019-09-19 10:59:03 +0200224def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 11:51:31 +0200225 """Checks common to both upload and commit."""
226 results = []
Mathias Bynensa0a6e292019-12-17 13:24:08 +0100227 author = input_api.change.author_email
228 if author and author not in _KNOWN_ROBOTS:
229 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200230 results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
231 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
232 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api))
233 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api))
234 results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200235 return results
236
Blink Reformat4c46d092018-04-07 15:32:37 +0000237
238def CheckChangeOnUpload(input_api, output_api):
239 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200240 results.extend(_CommonChecks(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000241 results.extend(_CheckBuildGN(input_api, output_api))
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100242 results.extend(_CheckJSON(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000243 results.extend(_CheckFormat(input_api, output_api))
244 results.extend(_CheckDevtoolsStyle(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000245 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000246 results.extend(_CheckCSSViolations(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700247 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
Yang Guoa7845d52019-10-31 11:30:23 +0100248 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000249 return results
250
251
252def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000253 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200254 results.extend(_CommonChecks(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700255 results.extend(_CheckDevtoolsLocalization(input_api, output_api, True))
Yang Guoa7845d52019-10-31 11:30:23 +0100256 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Mathias Bynens032591d2019-10-21 11:51:31 +0200257 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000258 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000259
260
Mandy Chena6be46a2019-07-09 17:06:27 +0000261def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 08:29:25 +0100262 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27 +0000263 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 08:29:25 +0100264 """
Mandy Chena6be46a2019-07-09 17:06:27 +0000265 local_paths = [
266 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
267 ]
268 affected_files = [
269 file_name for file_name in local_paths
270 if any(parent_directory in file_name for parent_directory in parent_directories) and any(
271 file_name.endswith(accepted_ending) for accepted_ending in accepted_endings)
272 ]
273 return affected_files
274
275
Blink Reformat4c46d092018-04-07 15:32:37 +0000276def _getAffectedFrontEndFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000277 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100278 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
279 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000280 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files]
281
282
283def _getAffectedJSFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000284 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100285 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
286 devtools_scripts = input_api.os_path.join(devtools_root, 'scripts')
287 affected_js_files = _getAffectedFiles(input_api, [devtools_front_end, devtools_scripts], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000288 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files]
289
290
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000291def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000292 original_sys_path = sys.path
293 try:
Yang Guo75beda92019-10-28 08:29:25 +0100294 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000295 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000296 finally:
297 sys.path = original_sys_path
298
Yang Guod8176982019-10-04 20:30:35 +0000299 node_path = devtools_paths.node_path()
Blink Reformat4c46d092018-04-07 15:32:37 +0000300
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000301 if script_arguments is None:
302 script_arguments = []
Mandy Chen465b4f72019-03-21 22:52:54 +0000303
Blink Reformat4c46d092018-04-07 15:32:37 +0000304 process = input_api.subprocess.Popen(
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000305 [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Blink Reformat4c46d092018-04-07 15:32:37 +0000306 out, _ = process.communicate()
307
308 if process.returncode != 0:
309 return [output_api.PresubmitError(out)]
310 return [output_api.PresubmitNotifyResult(out)]