blob: 14baa01fbf6c2b570cc819e0b237f3d191362eda [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
Tim van der Lippef515fdc2020-03-06 16:18:25 +000036import six
Blink Reformat4c46d092018-04-07 15:32:37 +000037
Yang Guoa7845d52019-10-31 11:30:23 +010038EXCLUSIVE_CHANGE_DIRECTORIES = [
39 [ 'third_party', 'v8' ],
40 [ 'node_modules' ],
41 [ 'OWNERS' ],
42]
43
Liviu Raufd2e3212019-12-18 16:38:20 +010044AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com"
Mathias Bynensa0a6e292019-12-17 13:24:08 +010045
Tim van der Lippe4d004ec2020-03-03 18:32:01 +000046
47def _ExecuteSubProcess(input_api, output_api, script_path, args, results):
Tim van der Lippef515fdc2020-03-06 16:18:25 +000048 if isinstance(script_path, six.string_types):
49 script_path = [input_api.python_executable, script_path]
50
51 process = input_api.subprocess.Popen(script_path + args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Tim van der Lippe4d004ec2020-03-03 18:32:01 +000052 out, _ = process.communicate()
53 if process.returncode != 0:
54 results.append(output_api.PresubmitError(out))
55 else:
56 results.append(output_api.PresubmitNotifyResult(out))
57 return results
58
59
Yang Guoa7845d52019-10-31 11:30:23 +010060def _CheckChangesAreExclusiveToDirectory(input_api, output_api):
Tim van der Lippebc42a632019-11-28 14:22:55 +000061 if input_api.change.DISABLE_THIRD_PARTY_CHECK != None:
62 return []
Brandon Goddarde7028672020-01-30 09:31:04 -080063 results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')]
Yang Guoa7845d52019-10-31 11:30:23 +010064 def IsParentDir(file, dir):
65 while file != '':
66 if file == dir:
67 return True
68 file = input_api.os_path.dirname(file)
Yang Guoa7845d52019-10-31 11:30:23 +010069 return False
70
71 def FileIsInDir(file, dirs):
72 for dir in dirs:
73 if IsParentDir(file, dir):
74 return True
75
76 affected_files = input_api.LocalPaths()
Yang Guoa7845d52019-10-31 11:30:23 +010077 num_affected = len(affected_files)
78 for dirs in EXCLUSIVE_CHANGE_DIRECTORIES:
Paul Lewis14effba2019-12-02 14:56:40 +000079 dir_list = ', '.join(dirs)
Yang Guoa7845d52019-10-31 11:30:23 +010080 affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files)
81 num_in_dir = len(affected_in_dir)
82 if num_in_dir == 0:
83 continue
Tim van der Lippeebb94a92019-11-19 17:07:53 +000084 # Addition of new third_party folders must have a new entry in `.gitignore`
85 if '.gitignore' in affected_files:
86 num_in_dir = num_in_dir + 1
Yang Guoa7845d52019-10-31 11:30:23 +010087 if num_in_dir < num_affected:
Brandon Goddarde7028672020-01-30 09:31:04 -080088 results.append(output_api
Paul Lewis14effba2019-12-02 14:56:40 +000089 .PresubmitError(('CLs that affect files in "%s" should be limited to these files/directories.' % dir_list) +
Brandon Goddarde7028672020-01-30 09:31:04 -080090 ' You can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message'))
91 break
92
93 return results
Yang Guoa7845d52019-10-31 11:30:23 +010094
Blink Reformat4c46d092018-04-07 15:32:37 +000095
96def _CheckBuildGN(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -080097 results = [output_api.PresubmitNotifyResult('Running BUILD.GN check:')]
Yang Guo75beda92019-10-28 08:29:25 +010098 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js')
Brandon Goddarde7028672020-01-30 09:31:04 -080099 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
100 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000101
102
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100103def _CheckJSON(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800104 results = [output_api.PresubmitNotifyResult('Running JSON Validator:')]
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100105 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'json_validator', 'validate_module_json.js')
Brandon Goddarde7028672020-01-30 09:31:04 -0800106 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
107 return results
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100108
109
Blink Reformat4c46d092018-04-07 15:32:37 +0000110def _CheckFormat(input_api, output_api):
Tim van der Lippefdbd42e2020-04-07 15:14:36 +0100111 node_modules_affected_files = _getAffectedFiles(input_api, [input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules')], [], [])
112
113 # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs.
114 if (len(node_modules_affected_files) > 0):
115 return [output_api.PresubmitNotifyResult('Skipping Format Checks because `node_modules` files are affected.')]
116
Brandon Goddarde7028672020-01-30 09:31:04 -0800117 results = [output_api.PresubmitNotifyResult('Running Format Checks:')]
Blink Reformat4c46d092018-04-07 15:32:37 +0000118
Tim van der Lippef515fdc2020-03-06 16:18:25 +0000119 return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results)
Blink Reformat4c46d092018-04-07 15:32:37 +0000120
121
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700122def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Brandon Goddarde7028672020-01-30 09:31:04 -0800123 results = [output_api.PresubmitNotifyResult('Running Localization Checks:')]
Mandy Chene997da72019-08-22 23:50:19 +0000124 devtools_root = input_api.PresubmitLocalPath()
vidorteg2b675b02019-11-25 09:51:28 -0800125 script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', 'run_localization_check.py')
Paul Lewis954a5a92019-11-20 15:33:49 +0000126 if check_all_files == True:
vidorteg2b675b02019-11-25 09:51:28 -0800127 # Scan all files and fix any errors
vidorteg75c025e2019-11-25 09:52:43 -0800128 args = ['--autofix', '--all']
Paul Lewis954a5a92019-11-20 15:33:49 +0000129 else:
vidorteg2b675b02019-11-25 09:51:28 -0800130 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
131 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'],
132 ['.js', '.grdp', '.grd', 'module.json'])
133
134 if len(affected_front_end_files) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800135 return results
Christy Chen1ab87e02020-01-30 16:32:16 -0800136
137 with input_api.CreateTemporaryFile() as file_list:
138 for affected_file in affected_front_end_files:
139 file_list.write(affected_file + '\n')
140 file_list.close()
141
vidorteg2b675b02019-11-25 09:51:28 -0800142 # Scan only added or modified files with specific extensions.
Christy Chen1ab87e02020-01-30 16:32:16 -0800143 args = ['--autofix', '--file-list', file_list.name]
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000144
145 return _ExecuteSubProcess(input_api, output_api, script_path, args, results)
Mandy Chen465b4f72019-03-21 22:52:54 +0000146
147
Blink Reformat4c46d092018-04-07 15:32:37 +0000148def _CheckDevtoolsStyle(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800149 results = [output_api.PresubmitNotifyResult('Running Devtools Style Check:')]
Tim van der Lippe98132242020-04-14 17:16:54 +0100150 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'test', 'run_lint_check.js')
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000151
Tim van der Lippe2a4ae2b2020-03-11 17:28:06 +0000152 front_end_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end')
153 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), 'test')
154 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')
155
156 default_linted_directories = [front_end_directory, test_directory, scripts_directory]
157
158 eslint_related_files = [
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +0100159 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', 'eslint'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06 +0000160 input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'),
161 input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintignore'),
162 input_api.os_path.join(scripts_directory, 'test', 'run_lint_check.py'),
Tim van der Lippe98132242020-04-14 17:16:54 +0100163 input_api.os_path.join(scripts_directory, 'test', 'run_lint_check.js'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06 +0000164 input_api.os_path.join(scripts_directory, '.eslintrc.js'),
165 input_api.os_path.join(scripts_directory, 'eslint_rules'),
166 ]
167
168 affected_files = _getAffectedFiles(input_api, eslint_related_files, [], ['.js', '.py', '.eslintignore'])
169
170 # We are changing the ESLint configuration, make sure to run the full check
171 if len(affected_files) is not 0:
172 results.append(output_api.PresubmitNotifyResult('Running full ESLint check'))
173 affected_files = default_linted_directories
174 else:
175 # Only run ESLint on files that are relevant, to save PRESUBMIT time
176 affected_files = _getAffectedFiles(input_api, default_linted_directories, ['D'], ['.js', '.ts'])
177
178 # If we have not changed any lintable files, then we should bail out.
179 # Otherwise, `run_lint_check.py` will lint *all* files.
180 if len(affected_files) is 0:
181 results.append(output_api.PresubmitNotifyResult('No affected files for ESLint check'))
182 return results
183
Tim van der Lippe98132242020-04-14 17:16:54 +0100184 results.extend(_checkWithNodeScript(input_api, output_api, lint_path, affected_files))
185 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000186
187
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000188def _CheckOptimizeSVGHashes(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800189 results = [output_api.PresubmitNotifyResult('Running SVG Optimization Check:')]
Blink Reformat4c46d092018-04-07 15:32:37 +0000190 if not input_api.platform.startswith('linux'):
Brandon Goddarde7028672020-01-30 09:31:04 -0800191 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000192
193 original_sys_path = sys.path
194 try:
195 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
196 import devtools_file_hashes
197 finally:
198 sys.path = original_sys_path
199
200 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 08:29:25 +0100201 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
202 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
203 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
204 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37 +0000205 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
206 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
207 if len(invalid_hash_file_paths) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800208 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000209 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 +0100210 file_paths_str = ', '.join(invalid_hash_file_names)
211 error_message = 'The following SVG files should be optimized using optimize_svg_images script before uploading: \n - %s' % file_paths_str
Brandon Goddarde7028672020-01-30 09:31:04 -0800212 results.append(output_api.PresubmitError(error_message))
213 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000214
215
Mathias Bynens032591d2019-10-21 11:51:31 +0200216
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000217def _CheckGeneratedFiles(input_api, output_api):
Tim van der Lippeb3b90762020-03-04 15:21:52 +0000218 v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8')
219 blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink')
220 protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol')
221 scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')
Tim van der Lippe5d2d79b2020-03-23 11:45:04 +0000222 scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated')
Tim van der Lippeb3b90762020-03-04 15:21:52 +0000223
224 generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py')
225 generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py')
226 generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py')
227 concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol',
228 'concatenate_protocols.py')
229
230 affected_files = _getAffectedFiles(input_api, [
231 v8_directory_path,
232 blink_directory_path,
233 input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'pyjson5'),
234 generated_aria_path,
235 generated_supported_css_path,
236 concatenate_protocols_path,
237 generated_protocol_path,
Tim van der Lippe5d2d79b2020-03-23 11:45:04 +0000238 scripts_generated_output_path,
239 ], [], ['.pdl', '.json5', '.py', '.js'])
Tim van der Lippeb3b90762020-03-04 15:21:52 +0000240
241 if len(affected_files) == 0:
242 return []
243
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000244 results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')]
Tim van der Lippeb0d65f12020-03-05 12:15:24 +0000245 generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps',
246 'generate_protocol_resources.py')
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000247
Tim van der Lippeb0d65f12020-03-05 12:15:24 +0000248 return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results)
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000249
250
Tim van der Lippe5279f842020-01-14 16:26:38 +0000251def _CheckNoUncheckedFiles(input_api, output_api):
252 results = []
253 process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'],
254 stdout=input_api.subprocess.PIPE,
255 stderr=input_api.subprocess.STDOUT)
256 out, _ = process.communicate()
257 if process.returncode != 0:
Tim van der Lippe9bb1cf62020-03-06 16:17:02 +0000258 files_changed_process = input_api.subprocess.Popen(['git', 'diff', '--name-only'],
259 stdout=input_api.subprocess.PIPE,
260 stderr=input_api.subprocess.STDOUT)
261 files_changed, _ = files_changed_process.communicate()
262
263 return [
264 output_api.PresubmitError('You have changed files that need to be committed:'),
265 output_api.PresubmitError(files_changed)
266 ]
Tim van der Lippe5279f842020-01-14 16:26:38 +0000267 return []
268
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000269def _CheckForTooLargeFiles(input_api, output_api):
Christy Chen1ab87e02020-01-30 16:32:16 -0800270 """Avoid large files, especially binary files, in the repository since
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000271 git doesn't scale well for those. They will be in everyone's repo
272 clones forever, forever making Chromium slower to clone and work
273 with."""
Christy Chen1ab87e02020-01-30 16:32:16 -0800274 # Uploading files to cloud storage is not trivial so we don't want
275 # to set the limit too low, but the upper limit for "normal" large
276 # files seems to be 1-2 MB, with a handful around 5-8 MB, so
277 # anything over 20 MB is exceptional.
278 TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
279 too_large_files = []
280 for f in input_api.AffectedFiles():
281 # Check both added and modified files (but not deleted files).
282 if f.Action() in ('A', 'M'):
283 size = input_api.os_path.getsize(f.AbsoluteLocalPath())
284 if size > TOO_LARGE_FILE_SIZE_LIMIT:
285 too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
286 if too_large_files:
287 message = (
288 'Do not commit large files to git since git scales badly for those.\n' +
289 'Instead put the large files in cloud storage and use DEPS to\n' +
290 'fetch them.\n' + '\n'.join(too_large_files)
291 )
292 return [output_api.PresubmitError(
293 'Too large files found in commit', long_text=message + '\n')]
294 else:
295 return []
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000296
Tim van der Lippe5279f842020-01-14 16:26:38 +0000297
Yang Guo4fd355c2019-09-19 10:59:03 +0200298def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 11:51:31 +0200299 """Checks common to both upload and commit."""
300 results = []
Liviu Raufd2e3212019-12-18 16:38:20 +0100301 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(input_api, output_api,
302 bot_whitelist=[AUTOROLL_ACCOUNT]
303 ))
Mathias Bynens032591d2019-10-21 11:51:31 +0200304 results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
305 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
306 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api))
307 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api))
308 results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000309 results.extend(_CheckBuildGN(input_api, output_api))
Tim van der Lippe4d004ec2020-03-03 18:32:01 +0000310 results.extend(_CheckGeneratedFiles(input_api, output_api))
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100311 results.extend(_CheckJSON(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000312 results.extend(_CheckDevtoolsStyle(input_api, output_api))
Tim van der Lippe5497d482020-01-14 15:27:30 +0000313 results.extend(_CheckFormat(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000314 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Yang Guoa7845d52019-10-31 11:30:23 +0100315 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Tim van der Lippe5279f842020-01-14 16:26:38 +0000316 results.extend(_CheckNoUncheckedFiles(input_api, output_api))
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000317 results.extend(_CheckForTooLargeFiles(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000318 return results
319
320
Liviu Raud614e092020-01-08 10:56:33 +0100321def CheckChangeOnUpload(input_api, output_api):
322 results = []
323 results.extend(_CommonChecks(input_api, output_api))
324 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
325 return results
326
327
Blink Reformat4c46d092018-04-07 15:32:37 +0000328def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000329 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200330 results.extend(_CommonChecks(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700331 results.extend(_CheckDevtoolsLocalization(input_api, output_api, True))
Mathias Bynens032591d2019-10-21 11:51:31 +0200332 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000333 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000334
335
Mandy Chena6be46a2019-07-09 17:06:27 +0000336def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 08:29:25 +0100337 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27 +0000338 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 08:29:25 +0100339 """
Mandy Chena6be46a2019-07-09 17:06:27 +0000340 local_paths = [
341 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
342 ]
343 affected_files = [
Tim van der Lippefdbd42e2020-04-07 15:14:36 +0100344 file_name for file_name in local_paths if any(parent_directory in file_name for parent_directory in parent_directories) and
345 (len(accepted_endings) is 0 or any(file_name.endswith(accepted_ending) for accepted_ending in accepted_endings))
Mandy Chena6be46a2019-07-09 17:06:27 +0000346 ]
347 return affected_files
348
349
Tim van der Lippec4617122020-03-06 16:24:19 +0000350def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000351 original_sys_path = sys.path
352 try:
Yang Guo75beda92019-10-28 08:29:25 +0100353 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000354 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000355 finally:
356 sys.path = original_sys_path
357
Tim van der Lippec4617122020-03-06 16:24:19 +0000358 return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, [])