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 16import os 17import re 18import subprocess 19import sys 20 21PROTOS = ( 22 'protos/perfetto/config/chrome/chrome_config.proto', 23 'protos/perfetto/config/inode_file/inode_file_config.proto', 24 'protos/perfetto/config/process_stats/process_stats_config.proto', 25 'protos/perfetto/config/data_source_config.proto', 26 'protos/perfetto/config/ftrace/ftrace_config.proto', 27 'protos/perfetto/config/test_config.proto', 28 'protos/perfetto/config/trace_config.proto', 29) 30 31MERGED_OUT_PROTO = 'protos/perfetto/config/perfetto_config.proto' 32 33REPLACEMENT_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. 42 43syntax = "proto2"; 44 45package perfetto.protos; 46''' 47 48def 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() 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__ 62 content = re.sub(r'^import.*?\n', '', content, flags=re.MULTILINE) 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 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 86if __name__ == '__main__': 87 sys.exit(main()) 88