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 | |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 15 | import itertools |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 16 | import subprocess |
| 17 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 18 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 19 | def CheckChange(input, output): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 20 | # There apparently is no way to wrap strings in blueprints, so ignore long |
| 21 | # lines in them. |
| 22 | def long_line_sources(x): return input.FilterSourceFile( |
| 23 | x, white_list=".*", |
| 24 | black_list=['Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$', |
Primiano Tucci | 94f90f0 | 2019-07-09 19:47:12 +0100 | [diff] [blame] | 25 | 'test/trace_processor/index$', 'BUILD$', 'protos/BUILD$', |
| 26 | '.*/Makefile$']) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 27 | results = [] |
| 28 | results += input.canned_checks.CheckDoNotSubmit(input, output) |
| 29 | results += input.canned_checks.CheckChangeHasNoTabs(input, output) |
| 30 | results += input.canned_checks.CheckLongLines( |
| 31 | input, output, 80, source_file_filter=long_line_sources) |
| 32 | results += input.canned_checks.CheckPatchFormatted( |
| 33 | input, output, check_js=True) |
| 34 | results += input.canned_checks.CheckGNFormatted(input, output) |
| 35 | results += CheckIncludeGuards(input, output) |
| 36 | results += CheckIncludeViolations(input, output) |
| 37 | results += CheckBuild(input, output) |
| 38 | results += CheckAndroidBlueprint(input, output) |
| 39 | results += CheckBinaryDescriptors(input, output) |
| 40 | results += CheckMergedTraceConfigProto(input, output) |
| 41 | results += CheckWhitelist(input, output) |
| 42 | return results |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 43 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 44 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 45 | def CheckChangeOnUpload(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 46 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 47 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 48 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 49 | def CheckChangeOnCommit(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 50 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 51 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 52 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 53 | def CheckBuild(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 54 | # If no GN files were modified, bail out. |
| 55 | def build_file_filter(x): return input_api.FilterSourceFile( |
| 56 | x, |
Florian Mayer | c039fd3 | 2019-07-02 13:49:44 +0100 | [diff] [blame] | 57 | white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_bazel', |
| 58 | 'BUILD\.extras')) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 59 | if not input_api.AffectedSourceFiles(build_file_filter): |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 60 | return [] |
| 61 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 62 | with open('BUILD') as f: |
| 63 | current_build = f.read() |
| 64 | |
| 65 | new_build = subprocess.check_output( |
| 66 | ['tools/gen_bazel', '--output', '/dev/stdout', '--output-proto', |
| 67 | '/dev/null']) |
| 68 | |
| 69 | with open('protos/BUILD') as f: |
| 70 | current_proto_build = f.read() |
| 71 | |
| 72 | new_proto_build = subprocess.check_output( |
| 73 | ['tools/gen_bazel', '--output', '/dev/null', '--output-proto', |
| 74 | '/dev/stdout']) |
| 75 | |
| 76 | if current_build != new_build or current_proto_build != new_proto_build: |
| 77 | return [ |
| 78 | output_api.PresubmitError( |
| 79 | 'BUILD and/or protos/BUILD is out of date. ' + |
| 80 | 'Please run tools/gen_bazel to update it.') |
| 81 | ] |
| 82 | return [] |
| 83 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 84 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 85 | def CheckAndroidBlueprint(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 86 | # If no GN files were modified, bail out. |
| 87 | def build_file_filter(x): return input_api.FilterSourceFile( |
| 88 | x, |
| 89 | white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp')) |
| 90 | if not input_api.AffectedSourceFiles(build_file_filter): |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 91 | return [] |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 92 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 93 | with open('Android.bp') as f: |
| 94 | current_blueprint = f.read() |
| 95 | |
| 96 | new_blueprint = subprocess.check_output( |
| 97 | ['tools/gen_android_bp', '--output', '/dev/stdout']) |
| 98 | |
| 99 | if current_blueprint != new_blueprint: |
| 100 | return [ |
| 101 | output_api.PresubmitError( |
| 102 | 'Android.bp is out of date. Please run tools/gen_android_bp ' |
| 103 | 'to update it.') |
| 104 | ] |
| 105 | return [] |
| 106 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 107 | |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 108 | def CheckIncludeGuards(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 109 | tool = 'tools/fix_include_guards' |
| 110 | |
| 111 | def file_filter(x): return input_api.FilterSourceFile( |
| 112 | x, |
| 113 | white_list=['.*[.]cc$', '.*[.]h$', tool]) |
| 114 | if not input_api.AffectedSourceFiles(file_filter): |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 115 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 116 | if subprocess.call([tool, '--check-only']): |
| 117 | return [ |
| 118 | output_api.PresubmitError( |
| 119 | 'Please run ' + tool + ' to fix include guards.') |
| 120 | ] |
| 121 | return [] |
| 122 | |
| 123 | |
| 124 | def CheckIncludeViolations(input_api, output_api): |
| 125 | tool = 'tools/check_include_violations' |
| 126 | |
| 127 | def file_filter(x): return input_api.FilterSourceFile( |
| 128 | x, |
| 129 | white_list=['include/.*[.]h$', tool]) |
| 130 | if not input_api.AffectedSourceFiles(file_filter): |
| 131 | return [] |
| 132 | if subprocess.call([tool]): |
| 133 | return [output_api.PresubmitError(tool + ' failed.')] |
| 134 | return [] |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 135 | |
| 136 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 137 | def CheckBinaryDescriptors(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 138 | tool = 'tools/gen_binary_descriptors' |
| 139 | |
| 140 | def file_filter(x): return input_api.FilterSourceFile( |
| 141 | x, |
| 142 | white_list=['protos/perfetto/.*[.]proto$', '.*[.]h', tool]) |
| 143 | if not input_api.AffectedSourceFiles(file_filter): |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 144 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 145 | if subprocess.call([tool, '--check-only']): |
| 146 | return [ |
| 147 | output_api.PresubmitError( |
| 148 | 'Please run ' + tool + ' to update binary descriptors.') |
| 149 | ] |
| 150 | return [] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 151 | |
| 152 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 153 | def CheckMergedTraceConfigProto(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 154 | tool = 'tools/gen_merged_protos' |
| 155 | |
| 156 | def build_file_filter(x): return input_api.FilterSourceFile( |
| 157 | x, |
| 158 | white_list=['protos/perfetto/.*[.]proto$', tool]) |
| 159 | if not input_api.AffectedSourceFiles(build_file_filter): |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 160 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 161 | if subprocess.call([tool, '--check-only']): |
| 162 | return [ |
| 163 | output_api.PresubmitError( |
| 164 | 'perfetto_config.proto or perfetto_trace.proto is out of ' + |
| 165 | 'date. Please run ' + tool + ' to update it.') |
| 166 | ] |
| 167 | return [] |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 168 | |
| 169 | |
| 170 | # Prevent removing or changing lines in event_whitelist. |
| 171 | def CheckWhitelist(input_api, output_api): |
| 172 | for f in input_api.AffectedFiles(): |
| 173 | if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist': |
| 174 | continue |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 175 | if any((not new_line.startswith('removed')) |
Isabelle Taylor | 8d49fa0 | 2018-10-26 13:23:00 +0100 | [diff] [blame] | 176 | and new_line != old_line for old_line, new_line |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 177 | in itertools.izip(f.OldContents(), f.NewContents())): |
| 178 | return [ |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 179 | output_api.PresubmitError( |
| 180 | 'event_whitelist only has two supported changes: ' |
| 181 | 'appending a new line, and replacing a line with removed.' |
| 182 | ) |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 183 | ] |
| 184 | return [] |