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