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