Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 1 | # 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 Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 15 | from __future__ import print_function |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 16 | import itertools |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 17 | import subprocess |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 18 | import time |
| 19 | |
Lalit Maganti | 86a053e | 2022-11-09 23:35:20 +0000 | [diff] [blame] | 20 | USE_PYTHON3 = True |
| 21 | |
| 22 | |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 23 | def 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 Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 33 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 34 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 35 | def CheckChange(input, output): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 36 | # There apparently is no way to wrap strings in blueprints, so ignore long |
| 37 | # lines in them. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 38 | def long_line_sources(x): |
| 39 | return input.FilterSourceFile( |
| 40 | x, |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 41 | files_to_check='.*', |
| 42 | files_to_skip=[ |
Lalit Maganti | 3dc0ffe | 2021-06-09 13:27:52 +0100 | [diff] [blame] | 43 | 'Android[.]bp', |
Lalit Maganti | bf99dd3 | 2023-02-07 23:50:48 +0000 | [diff] [blame] | 44 | "buildtools/grpc/BUILD.gn", |
Lalit Maganti | 3dc0ffe | 2021-06-09 13:27:52 +0100 | [diff] [blame] | 45 | '.*[.]json$', |
| 46 | '.*[.]sql$', |
| 47 | '.*[.]out$', |
Anna Mayzner | 6988df8 | 2023-01-23 10:37:16 +0000 | [diff] [blame] | 48 | 'test/trace_processor/.*/tests.*$', |
Lalit Maganti | 3dc0ffe | 2021-06-09 13:27:52 +0100 | [diff] [blame] | 49 | '(.*/)?BUILD$', |
| 50 | 'WORKSPACE', |
| 51 | '.*/Makefile$', |
| 52 | '/perfetto_build_flags.h$', |
| 53 | "infra/luci/.*", |
Steve Golton | 7a4efa5 | 2024-01-25 09:18:45 +0000 | [diff] [blame] | 54 | "^ui/.*\.[jt]s$", # TS/JS handled by eslint |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 55 | ]) |
| 56 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 57 | results = [] |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 58 | 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 Golton | 7a4efa5 | 2024-01-25 09:18:45 +0000 | [diff] [blame] | 68 | # TS/JS handled by eslint |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 69 | results += RunAndReportIfLong( |
Steve Golton | 7a4efa5 | 2024-01-25 09:18:45 +0000 | [diff] [blame] | 70 | input.canned_checks.CheckPatchFormatted, input, output, check_js=False) |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 71 | results += RunAndReportIfLong(input.canned_checks.CheckGNFormatted, input, |
| 72 | output) |
| 73 | results += RunAndReportIfLong(CheckIncludeGuards, input, output) |
| 74 | results += RunAndReportIfLong(CheckIncludeViolations, input, output) |
Primiano Tucci | 0dc67c8 | 2024-02-21 17:27:42 +0000 | [diff] [blame] | 75 | results += RunAndReportIfLong(CheckIncludePaths, input, output) |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 76 | 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 Tucci | 0fd92a9 | 2023-07-17 11:47:38 -0700 | [diff] [blame] | 83 | results += RunAndReportIfLong(CheckBadCppPatterns, input, output) |
Anna Mayzner | 412e056 | 2022-11-25 09:55:51 +0000 | [diff] [blame] | 84 | results += RunAndReportIfLong(CheckSqlModules, input, output) |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 85 | results += RunAndReportIfLong(CheckSqlMetrics, input, output) |
Primiano Tucci | 3d4217d | 2021-11-05 11:11:51 +0000 | [diff] [blame] | 86 | results += RunAndReportIfLong(CheckTestData, input, output) |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 87 | results += RunAndReportIfLong(CheckAmalgamatedPythonTools, input, output) |
Rasika Navarange | 93dc176 | 2024-02-07 16:38:07 +0000 | [diff] [blame] | 88 | results += RunAndReportIfLong(CheckChromeStdlib, input, output) |
Primiano Tucci | d4bbac3 | 2024-02-09 16:45:56 +0000 | [diff] [blame] | 89 | results += RunAndReportIfLong(CheckAbsolutePathsInGn, input, output) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 90 | return results |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 91 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 92 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 93 | def CheckChangeOnUpload(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 94 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 95 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 96 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 97 | def CheckChangeOnCommit(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 98 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 99 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 100 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 101 | def CheckBuild(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 102 | # The script invocation doesn't work on Windows. |
| 103 | if input_api.is_windows: |
| 104 | return [] |
| 105 | |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 106 | tool = 'tools/gen_bazel' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 107 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 108 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 109 | def build_file_filter(x): |
| 110 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 111 | x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 112 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 113 | if not input_api.AffectedSourceFiles(build_file_filter): |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 114 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 115 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 116 | return [ |
| 117 | output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' + |
| 118 | tool + ' to update them.') |
| 119 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 120 | return [] |
| 121 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 122 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 123 | def CheckAndroidBlueprint(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 124 | # The script invocation doesn't work on Windows. |
| 125 | if input_api.is_windows: |
| 126 | return [] |
| 127 | |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 128 | tool = 'tools/gen_android_bp' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 129 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 130 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 131 | def build_file_filter(x): |
| 132 | return input_api.FilterSourceFile( |
Rasika Navarange | 4c6a6a6 | 2023-11-12 13:39:54 +0000 | [diff] [blame] | 133 | 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 Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 139 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 140 | if not input_api.AffectedSourceFiles(build_file_filter): |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 141 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 142 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 143 | return [ |
| 144 | output_api.PresubmitError('Android build files are out of date. ' + |
| 145 | 'Run ' + tool + ' to update them.') |
| 146 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 147 | return [] |
| 148 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 149 | |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 150 | def CheckIncludeGuards(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 151 | # The script invocation doesn't work on Windows. |
| 152 | if input_api.is_windows: |
| 153 | return [] |
| 154 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 155 | tool = 'tools/fix_include_guards' |
| 156 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 157 | def file_filter(x): |
| 158 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 159 | x, files_to_check=['.*[.]cc$', '.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 160 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 161 | if not input_api.AffectedSourceFiles(file_filter): |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 162 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 163 | if subprocess.call([tool, '--check-only']): |
| 164 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 165 | output_api.PresubmitError('Please run ' + tool + |
| 166 | ' to fix include guards.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 167 | ] |
| 168 | return [] |
| 169 | |
| 170 | |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 171 | def CheckBannedCpp(input_api, output_api): |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 172 | bad_cpp = [ |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 173 | (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 Tucci | 78cd82b | 2021-10-13 13:50:27 +0100 | [diff] [blame] | 189 | (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 Tucci | 997ab09 | 2021-10-18 20:53:35 +0100 | [diff] [blame] | 193 | (r'//.*\bDNS\b', |
| 194 | '// DNS (Do Not Ship) found. Did you mean to remove some testing code?'), |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 195 | (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 Tucci | 58d2dc6 | 2021-06-24 16:03:24 +0100 | [diff] [blame] | 198 | (r'^#include <inttypes.h>', 'Use <cinttypes> rather than <inttypes.h>. ' + |
| 199 | 'See https://github.com/google/perfetto/issues/146'), |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 200 | ] |
| 201 | |
| 202 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 203 | return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$']) |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 204 | |
| 205 | errors = [] |
| 206 | for f in input_api.AffectedSourceFiles(file_filter): |
| 207 | for line_number, line in f.ChangedContents(): |
Primiano Tucci | 78cd82b | 2021-10-13 13:50:27 +0100 | [diff] [blame] | 208 | if input_api.re.search(r'^\s*//', line): |
| 209 | continue # Skip comments |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 210 | for regex, message in bad_cpp: |
| 211 | if input_api.re.search(regex, line): |
| 212 | errors.append( |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 213 | output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format( |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 214 | f.LocalPath(), line_number, message))) |
| 215 | return errors |
| 216 | |
| 217 | |
Primiano Tucci | 0fd92a9 | 2023-07-17 11:47:38 -0700 | [diff] [blame] | 218 | def 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 Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 237 | def CheckIncludeViolations(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 238 | # The script invocation doesn't work on Windows. |
| 239 | if input_api.is_windows: |
| 240 | return [] |
| 241 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 242 | tool = 'tools/check_include_violations' |
| 243 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 244 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 245 | return input_api.FilterSourceFile( |
| 246 | x, files_to_check=['include/.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 247 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 248 | if not input_api.AffectedSourceFiles(file_filter): |
| 249 | return [] |
| 250 | if subprocess.call([tool]): |
| 251 | return [output_api.PresubmitError(tool + ' failed.')] |
| 252 | return [] |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 253 | |
| 254 | |
Primiano Tucci | 0dc67c8 | 2024-02-21 17:27:42 +0000 | [diff] [blame] | 255 | def 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 Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 280 | def CheckBinaryDescriptors(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 281 | # The script invocation doesn't work on Windows. |
| 282 | if input_api.is_windows: |
| 283 | return [] |
| 284 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 285 | tool = 'tools/gen_binary_descriptors' |
| 286 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 287 | def file_filter(x): |
| 288 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 289 | x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 290 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 291 | if not input_api.AffectedSourceFiles(file_filter): |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 292 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 293 | if subprocess.call([tool, '--check-only']): |
| 294 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 295 | output_api.PresubmitError('Please run ' + tool + |
| 296 | ' to update binary descriptors.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 297 | ] |
| 298 | return [] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 299 | |
| 300 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 301 | def CheckMergedTraceConfigProto(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 302 | # The script invocation doesn't work on Windows. |
| 303 | if input_api.is_windows: |
| 304 | return [] |
| 305 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 306 | tool = 'tools/gen_merged_protos' |
| 307 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 308 | def build_file_filter(x): |
| 309 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 310 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 311 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 312 | if not input_api.AffectedSourceFiles(build_file_filter): |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 313 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 314 | 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 Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 321 | |
| 322 | |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 323 | # Prevent removing or changing lines in event_list. |
| 324 | def CheckProtoEventList(input_api, output_api): |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 325 | for f in input_api.AffectedFiles(): |
Hector Dearman | 7ea83c9 | 2022-05-12 15:21:49 +0100 | [diff] [blame] | 326 | if f.LocalPath() != 'src/tools/ftrace_proto_gen/event_list': |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 327 | continue |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 328 | if any((not new_line.startswith('removed')) and new_line != old_line |
Hector Dearman | d88945e | 2021-12-07 18:56:40 +0000 | [diff] [blame] | 329 | for old_line, new_line in zip(f.OldContents(), f.NewContents())): |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 330 | return [ |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 331 | output_api.PresubmitError( |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 332 | 'event_list only has two supported changes: ' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 333 | 'appending a new line, and replacing a line with removed.') |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 334 | ] |
| 335 | return [] |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 336 | |
| 337 | |
| 338 | def CheckProtoComments(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 339 | # The script invocation doesn't work on Windows. |
| 340 | if input_api.is_windows: |
| 341 | return [] |
| 342 | |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 343 | tool = 'tools/check_proto_comments' |
| 344 | |
| 345 | def file_filter(x): |
| 346 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 347 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 348 | |
| 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 Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 354 | |
| 355 | |
Anna Mayzner | 412e056 | 2022-11-25 09:55:51 +0000 | [diff] [blame] | 356 | def CheckSqlModules(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 357 | # The script invocation doesn't work on Windows. |
| 358 | if input_api.is_windows: |
| 359 | return [] |
| 360 | |
Anna Mayzner | 412e056 | 2022-11-25 09:55:51 +0000 | [diff] [blame] | 361 | tool = 'tools/check_sql_modules.py' |
| 362 | |
| 363 | def file_filter(x): |
| 364 | return input_api.FilterSourceFile( |
Alexander Timin | 8e1c5e8 | 2023-11-06 14:21:12 +0000 | [diff] [blame] | 365 | x, |
| 366 | files_to_check=[ |
| 367 | 'src/trace_processor/perfetto_sql/stdlib/.*[.]sql$', tool |
| 368 | ]) |
Anna Mayzner | 412e056 | 2022-11-25 09:55:51 +0000 | [diff] [blame] | 369 | |
| 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 Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 377 | def CheckSqlMetrics(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 378 | # The script invocation doesn't work on Windows. |
| 379 | if input_api.is_windows: |
| 380 | return [] |
| 381 | |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 382 | 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 Tucci | 3d4217d | 2021-11-05 11:11:51 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | def CheckTestData(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 396 | # The script invocation doesn't work on Windows. |
| 397 | if input_api.is_windows: |
| 398 | return [] |
| 399 | |
Primiano Tucci | 3d4217d | 2021-11-05 11:11:51 +0000 | [diff] [blame] | 400 | tool = 'tools/test_data' |
| 401 | if subprocess.call([tool, 'status', '--quiet']): |
| 402 | return [ |
| 403 | output_api.PresubmitError( |
Mohit Saini | 8abc524 | 2022-06-24 11:16:03 +0100 | [diff] [blame] | 404 | '//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 Shulaev | d85d05f | 2022-07-04 11:54:42 +0100 | [diff] [blame] | 407 | ' or `tools/test_data download --overwrite` to sync local test_data' |
| 408 | ) |
Primiano Tucci | 3d4217d | 2021-11-05 11:11:51 +0000 | [diff] [blame] | 409 | ] |
| 410 | return [] |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 411 | |
| 412 | |
Rasika Navarange | 93dc176 | 2024-02-07 16:38:07 +0000 | [diff] [blame] | 413 | def 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 Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 444 | def CheckAmalgamatedPythonTools(input_api, output_api): |
Bruce Dawson | 7627d04 | 2023-07-17 10:08:24 -0700 | [diff] [blame] | 445 | # The script invocation doesn't work on Windows. |
| 446 | if input_api.is_windows: |
| 447 | return [] |
| 448 | |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 449 | 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 Tucci | d4bbac3 | 2024-02-09 16:45:56 +0000 | [diff] [blame] | 464 | |
| 465 | |
| 466 | def 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 | ] |