Hector Dearman | fcbafda | 2018-01-18 11:13:57 +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 argparse |
| 17 | import os |
| 18 | import subprocess |
| 19 | import sys |
| 20 | |
Primiano Tucci | 098f095 | 2019-09-03 10:21:20 +0100 | [diff] [blame] | 21 | ROOT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) |
Hector Dearman | fcbafda | 2018-01-18 11:13:57 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def main(): |
| 25 | parser = argparse.ArgumentParser() |
| 26 | parser.add_argument( |
| 27 | 'encode_or_decode', |
| 28 | choices=['encode', 'decode'], |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame^] | 29 | help='encode into binary format or decode to text.') |
| 30 | parser.add_argument( |
| 31 | '--proto_name', |
| 32 | default='TraceConfig', |
Hector Dearman | fcbafda | 2018-01-18 11:13:57 +0000 | [diff] [blame] | 33 | help='name of proto to encode/decode (default: TraceConfig).') |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame^] | 34 | parser.add_argument( |
| 35 | '--protoc', default='protoc', help='Path to the protoc executable') |
| 36 | parser.add_argument( |
| 37 | '--input', |
| 38 | default='-', |
Hector Dearman | fcbafda | 2018-01-18 11:13:57 +0000 | [diff] [blame] | 39 | help='input file, or "-" for stdin (default: "-")') |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame^] | 40 | parser.add_argument( |
| 41 | '--output', |
| 42 | default='-', |
Hector Dearman | fcbafda | 2018-01-18 11:13:57 +0000 | [diff] [blame] | 43 | help='output file, or "-" for stdout (default: "-")') |
| 44 | args = parser.parse_args() |
| 45 | |
Primiano Tucci | 098f095 | 2019-09-03 10:21:20 +0100 | [diff] [blame] | 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) |
Hector Dearman | fcbafda | 2018-01-18 11:13:57 +0000 | [diff] [blame] | 56 | return 0 |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | sys.exit(main()) |