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 argparse 17import os 18import subprocess 19import sys 20 21 22def run(encode_or_decode, protoc_path, proto, root, input, output): 23 cmd = [ 24 protoc_path, 25 '--{}=perfetto.protos.{}'.format(encode_or_decode, proto), 26 os.path.join(root, 'protos/perfetto/config/trace_config.proto'), 27 os.path.join(root, 'protos/perfetto/trace/trace.proto'), 28 '--proto_path={}/protos'.format(root), 29 ] 30 subprocess.check_call(cmd, stdin=input, stdout=output, stderr=sys.stderr) 31 32 33def main(): 34 parser = argparse.ArgumentParser() 35 parser.add_argument( 36 'encode_or_decode', 37 choices=['encode', 'decode'], 38 help='encode into binary format or decode to text.' 39 ) 40 parser.add_argument('--proto_name', default='TraceConfig', 41 help='name of proto to encode/decode (default: TraceConfig).') 42 parser.add_argument('--protoc_path', default=None, 43 help='path to protoc') 44 parser.add_argument('--root', default='.', 45 help='root directory (default: "protos")') 46 parser.add_argument('--input', default='-', 47 help='input file, or "-" for stdin (default: "-")') 48 parser.add_argument('--output', default='-', 49 help='output file, or "-" for stdout (default: "-")') 50 args = parser.parse_args() 51 52 encode_or_decode = args.encode_or_decode 53 proto_name = args.proto_name 54 protoc_path = args.protoc_path 55 root = args.root 56 input = sys.stdin if args.input == '-' else open(args.input, 'rb') 57 output = sys.stdout if args.output == '-' else open(args.output, 'wb') 58 if not protoc_path: 59 directory = os.path.dirname(__file__) 60 protoc_path = os.path.join(directory, 'gcc_like_host', 'protoc') 61 run(encode_or_decode, protoc_path, proto_name, root, input, output) 62 return 0 63 64 65if __name__ == '__main__': 66 sys.exit(main()) 67