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 = ( |
Oystein Eftevaag | ab3b1b2 | 2018-03-08 16:27:06 -0800 | [diff] [blame] | 22 | 'protos/perfetto/config/chrome/chrome_config.proto', |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 23 | 'protos/perfetto/config/inode_file/inode_file_config.proto', |
Hector Dearman | 1b9c58a | 2018-03-29 18:45:06 +0100 | [diff] [blame] | 24 | 'protos/perfetto/config/process_stats/process_stats_config.proto', |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 25 | 'protos/perfetto/config/data_source_config.proto', |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 26 | 'protos/perfetto/config/ftrace/ftrace_config.proto', |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 27 | 'protos/perfetto/config/test_config.proto', |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 28 | 'protos/perfetto/config/trace_config.proto', |
| 29 | ) |
| 30 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 31 | MERGED_OUT_PROTO = 'protos/perfetto/config/perfetto_config.proto' |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 32 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 33 | REPLACEMENT_HEADER = ''' |
| 34 | // AUTOGENERATED - DO NOT EDIT |
| 35 | // --------------------------- |
| 36 | // This file has been generated by |
| 37 | // AOSP://external/perfetto/%s |
| 38 | // merging the perfetto config protos. |
| 39 | // This fused proto is intended to be copied in: |
| 40 | // - Android tree, for statsd. |
| 41 | // - Google internal repos. |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 42 | |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 43 | syntax = "proto2"; |
| 44 | |
| 45 | package perfetto.protos; |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 46 | ''' |
| 47 | |
| 48 | def main(): |
| 49 | root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 50 | merged_content = '' |
| 51 | for proto in PROTOS: |
| 52 | path = os.path.join(root_dir, proto) |
| 53 | with open(path) as f: |
| 54 | content = f.read() |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 55 | |
| 56 | # Remove header |
| 57 | header = re.match(r'\/\*(?:.|\s)*?package.*;\n', content) |
| 58 | header = header.group(0) |
| 59 | content = content[len(header):] |
| 60 | if merged_content == '': |
| 61 | merged_content += REPLACEMENT_HEADER.lstrip() % __file__ |
Oystein Eftevaag | ab3b1b2 | 2018-03-08 16:27:06 -0800 | [diff] [blame] | 62 | content = re.sub(r'^import.*?\n', '', content, flags=re.MULTILINE) |
Primiano Tucci | 08882a4 | 2018-01-19 22:09:01 +0000 | [diff] [blame] | 63 | content = re.sub(r'TODO\([^)]*\):', 'TODO:', content) |
| 64 | merged_content += '\n// Begin of %s\n' % proto |
| 65 | merged_content += content |
| 66 | merged_content += '\n// End of %s\n' % proto |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 67 | |
| 68 | out_path = os.path.join(root_dir, MERGED_OUT_PROTO) |
| 69 | |
| 70 | prev_content = None |
| 71 | if os.path.exists(out_path): |
| 72 | with open(out_path, 'rb') as fprev: |
| 73 | prev_content = fprev.read() |
| 74 | |
| 75 | if prev_content == merged_content: |
| 76 | return 0 |
| 77 | |
| 78 | if '--check-only' in sys.argv: |
| 79 | return 1 |
| 80 | |
| 81 | print 'Updating %s' % MERGED_OUT_PROTO |
| 82 | with open(out_path, 'wb') as fout: |
| 83 | fout.write(merged_content) |
| 84 | return 0 |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | sys.exit(main()) |