Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2018 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import re |
| 18 | import subprocess |
| 19 | import sys |
| 20 | |
| 21 | PROTOS = ( |
| 22 | 'protos/perfetto/config/data_source_config.proto', |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 23 | 'protos/perfetto/config/ftrace/ftrace_config.proto', |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 24 | 'protos/perfetto/config/trace_config.proto', |
| 25 | ) |
| 26 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 27 | MERGED_OUT_PROTO = 'protos/perfetto/config/perfetto_config.proto' |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 28 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 29 | REPLACEMENT_HEADER = ''' |
| 30 | // AUTOGENERATED - DO NOT EDIT |
| 31 | // --------------------------- |
| 32 | // This file has been generated by |
| 33 | // AOSP://external/perfetto/%s |
| 34 | // merging the perfetto config protos. |
| 35 | // This fused proto is intended to be copied in: |
| 36 | // - Android tree, for statsd. |
| 37 | // - Google internal repos. |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 38 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 39 | syntax = "proto2"; |
| 40 | |
| 41 | package perfetto.protos; |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 42 | ''' |
| 43 | |
| 44 | def main(): |
| 45 | root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 46 | merged_content = '' |
| 47 | for proto in PROTOS: |
| 48 | path = os.path.join(root_dir, proto) |
| 49 | with open(path) as f: |
| 50 | content = f.read() |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 51 | |
| 52 | # Remove header |
| 53 | header = re.match(r'\/\*(?:.|\s)*?package.*;\n', content) |
| 54 | header = header.group(0) |
| 55 | content = content[len(header):] |
| 56 | if merged_content == '': |
| 57 | merged_content += REPLACEMENT_HEADER.lstrip() % __file__ |
| 58 | content = re.sub(r'\nimport.*?\n', '', content) |
| 59 | content = re.sub(r'TODO\([^)]*\):', 'TODO:', content) |
| 60 | merged_content += '\n// Begin of %s\n' % proto |
| 61 | merged_content += content |
| 62 | merged_content += '\n// End of %s\n' % proto |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 63 | |
| 64 | out_path = os.path.join(root_dir, MERGED_OUT_PROTO) |
| 65 | |
| 66 | prev_content = None |
| 67 | if os.path.exists(out_path): |
| 68 | with open(out_path, 'rb') as fprev: |
| 69 | prev_content = fprev.read() |
| 70 | |
| 71 | if prev_content == merged_content: |
| 72 | return 0 |
| 73 | |
| 74 | if '--check-only' in sys.argv: |
| 75 | return 1 |
| 76 | |
| 77 | print 'Updating %s' % MERGED_OUT_PROTO |
| 78 | with open(out_path, 'wb') as fout: |
| 79 | fout.write(merged_content) |
| 80 | return 0 |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | sys.exit(main()) |