blob: 6eb024134e9bd949178cfc8c65f9acda8d85f65c [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
Liviu Raufd2e3212019-12-18 16:38:20 +010043AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com"
Mathias Bynensa0a6e292019-12-17 13:24:08 +010044
Yang Guoa7845d52019-10-31 11:30:23 +010045def _CheckChangesAreExclusiveToDirectory(input_api, output_api):
Tim van der Lippebc42a632019-11-28 14:22:55 +000046 if input_api.change.DISABLE_THIRD_PARTY_CHECK != None:
47 return []
Brandon Goddarde7028672020-01-30 09:31:04 -080048 results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')]
Yang Guoa7845d52019-10-31 11:30:23 +010049 def IsParentDir(file, dir):
50 while file != '':
51 if file == dir:
52 return True
53 file = input_api.os_path.dirname(file)
Yang Guoa7845d52019-10-31 11:30:23 +010054 return False
55
56 def FileIsInDir(file, dirs):
57 for dir in dirs:
58 if IsParentDir(file, dir):
59 return True
60
61 affected_files = input_api.LocalPaths()
Yang Guoa7845d52019-10-31 11:30:23 +010062 num_affected = len(affected_files)
63 for dirs in EXCLUSIVE_CHANGE_DIRECTORIES:
Paul Lewis14effba2019-12-02 14:56:40 +000064 dir_list = ', '.join(dirs)
Yang Guoa7845d52019-10-31 11:30:23 +010065 affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files)
66 num_in_dir = len(affected_in_dir)
67 if num_in_dir == 0:
68 continue
Tim van der Lippeebb94a92019-11-19 17:07:53 +000069 # Addition of new third_party folders must have a new entry in `.gitignore`
70 if '.gitignore' in affected_files:
71 num_in_dir = num_in_dir + 1
Yang Guoa7845d52019-10-31 11:30:23 +010072 if num_in_dir < num_affected:
Brandon Goddarde7028672020-01-30 09:31:04 -080073 results.append(output_api
Paul Lewis14effba2019-12-02 14:56:40 +000074 .PresubmitError(('CLs that affect files in "%s" should be limited to these files/directories.' % dir_list) +
Brandon Goddarde7028672020-01-30 09:31:04 -080075 ' You can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message'))
76 break
77
78 return results
Yang Guoa7845d52019-10-31 11:30:23 +010079
Blink Reformat4c46d092018-04-07 15:32:37 +000080
81def _CheckBuildGN(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -080082 results = [output_api.PresubmitNotifyResult('Running BUILD.GN check:')]
Yang Guo75beda92019-10-28 08:29:25 +010083 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'check_gn.js')
Brandon Goddarde7028672020-01-30 09:31:04 -080084 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
85 return results
Blink Reformat4c46d092018-04-07 15:32:37 +000086
87
Tim van der Lippee4bdd742019-12-17 16:40:16 +010088def _CheckJSON(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -080089 results = [output_api.PresubmitNotifyResult('Running JSON Validator:')]
Tim van der Lippee4bdd742019-12-17 16:40:16 +010090 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'json_validator', 'validate_module_json.js')
Brandon Goddarde7028672020-01-30 09:31:04 -080091 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
92 return results
Tim van der Lippee4bdd742019-12-17 16:40:16 +010093
94
Brandon Goddard53faba12020-01-24 16:55:04 +000095def _CheckLicenses(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -080096 results = [output_api.PresubmitNotifyResult('Running License Header Check:')]
Brandon Goddard53faba12020-01-24 16:55:04 +000097 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'test', 'run_license_header_check.js')
Brandon Goddarde7028672020-01-30 09:31:04 -080098 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
99 return results
Brandon Goddard53faba12020-01-24 16:55:04 +0000100
101
Paul Lewis21fb3492020-01-14 15:28:22 +0000102def _CheckUnitTests(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800103 results = [output_api.PresubmitNotifyResult('Running Unit Tests:')]
Paul Lewis21fb3492020-01-14 15:28:22 +0000104 unittest_root = input_api.os_path.join(input_api.PresubmitLocalPath(), 'test')
105 affected_unittest_files = _getAffectedFiles(input_api, [unittest_root], ['D'], ['.ts'])
106 if len(affected_unittest_files) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800107 return results
Paul Lewis21fb3492020-01-14 15:28:22 +0000108
109 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'test', 'check_for_unittest_onlys.js')
Brandon Goddarde7028672020-01-30 09:31:04 -0800110 results.extend(_checkWithNodeScript(input_api, output_api, script_path, affected_unittest_files))
111 return results
Paul Lewis21fb3492020-01-14 15:28:22 +0000112
113
Blink Reformat4c46d092018-04-07 15:32:37 +0000114def _CheckFormat(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800115 results = [output_api.PresubmitNotifyResult('Running Format Checks:')]
Blink Reformat4c46d092018-04-07 15:32:37 +0000116 def popen(args):
117 return input_api.subprocess.Popen(args=args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
118
119 affected_files = _getAffectedJSFiles(input_api)
120 if len(affected_files) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800121 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000122 original_sys_path = sys.path
123 try:
Yang Guo75beda92019-10-28 08:29:25 +0100124 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000125 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000126 finally:
127 sys.path = original_sys_path
128
129 ignore_files = []
130 eslint_ignore_path = input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintignore')
131 with open(eslint_ignore_path, 'r') as ignore_manifest:
132 for line in ignore_manifest:
Ingvar Stepanyanb532f3f2019-10-22 12:32:59 +0100133 ignore_files.append(input_api.os_path.normpath(line.strip()))
Mathias Bynens032591d2019-10-21 11:51:31 +0200134 formattable_files = [
135 affected_file for affected_file in affected_files if all(ignore_file not in affected_file for ignore_file in ignore_files)
136 ]
Blink Reformat4c46d092018-04-07 15:32:37 +0000137 if len(formattable_files) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800138 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000139
140 check_formatting_process = popen(['git', 'cl', 'format', '--js', '--dry-run'] + formattable_files)
141 check_formatting_process.communicate()
142 if check_formatting_process.returncode == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800143 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000144
145 format_args = ['git', 'cl', 'format', '--js'] + formattable_files
146 format_process = popen(format_args)
147 format_out, _ = format_process.communicate()
148 if format_process.returncode != 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800149 results.append(output_api.PresubmitError(format_out))
150 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000151
Brandon Goddarde7028672020-01-30 09:31:04 -0800152 results.append(output_api.PresubmitError('ERROR: Found formatting violations.\n'
Yang Guo75beda92019-10-28 08:29:25 +0100153 'Ran clang-format on diff\n'
Brandon Goddarde7028672020-01-30 09:31:04 -0800154 'Use git status to check the formatting changes'))
155 results.append(output_api.PresubmitError(format_out))
156 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000157
158
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700159def _CheckDevtoolsLocalization(input_api, output_api, check_all_files=False): # pylint: disable=invalid-name
Brandon Goddarde7028672020-01-30 09:31:04 -0800160 results = [output_api.PresubmitNotifyResult('Running Localization Checks:')]
Mandy Chene997da72019-08-22 23:50:19 +0000161 devtools_root = input_api.PresubmitLocalPath()
vidorteg2b675b02019-11-25 09:51:28 -0800162 script_path = input_api.os_path.join(devtools_root, 'scripts', 'test', 'run_localization_check.py')
Paul Lewis954a5a92019-11-20 15:33:49 +0000163 if check_all_files == True:
vidorteg2b675b02019-11-25 09:51:28 -0800164 # Scan all files and fix any errors
vidorteg75c025e2019-11-25 09:52:43 -0800165 args = ['--autofix', '--all']
Paul Lewis954a5a92019-11-20 15:33:49 +0000166 else:
vidorteg2b675b02019-11-25 09:51:28 -0800167 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
168 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'],
169 ['.js', '.grdp', '.grd', 'module.json'])
170
171 if len(affected_front_end_files) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800172 return results
vidorteg2b675b02019-11-25 09:51:28 -0800173 # Scan only added or modified files with specific extensions.
174 args = [
175 '--autofix',
176 '--files',
177 ] + affected_front_end_files
178 process = input_api.subprocess.Popen(
179 [input_api.python_executable, script_path] + args, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
180 out, _ = process.communicate()
181 if process.returncode != 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800182 results.append(output_api.PresubmitError(out))
183 else:
184 results.append(output_api.PresubmitNotifyResult(out))
185 return results
Mandy Chen465b4f72019-03-21 22:52:54 +0000186
187
Blink Reformat4c46d092018-04-07 15:32:37 +0000188def _CheckDevtoolsStyle(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800189 results = [output_api.PresubmitNotifyResult('Running Devtools Style Check:')]
Yang Guo75beda92019-10-28 08:29:25 +0100190 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 +0000191 process = input_api.subprocess.Popen([input_api.python_executable, lint_path],
192 stdout=input_api.subprocess.PIPE,
193 stderr=input_api.subprocess.STDOUT)
194 out, _ = process.communicate()
195 if process.returncode != 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800196 results.append(output_api.PresubmitError(out))
197 else:
198 results.append(output_api.PresubmitNotifyResult(out))
199 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000200
201
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000202def _CheckOptimizeSVGHashes(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800203 results = [output_api.PresubmitNotifyResult('Running SVG Optimization Check:')]
Blink Reformat4c46d092018-04-07 15:32:37 +0000204 if not input_api.platform.startswith('linux'):
Brandon Goddarde7028672020-01-30 09:31:04 -0800205 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000206
207 original_sys_path = sys.path
208 try:
209 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
210 import devtools_file_hashes
211 finally:
212 sys.path = original_sys_path
213
214 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 08:29:25 +0100215 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
216 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
217 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
218 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37 +0000219 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
220 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
221 if len(invalid_hash_file_paths) == 0:
Brandon Goddarde7028672020-01-30 09:31:04 -0800222 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000223 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 +0100224 file_paths_str = ', '.join(invalid_hash_file_names)
225 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 -0800226 results.append(output_api.PresubmitError(error_message))
227 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000228
229
230def _CheckCSSViolations(input_api, output_api):
Brandon Goddarde7028672020-01-30 09:31:04 -0800231 results = [output_api.PresubmitNotifyResult('Running CSS Violation Check:')]
Blink Reformat4c46d092018-04-07 15:32:37 +0000232 for f in input_api.AffectedFiles(include_deletes=False):
Yang Guo75beda92019-10-28 08:29:25 +0100233 if not f.LocalPath().endswith('.css'):
Blink Reformat4c46d092018-04-07 15:32:37 +0000234 continue
235 for line_number, line in f.ChangedContents():
Yang Guo75beda92019-10-28 08:29:25 +0100236 if '/deep/' in line:
237 results.append(output_api.PresubmitError(('%s:%d uses /deep/ selector') % (f.LocalPath(), line_number)))
238 if '::shadow' in line:
239 results.append(output_api.PresubmitError(('%s:%d uses ::shadow selector') % (f.LocalPath(), line_number)))
Blink Reformat4c46d092018-04-07 15:32:37 +0000240 return results
241
Mathias Bynens032591d2019-10-21 11:51:31 +0200242
Tim van der Lippe5279f842020-01-14 16:26:38 +0000243def _CheckNoUncheckedFiles(input_api, output_api):
244 results = []
245 process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'],
246 stdout=input_api.subprocess.PIPE,
247 stderr=input_api.subprocess.STDOUT)
248 out, _ = process.communicate()
249 if process.returncode != 0:
250 return [output_api.PresubmitError('You have changed files that need to be committed.')]
251 return []
252
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000253def _CheckForTooLargeFiles(input_api, output_api):
254 """Avoid large files, especially binary files, in the repository since
255 git doesn't scale well for those. They will be in everyone's repo
256 clones forever, forever making Chromium slower to clone and work
257 with."""
258 # Uploading files to cloud storage is not trivial so we don't want
259 # to set the limit too low, but the upper limit for "normal" large
260 # files seems to be 1-2 MB, with a handful around 5-8 MB, so
261 # anything over 20 MB is exceptional.
262 TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
263 too_large_files = []
264 for f in input_api.AffectedFiles():
265 # Check both added and modified files (but not deleted files).
266 if f.Action() in ('A', 'M'):
267 size = input_api.os_path.getsize(f.AbsoluteLocalPath())
268 if size > TOO_LARGE_FILE_SIZE_LIMIT:
269 too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
270 if too_large_files:
271 message = (
272 'Do not commit large files to git since git scales badly for those.\n' +
273 'Instead put the large files in cloud storage and use DEPS to\n' +
274 'fetch them.\n' + '\n'.join(too_large_files)
275 )
276 return [output_api.PresubmitError(
277 'Too large files found in commit', long_text=message + '\n')]
278 else:
279 return []
280
Tim van der Lippe5279f842020-01-14 16:26:38 +0000281
Yang Guo4fd355c2019-09-19 10:59:03 +0200282def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 11:51:31 +0200283 """Checks common to both upload and commit."""
284 results = []
Liviu Raufd2e3212019-12-18 16:38:20 +0100285 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(input_api, output_api,
286 bot_whitelist=[AUTOROLL_ACCOUNT]
287 ))
Mathias Bynens032591d2019-10-21 11:51:31 +0200288 results.extend(input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
289 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
290 results.extend(input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api))
291 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(input_api, output_api))
292 results.extend(input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000293 results.extend(_CheckBuildGN(input_api, output_api))
Tim van der Lippee4bdd742019-12-17 16:40:16 +0100294 results.extend(_CheckJSON(input_api, output_api))
Brandon Goddard53faba12020-01-24 16:55:04 +0000295 results.extend(_CheckLicenses(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000296 results.extend(_CheckDevtoolsStyle(input_api, output_api))
Tim van der Lippe5497d482020-01-14 15:27:30 +0000297 results.extend(_CheckFormat(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12 +0000298 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000299 results.extend(_CheckCSSViolations(input_api, output_api))
Yang Guoa7845d52019-10-31 11:30:23 +0100300 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Paul Lewis21fb3492020-01-14 15:28:22 +0000301 results.extend(_CheckUnitTests(input_api, output_api))
Tim van der Lippe5279f842020-01-14 16:26:38 +0000302 results.extend(_CheckNoUncheckedFiles(input_api, output_api))
Tim van der Lippe8fdda112020-01-27 11:27:06 +0000303 results.extend(_CheckForTooLargeFiles(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37 +0000304 return results
305
306
Liviu Raud614e092020-01-08 10:56:33 +0100307def CheckChangeOnUpload(input_api, output_api):
308 results = []
309 results.extend(_CommonChecks(input_api, output_api))
310 results.extend(_CheckDevtoolsLocalization(input_api, output_api))
311 return results
312
313
Blink Reformat4c46d092018-04-07 15:32:37 +0000314def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000315 results = []
Yang Guo4fd355c2019-09-19 10:59:03 +0200316 results.extend(_CommonChecks(input_api, output_api))
e52a82bdfb5106bd658c2c5ea465e200594be1e2019-10-29 16:02:46 -0700317 results.extend(_CheckDevtoolsLocalization(input_api, output_api, True))
Mathias Bynens032591d2019-10-21 11:51:31 +0200318 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Mandy Chenf0fbdbe2019-08-22 23:58:37 +0000319 return results
Blink Reformat4c46d092018-04-07 15:32:37 +0000320
321
Mandy Chena6be46a2019-07-09 17:06:27 +0000322def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 08:29:25 +0100323 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27 +0000324 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 08:29:25 +0100325 """
Mandy Chena6be46a2019-07-09 17:06:27 +0000326 local_paths = [
327 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
328 ]
329 affected_files = [
330 file_name for file_name in local_paths
331 if any(parent_directory in file_name for parent_directory in parent_directories) and any(
332 file_name.endswith(accepted_ending) for accepted_ending in accepted_endings)
333 ]
334 return affected_files
335
336
Blink Reformat4c46d092018-04-07 15:32:37 +0000337def _getAffectedFrontEndFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000338 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100339 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
340 affected_front_end_files = _getAffectedFiles(input_api, [devtools_front_end], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000341 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files]
342
343
344def _getAffectedJSFiles(input_api):
Blink Reformat4c46d092018-04-07 15:32:37 +0000345 devtools_root = input_api.PresubmitLocalPath()
Yang Guo75beda92019-10-28 08:29:25 +0100346 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
347 devtools_scripts = input_api.os_path.join(devtools_root, 'scripts')
348 affected_js_files = _getAffectedFiles(input_api, [devtools_front_end, devtools_scripts], ['D'], ['.js'])
Blink Reformat4c46d092018-04-07 15:32:37 +0000349 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files]
350
351
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000352def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=None): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37 +0000353 original_sys_path = sys.path
354 try:
Yang Guo75beda92019-10-28 08:29:25 +0100355 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35 +0000356 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37 +0000357 finally:
358 sys.path = original_sys_path
359
Yang Guod8176982019-10-04 20:30:35 +0000360 node_path = devtools_paths.node_path()
Blink Reformat4c46d092018-04-07 15:32:37 +0000361
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000362 if script_arguments is None:
363 script_arguments = []
Mandy Chen465b4f72019-03-21 22:52:54 +0000364
Blink Reformat4c46d092018-04-07 15:32:37 +0000365 process = input_api.subprocess.Popen(
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000366 [node_path, script_path] + script_arguments, stdout=input_api.subprocess.PIPE, stderr=input_api.subprocess.STDOUT)
Blink Reformat4c46d092018-04-07 15:32:37 +0000367 out, _ = process.communicate()
368
369 if process.returncode != 0:
370 return [output_api.PresubmitError(out)]
371 return [output_api.PresubmitNotifyResult(out)]