1# Copyright (C) 2018 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 15import argparse 16import os 17import subprocess 18import sys 19 20ROOT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 21 22 23def main(): 24 parser = argparse.ArgumentParser() 25 parser.add_argument( 26 'encode_or_decode', 27 choices=['encode', 'decode'], 28 help='encode into binary format or decode to text.') 29 parser.add_argument( 30 '--proto_name', 31 default='TraceConfig', 32 help='name of proto to encode/decode (default: TraceConfig).') 33 parser.add_argument( 34 '--protoc', default='protoc', help='Path to the protoc executable') 35 parser.add_argument( 36 '--input', 37 default='-', 38 help='input file, or "-" for stdin (default: "-")') 39 parser.add_argument( 40 '--output', 41 default='-', 42 help='output file, or "-" for stdout (default: "-")') 43 args = parser.parse_args() 44 45 cmd = [ 46 args.protoc, 47 '--%s=perfetto.protos.%s' % (args.encode_or_decode, args.proto_name), 48 '--proto_path=%s' % ROOT_DIR, 49 os.path.join(ROOT_DIR, 'protos/perfetto/config/trace_config.proto'), 50 os.path.join(ROOT_DIR, 'protos/perfetto/trace/trace.proto'), 51 ] 52 in_file = sys.stdin if args.input == '-' else open(args.input, 'rb') 53 out_file = sys.stdout if args.output == '-' else open(args.output, 'wb') 54 subprocess.check_call(cmd, stdin=in_file, stdout=out_file, stderr=sys.stderr) 55 return 0 56 57 58if __name__ == '__main__': 59 sys.exit(main()) 60