blob: 124a502e50bcbbc74d92a0646768872839124d4f [file] [log] [blame]
Hector Dearman534765e2017-11-01 11:17:38 +00001# Copyright (C) 2017 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Hector Dearman2cc71f62021-07-14 10:28:58 +010015from __future__ import print_function
Florian Mayer640b7ae2018-05-09 15:28:32 +010016import itertools
Sami Kyostilab27619f2017-12-13 19:22:16 +000017import subprocess
Hector Dearman2cc71f62021-07-14 10:28:58 +010018import time
19
Lalit Maganti86a053e2022-11-09 23:35:20 +000020USE_PYTHON3 = True
21
22
Hector Dearman2cc71f62021-07-14 10:28:58 +010023def RunAndReportIfLong(func, *args, **kargs):
24 start = time.time()
25 results = func(*args, **kargs)
26 end = time.time()
27 limit = 0.5 # seconds
28 name = func.__name__
29 runtime = end - start
30 if runtime > limit:
31 print("{} took >{:.2}s ({:.2}s)".format(name, limit, runtime))
32 return results
Sami Kyostilab27619f2017-12-13 19:22:16 +000033
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010034
Hector Dearman534765e2017-11-01 11:17:38 +000035def CheckChange(input, output):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010036 # There apparently is no way to wrap strings in blueprints, so ignore long
37 # lines in them.
Primiano Tucci834fdc72019-10-04 11:33:44 +010038 def long_line_sources(x):
39 return input.FilterSourceFile(
40 x,
Ryan Savitski135038e2020-11-12 20:57:57 +000041 files_to_check='.*',
42 files_to_skip=[
Lalit Maganti3dc0ffe2021-06-09 13:27:52 +010043 'Android[.]bp',
Lalit Magantibf99dd32023-02-07 23:50:48 +000044 "buildtools/grpc/BUILD.gn",
Lalit Maganti3dc0ffe2021-06-09 13:27:52 +010045 '.*[.]json$',
46 '.*[.]sql$',
47 '.*[.]out$',
Anna Mayzner6988df82023-01-23 10:37:16 +000048 'test/trace_processor/.*/tests.*$',
Lalit Maganti3dc0ffe2021-06-09 13:27:52 +010049 '(.*/)?BUILD$',
50 'WORKSPACE',
51 '.*/Makefile$',
52 '/perfetto_build_flags.h$',
53 "infra/luci/.*",
Steve Golton7a4efa52024-01-25 09:18:45 +000054 "^ui/.*\.[jt]s$", # TS/JS handled by eslint
Primiano Tucci834fdc72019-10-04 11:33:44 +010055 ])
56
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010057 results = []
Hector Dearman2cc71f62021-07-14 10:28:58 +010058 results += RunAndReportIfLong(input.canned_checks.CheckDoNotSubmit, input,
59 output)
60 results += RunAndReportIfLong(input.canned_checks.CheckChangeHasNoTabs, input,
61 output)
62 results += RunAndReportIfLong(
63 input.canned_checks.CheckLongLines,
64 input,
65 output,
66 80,
67 source_file_filter=long_line_sources)
Steve Golton7a4efa52024-01-25 09:18:45 +000068 # TS/JS handled by eslint
Hector Dearman2cc71f62021-07-14 10:28:58 +010069 results += RunAndReportIfLong(
Steve Golton7a4efa52024-01-25 09:18:45 +000070 input.canned_checks.CheckPatchFormatted, input, output, check_js=False)
Hector Dearman2cc71f62021-07-14 10:28:58 +010071 results += RunAndReportIfLong(input.canned_checks.CheckGNFormatted, input,
72 output)
73 results += RunAndReportIfLong(CheckIncludeGuards, input, output)
74 results += RunAndReportIfLong(CheckIncludeViolations, input, output)
Primiano Tucci0dc67c82024-02-21 17:27:42 +000075 results += RunAndReportIfLong(CheckIncludePaths, input, output)
Hector Dearman2cc71f62021-07-14 10:28:58 +010076 results += RunAndReportIfLong(CheckProtoComments, input, output)
77 results += RunAndReportIfLong(CheckBuild, input, output)
78 results += RunAndReportIfLong(CheckAndroidBlueprint, input, output)
79 results += RunAndReportIfLong(CheckBinaryDescriptors, input, output)
80 results += RunAndReportIfLong(CheckMergedTraceConfigProto, input, output)
81 results += RunAndReportIfLong(CheckProtoEventList, input, output)
82 results += RunAndReportIfLong(CheckBannedCpp, input, output)
Primiano Tucci0fd92a92023-07-17 11:47:38 -070083 results += RunAndReportIfLong(CheckBadCppPatterns, input, output)
Anna Mayzner412e0562022-11-25 09:55:51 +000084 results += RunAndReportIfLong(CheckSqlModules, input, output)
Hector Dearman2cc71f62021-07-14 10:28:58 +010085 results += RunAndReportIfLong(CheckSqlMetrics, input, output)
Primiano Tucci3d4217d2021-11-05 11:11:51 +000086 results += RunAndReportIfLong(CheckTestData, input, output)
Primiano Tucci11d94e12022-08-02 17:44:33 +010087 results += RunAndReportIfLong(CheckAmalgamatedPythonTools, input, output)
Rasika Navarange93dc1762024-02-07 16:38:07 +000088 results += RunAndReportIfLong(CheckChromeStdlib, input, output)
Primiano Tuccid4bbac32024-02-09 16:45:56 +000089 results += RunAndReportIfLong(CheckAbsolutePathsInGn, input, output)
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010090 return results
Hector Dearman534765e2017-11-01 11:17:38 +000091
Sami Kyostilab27619f2017-12-13 19:22:16 +000092
Hector Dearman534765e2017-11-01 11:17:38 +000093def CheckChangeOnUpload(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010094 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000095
Sami Kyostilab27619f2017-12-13 19:22:16 +000096
Hector Dearman534765e2017-11-01 11:17:38 +000097def CheckChangeOnCommit(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010098 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000099
Sami Kyostilab27619f2017-12-13 19:22:16 +0000100
Lalit Maganti279ecde2019-04-01 16:57:12 +0100101def CheckBuild(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700102 # The script invocation doesn't work on Windows.
103 if input_api.is_windows:
104 return []
105
Primiano Tucci9c411652019-08-27 07:13:59 +0200106 tool = 'tools/gen_bazel'
Primiano Tucci834fdc72019-10-04 11:33:44 +0100107
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100108 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +0100109 def build_file_filter(x):
110 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000111 x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool))
Primiano Tucci834fdc72019-10-04 11:33:44 +0100112
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100113 if not input_api.AffectedSourceFiles(build_file_filter):
Lalit Maganti279ecde2019-04-01 16:57:12 +0100114 return []
Primiano Tucci9c411652019-08-27 07:13:59 +0200115 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +0100116 return [
117 output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' +
118 tool + ' to update them.')
119 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100120 return []
121
Lalit Maganti279ecde2019-04-01 16:57:12 +0100122
Sami Kyostilab27619f2017-12-13 19:22:16 +0000123def CheckAndroidBlueprint(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700124 # The script invocation doesn't work on Windows.
125 if input_api.is_windows:
126 return []
127
Primiano Tucci9c411652019-08-27 07:13:59 +0200128 tool = 'tools/gen_android_bp'
Primiano Tucci834fdc72019-10-04 11:33:44 +0100129
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100130 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +0100131 def build_file_filter(x):
132 return input_api.FilterSourceFile(
Rasika Navarange4c6a6a62023-11-12 13:39:54 +0000133 x,
134 files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', tool),
135 # Do not require Android.bp to be regenerated for chrome
136 # stdlib changes.
137 files_to_skip=(
138 'src/trace_processor/perfetto_sql/stdlib/chrome/BUILD.gn'))
Primiano Tucci834fdc72019-10-04 11:33:44 +0100139
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100140 if not input_api.AffectedSourceFiles(build_file_filter):
Sami Kyostilab27619f2017-12-13 19:22:16 +0000141 return []
Primiano Tucci9c411652019-08-27 07:13:59 +0200142 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +0100143 return [
144 output_api.PresubmitError('Android build files are out of date. ' +
145 'Run ' + tool + ' to update them.')
146 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100147 return []
148
Primiano Tuccic5010802018-01-19 17:13:21 +0000149
Primiano Tucci40e98722018-02-16 11:50:17 +0000150def CheckIncludeGuards(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700151 # The script invocation doesn't work on Windows.
152 if input_api.is_windows:
153 return []
154
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100155 tool = 'tools/fix_include_guards'
156
Primiano Tucci834fdc72019-10-04 11:33:44 +0100157 def file_filter(x):
158 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000159 x, files_to_check=['.*[.]cc$', '.*[.]h$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100160
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100161 if not input_api.AffectedSourceFiles(file_filter):
Primiano Tucci40e98722018-02-16 11:50:17 +0000162 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100163 if subprocess.call([tool, '--check-only']):
164 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100165 output_api.PresubmitError('Please run ' + tool +
166 ' to fix include guards.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100167 ]
168 return []
169
170
Ryan704bc822020-03-31 02:31:13 +0100171def CheckBannedCpp(input_api, output_api):
Hector Dearman53d91b92019-12-13 17:07:52 +0000172 bad_cpp = [
Hector Dearman53d91b92019-12-13 17:07:52 +0000173 (r'\bstd::stoi\b',
174 'std::stoi throws exceptions prefer base::StringToInt32()'),
175 (r'\bstd::stol\b',
176 'std::stoull throws exceptions prefer base::StringToInt32()'),
177 (r'\bstd::stoul\b',
178 'std::stoull throws exceptions prefer base::StringToUint32()'),
179 (r'\bstd::stoll\b',
180 'std::stoull throws exceptions prefer base::StringToInt64()'),
181 (r'\bstd::stoull\b',
182 'std::stoull throws exceptions prefer base::StringToUint64()'),
183 (r'\bstd::stof\b',
184 'std::stof throws exceptions prefer base::StringToDouble()'),
185 (r'\bstd::stod\b',
186 'std::stod throws exceptions prefer base::StringToDouble()'),
187 (r'\bstd::stold\b',
188 'std::stold throws exceptions prefer base::StringToDouble()'),
Primiano Tucci78cd82b2021-10-13 13:50:27 +0100189 (r'\bstrncpy\b',
190 'strncpy does not null-terminate if src > dst. Use base::StringCopy'),
191 (r'[(=]\s*snprintf\(',
192 'snprintf can return > dst_size. Use base::SprintfTrunc'),
Primiano Tucci997ab092021-10-18 20:53:35 +0100193 (r'//.*\bDNS\b',
194 '// DNS (Do Not Ship) found. Did you mean to remove some testing code?'),
Ryan704bc822020-03-31 02:31:13 +0100195 (r'\bPERFETTO_EINTR\(close\(',
196 'close(2) must not be retried on EINTR on Linux and other OSes '
197 'that we run on, as the fd will be closed.'),
Primiano Tucci58d2dc62021-06-24 16:03:24 +0100198 (r'^#include <inttypes.h>', 'Use <cinttypes> rather than <inttypes.h>. ' +
199 'See https://github.com/google/perfetto/issues/146'),
Hector Dearman53d91b92019-12-13 17:07:52 +0000200 ]
201
202 def file_filter(x):
Ryan Savitski135038e2020-11-12 20:57:57 +0000203 return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$'])
Hector Dearman53d91b92019-12-13 17:07:52 +0000204
205 errors = []
206 for f in input_api.AffectedSourceFiles(file_filter):
207 for line_number, line in f.ChangedContents():
Primiano Tucci78cd82b2021-10-13 13:50:27 +0100208 if input_api.re.search(r'^\s*//', line):
209 continue # Skip comments
Hector Dearman53d91b92019-12-13 17:07:52 +0000210 for regex, message in bad_cpp:
211 if input_api.re.search(regex, line):
212 errors.append(
Ryan704bc822020-03-31 02:31:13 +0100213 output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format(
Hector Dearman53d91b92019-12-13 17:07:52 +0000214 f.LocalPath(), line_number, message)))
215 return errors
216
217
Primiano Tucci0fd92a92023-07-17 11:47:38 -0700218def CheckBadCppPatterns(input_api, output_api):
219 bad_patterns = [
220 (r'.*/tracing_service_impl[.]cc$', r'\btrigger_config\(\)',
221 'Use GetTriggerMode(session->config) rather than .trigger_config()'),
222 ]
223 errors = []
224 for file_regex, code_regex, message in bad_patterns:
225 filt = lambda x: input_api.FilterSourceFile(x, files_to_check=[file_regex])
226 for f in input_api.AffectedSourceFiles(filt):
227 for line_number, line in f.ChangedContents():
228 if input_api.re.search(r'^\s*//', line):
229 continue # Skip comments
230 if input_api.re.search(code_regex, line):
231 errors.append(
232 output_api.PresubmitError('{}:{} {}'.format(
233 f.LocalPath(), line_number, message)))
234 return errors
235
236
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100237def CheckIncludeViolations(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700238 # The script invocation doesn't work on Windows.
239 if input_api.is_windows:
240 return []
241
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100242 tool = 'tools/check_include_violations'
243
Primiano Tucci834fdc72019-10-04 11:33:44 +0100244 def file_filter(x):
Ryan Savitski135038e2020-11-12 20:57:57 +0000245 return input_api.FilterSourceFile(
246 x, files_to_check=['include/.*[.]h$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100247
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100248 if not input_api.AffectedSourceFiles(file_filter):
249 return []
250 if subprocess.call([tool]):
251 return [output_api.PresubmitError(tool + ' failed.')]
252 return []
Primiano Tucci40e98722018-02-16 11:50:17 +0000253
254
Primiano Tucci0dc67c82024-02-21 17:27:42 +0000255def CheckIncludePaths(input_api, output_api):
256
257 def file_filter(x):
258 return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$'])
259
260 error_lines = []
261 for f in input_api.AffectedSourceFiles(file_filter):
262 for line_num, line in f.ChangedContents():
263 m = input_api.re.search(r'^#include "(.*\.h)"', line)
264 if not m:
265 continue
266 inc_hdr = m.group(1)
267 if inc_hdr.startswith('include/perfetto'):
268 error_lines.append(' %s:%s: Redundant "include/" in #include path"' %
269 (f.LocalPath(), line_num))
270 if '/' not in inc_hdr:
271 error_lines.append(
272 ' %s:%s: relative #include not allowed, use full path' %
273 (f.LocalPath(), line_num))
274 return [] if len(error_lines) == 0 else [
275 output_api.PresubmitError('Invalid #include paths detected:\n' +
276 '\n'.join(error_lines))
277 ]
278
279
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000280def CheckBinaryDescriptors(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700281 # The script invocation doesn't work on Windows.
282 if input_api.is_windows:
283 return []
284
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100285 tool = 'tools/gen_binary_descriptors'
286
Primiano Tucci834fdc72019-10-04 11:33:44 +0100287 def file_filter(x):
288 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000289 x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100290
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100291 if not input_api.AffectedSourceFiles(file_filter):
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000292 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100293 if subprocess.call([tool, '--check-only']):
294 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100295 output_api.PresubmitError('Please run ' + tool +
296 ' to update binary descriptors.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100297 ]
298 return []
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000299
300
Primiano Tuccic5010802018-01-19 17:13:21 +0000301def CheckMergedTraceConfigProto(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700302 # The script invocation doesn't work on Windows.
303 if input_api.is_windows:
304 return []
305
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100306 tool = 'tools/gen_merged_protos'
307
Primiano Tucci834fdc72019-10-04 11:33:44 +0100308 def build_file_filter(x):
309 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000310 x, files_to_check=['protos/perfetto/.*[.]proto$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100311
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100312 if not input_api.AffectedSourceFiles(build_file_filter):
Primiano Tuccic5010802018-01-19 17:13:21 +0000313 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100314 if subprocess.call([tool, '--check-only']):
315 return [
316 output_api.PresubmitError(
317 'perfetto_config.proto or perfetto_trace.proto is out of ' +
318 'date. Please run ' + tool + ' to update it.')
319 ]
320 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100321
322
Primiano Tuccia3645202020-08-03 16:28:18 +0200323# Prevent removing or changing lines in event_list.
324def CheckProtoEventList(input_api, output_api):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100325 for f in input_api.AffectedFiles():
Hector Dearman7ea83c92022-05-12 15:21:49 +0100326 if f.LocalPath() != 'src/tools/ftrace_proto_gen/event_list':
Florian Mayer640b7ae2018-05-09 15:28:32 +0100327 continue
Primiano Tucci834fdc72019-10-04 11:33:44 +0100328 if any((not new_line.startswith('removed')) and new_line != old_line
Hector Dearmand88945e2021-12-07 18:56:40 +0000329 for old_line, new_line in zip(f.OldContents(), f.NewContents())):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100330 return [
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100331 output_api.PresubmitError(
Primiano Tuccia3645202020-08-03 16:28:18 +0200332 'event_list only has two supported changes: '
Primiano Tucci834fdc72019-10-04 11:33:44 +0100333 'appending a new line, and replacing a line with removed.')
Florian Mayer640b7ae2018-05-09 15:28:32 +0100334 ]
335 return []
Anindita Ghosh2aa4e422020-06-26 15:48:32 +0100336
337
338def CheckProtoComments(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700339 # The script invocation doesn't work on Windows.
340 if input_api.is_windows:
341 return []
342
Anindita Ghosh2aa4e422020-06-26 15:48:32 +0100343 tool = 'tools/check_proto_comments'
344
345 def file_filter(x):
346 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000347 x, files_to_check=['protos/perfetto/.*[.]proto$', tool])
Anindita Ghosh2aa4e422020-06-26 15:48:32 +0100348
349 if not input_api.AffectedSourceFiles(file_filter):
350 return []
351 if subprocess.call([tool]):
352 return [output_api.PresubmitError(tool + ' failed')]
353 return []
Lalit Magantie0e8bdb2021-01-11 19:43:55 +0000354
355
Anna Mayzner412e0562022-11-25 09:55:51 +0000356def CheckSqlModules(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700357 # The script invocation doesn't work on Windows.
358 if input_api.is_windows:
359 return []
360
Anna Mayzner412e0562022-11-25 09:55:51 +0000361 tool = 'tools/check_sql_modules.py'
362
363 def file_filter(x):
364 return input_api.FilterSourceFile(
Alexander Timin8e1c5e82023-11-06 14:21:12 +0000365 x,
366 files_to_check=[
367 'src/trace_processor/perfetto_sql/stdlib/.*[.]sql$', tool
368 ])
Anna Mayzner412e0562022-11-25 09:55:51 +0000369
370 if not input_api.AffectedSourceFiles(file_filter):
371 return []
372 if subprocess.call([tool]):
373 return [output_api.PresubmitError(tool + ' failed')]
374 return []
375
376
Lalit Magantie0e8bdb2021-01-11 19:43:55 +0000377def CheckSqlMetrics(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700378 # The script invocation doesn't work on Windows.
379 if input_api.is_windows:
380 return []
381
Lalit Magantie0e8bdb2021-01-11 19:43:55 +0000382 tool = 'tools/check_sql_metrics.py'
383
384 def file_filter(x):
385 return input_api.FilterSourceFile(
386 x, files_to_check=['src/trace_processor/metrics/.*[.]sql$', tool])
387
388 if not input_api.AffectedSourceFiles(file_filter):
389 return []
390 if subprocess.call([tool]):
391 return [output_api.PresubmitError(tool + ' failed')]
392 return []
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000393
394
395def CheckTestData(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700396 # The script invocation doesn't work on Windows.
397 if input_api.is_windows:
398 return []
399
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000400 tool = 'tools/test_data'
401 if subprocess.call([tool, 'status', '--quiet']):
402 return [
403 output_api.PresubmitError(
Mohit Saini8abc5242022-06-24 11:16:03 +0100404 '//test/data is out of sync. Run ' + tool + ' status for more. \n'
405 'If you rebaselined UI tests or added a new test trace, run:'
406 '`tools/test_data upload`. Otherwise run `tools/install-build-deps`'
Andrew Shulaevd85d05f2022-07-04 11:54:42 +0100407 ' or `tools/test_data download --overwrite` to sync local test_data'
408 )
Primiano Tucci3d4217d2021-11-05 11:11:51 +0000409 ]
410 return []
Primiano Tucci11d94e12022-08-02 17:44:33 +0100411
412
Rasika Navarange93dc1762024-02-07 16:38:07 +0000413def CheckChromeStdlib(input_api, output_api):
414 stdlib_paths = ("src/trace_processor/perfetto_sql/stdlib/chrome/",
415 "test/data/chrome/",
416 "test/trace_processor/diff_tests/stdlib/chrome/")
417
418 def chrome_stdlib_file_filter(x):
419 return input_api.FilterSourceFile(x, files_to_check=stdlib_paths)
420
421 # Only check chrome stdlib files
422 if not any(input_api.AffectedFiles(file_filter=chrome_stdlib_file_filter)):
423 return []
424
425 # Always allow Copybara service to make changes to chrome stdlib
426 if input_api.change.COPYBARA_IMPORT:
427 return []
428
429 if input_api.change.CHROME_STDLIB_MANUAL_ROLL:
430 return []
431
432 message = (
433 'Files under {0} and {1} '
434 'are rolled from the Chromium repository by a '
435 'Copybara service.\nYou should not modify these in '
436 'the Perfetto repository, please make your changes '
437 'in Chromium instead.\n'
438 'If you want to do a manual roll, you must specify '
439 'CHROME_STDLIB_MANUAL_ROLL=<reason> in the CL description.').format(
440 *stdlib_paths)
441 return [output_api.PresubmitError(message)]
442
443
Primiano Tucci11d94e12022-08-02 17:44:33 +0100444def CheckAmalgamatedPythonTools(input_api, output_api):
Bruce Dawson7627d042023-07-17 10:08:24 -0700445 # The script invocation doesn't work on Windows.
446 if input_api.is_windows:
447 return []
448
Primiano Tucci11d94e12022-08-02 17:44:33 +0100449 tool = 'tools/gen_amalgamated_python_tools'
450
451 # If no GN files were modified, bail out.
452 def build_file_filter(x):
453 return input_api.FilterSourceFile(x, files_to_check=('python/.*$', tool))
454
455 if not input_api.AffectedSourceFiles(build_file_filter):
456 return []
457 if subprocess.call([tool, '--check-only']):
458 return [
459 output_api.PresubmitError(
460 'amalgamated python tools/ are out of date. ' + 'Run ' + tool +
461 ' to update them.')
462 ]
463 return []
Primiano Tuccid4bbac32024-02-09 16:45:56 +0000464
465
466def CheckAbsolutePathsInGn(input_api, output_api):
467
468 def file_filter(x):
469 return input_api.FilterSourceFile(
470 x,
471 files_to_check=[r'.*\.gni?$'],
472 files_to_skip=['^.gn$', '^gn/.*', '^buildtools/.*'])
473
474 error_lines = []
475 for f in input_api.AffectedSourceFiles(file_filter):
476 for line_number, line in f.ChangedContents():
477 if input_api.re.search(r'(^\s*[#])|([#]\s*nogncheck)', line):
478 continue # Skip comments and '# nogncheck' lines
479 if input_api.re.search(r'"//[^"]', line):
480 error_lines.append(' %s:%s: %s' %
481 (f.LocalPath(), line_number, line.strip()))
482
483 if len(error_lines) == 0:
484 return []
485 return [
486 output_api.PresubmitError(
487 'Use relative paths in GN rather than absolute:\n' +
488 '\n'.join(error_lines))
489 ]