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 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 18 | def CheckChange(input, output): |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 19 | # There apparently is no way to wrap strings in blueprints, so ignore long |
| 20 | # lines in them. |
| 21 | long_line_sources = lambda x: input.FilterSourceFile( |
Primiano Tucci | 2c761ef | 2019-01-07 20:20:46 +0000 | [diff] [blame^] | 22 | x, white_list=".*", |
| 23 | black_list=['Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$']) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 24 | results = [] |
| 25 | results += input.canned_checks.CheckDoNotSubmit(input, output) |
| 26 | results += input.canned_checks.CheckChangeHasNoTabs(input, output) |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 27 | results += input.canned_checks.CheckLongLines( |
| 28 | input, output, 80, source_file_filter=long_line_sources) |
Deepanjan Roy | a752b46 | 2018-07-04 01:24:31 +0100 | [diff] [blame] | 29 | results += input.canned_checks.CheckPatchFormatted( |
| 30 | input, output, check_js=True) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 31 | results += input.canned_checks.CheckGNFormatted(input, output) |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 32 | results += CheckIncludeGuards(input, output) |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 33 | results += CheckAndroidBlueprint(input, output) |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 34 | results += CheckBinaryDescriptors(input, output) |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 35 | results += CheckMergedTraceConfigProto(input, output) |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 36 | results += CheckWhitelist(input, output) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 37 | return results |
| 38 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 39 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 40 | def CheckChangeOnUpload(input_api, output_api): |
| 41 | return CheckChange(input_api, output_api) |
| 42 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 43 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 44 | def CheckChangeOnCommit(input_api, output_api): |
| 45 | return CheckChange(input_api, output_api) |
| 46 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 47 | |
| 48 | def CheckAndroidBlueprint(input_api, output_api): |
| 49 | # If no GN files were modified, bail out. |
| 50 | build_file_filter = lambda x: input_api.FilterSourceFile( |
| 51 | x, |
| 52 | white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp')) |
| 53 | if not input_api.AffectedSourceFiles(build_file_filter): |
| 54 | return [] |
| 55 | |
| 56 | with open('Android.bp') as f: |
| 57 | current_blueprint = f.read() |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 58 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 59 | new_blueprint = subprocess.check_output( |
| 60 | ['tools/gen_android_bp', '--output', '/dev/stdout']) |
| 61 | |
| 62 | if current_blueprint != new_blueprint: |
| 63 | return [ |
| 64 | output_api.PresubmitError( |
| 65 | 'Android.bp is out of date. Please run tools/gen_android_bp ' |
| 66 | 'to update it.') |
| 67 | ] |
| 68 | return [] |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 69 | |
| 70 | |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 71 | def CheckIncludeGuards(input_api, output_api): |
| 72 | tool = 'tools/fix_include_guards' |
| 73 | file_filter = lambda x: input_api.FilterSourceFile( |
| 74 | x, |
| 75 | white_list=('.*[.]cc$', '.*[.]h$', tool)) |
| 76 | if not input_api.AffectedSourceFiles(file_filter): |
| 77 | return [] |
| 78 | if subprocess.call([tool, '--check-only']): |
| 79 | return [ |
| 80 | output_api.PresubmitError( |
| 81 | 'Please run ' + tool + ' to fix include guards.') |
| 82 | ] |
| 83 | return [] |
| 84 | |
| 85 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 86 | def CheckBinaryDescriptors(input_api, output_api): |
| 87 | tool = 'tools/gen_binary_descriptors' |
| 88 | file_filter = lambda x: input_api.FilterSourceFile( |
| 89 | x, |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 90 | white_list=('protos/perfetto/.*[.]proto$', '.*[.]h', tool)) |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 91 | if not input_api.AffectedSourceFiles(file_filter): |
| 92 | return [] |
| 93 | if subprocess.call([tool, '--check-only']): |
| 94 | return [ |
| 95 | output_api.PresubmitError( |
| 96 | 'Please run ' + tool + ' to update binary descriptors.') |
| 97 | ] |
| 98 | return [] |
| 99 | |
| 100 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 101 | def CheckMergedTraceConfigProto(input_api, output_api): |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 102 | tool = 'tools/gen_merged_protos' |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 103 | build_file_filter = lambda x: input_api.FilterSourceFile( |
| 104 | x, |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 105 | white_list=('protos/perfetto/.*[.]proto$', tool)) |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 106 | if not input_api.AffectedSourceFiles(build_file_filter): |
| 107 | return [] |
| 108 | if subprocess.call([tool, '--check-only']): |
| 109 | return [ |
| 110 | output_api.PresubmitError( |
Hector Dearman | 55ef3e0 | 2018-04-11 17:28:55 +0100 | [diff] [blame] | 111 | 'perfetto_config.proto or perfetto_trace.proto is out of ' + |
| 112 | 'date. Please run ' + tool + ' to update it.') |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 113 | ] |
| 114 | return [] |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 115 | |
| 116 | |
| 117 | # Prevent removing or changing lines in event_whitelist. |
| 118 | def CheckWhitelist(input_api, output_api): |
| 119 | for f in input_api.AffectedFiles(): |
| 120 | if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist': |
| 121 | continue |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 122 | if any((not new_line.startswith('removed')) |
Isabelle Taylor | 8d49fa0 | 2018-10-26 13:23:00 +0100 | [diff] [blame] | 123 | and new_line != old_line for old_line, new_line |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 124 | in itertools.izip(f.OldContents(), f.NewContents())): |
| 125 | return [ |
| 126 | output_api.PresubmitError( |
| 127 | 'event_whitelist only has two supported changes: ' |
| 128 | 'appending a new line, and replacing a line with removed.' |
| 129 | ) |
| 130 | ] |
| 131 | return [] |