Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (C) 2020 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 | from __future__ import absolute_import |
| 17 | from __future__ import division |
| 18 | from __future__ import print_function |
| 19 | |
| 20 | import argparse |
| 21 | import os |
| 22 | import sys |
| 23 | |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 24 | from proto_utils import serialize_python_trace, serialize_textproto_trace |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def main(): |
| 28 | parser = argparse.ArgumentParser() |
| 29 | parser.add_argument( |
| 30 | '--out', |
| 31 | type=str, |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 32 | help='out directory to search for trace descriptor') |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 33 | parser.add_argument( |
| 34 | '--descriptor', type=str, help='path to the trace descriptor') |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 35 | parser.add_argument('trace_path', type=str, help='path of trace to serialize') |
| 36 | args = parser.parse_args() |
| 37 | |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 38 | if args.out and not args.descriptor: |
| 39 | trace_protos_path = os.path.join(args.out, 'gen', 'protos', 'perfetto', |
| 40 | 'trace') |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 41 | chrome_extension_descriptor_path = os.path.join( |
| 42 | args.out, 'gen', 'protos', 'third_party', 'chromium', |
| 43 | 'chrome_track_event.descriptor') |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 44 | trace_descriptor_path = os.path.join(trace_protos_path, 'trace.descriptor') |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 45 | test_extensions_descriptor_path = os.path.join( |
| 46 | trace_protos_path, 'test_extensions.descriptor') |
| 47 | extension_descriptors = [ |
| 48 | chrome_extension_descriptor_path, test_extensions_descriptor_path |
| 49 | ] |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 50 | elif args.descriptor and not args.out: |
| 51 | trace_descriptor_path = args.descriptor |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 52 | extension_descriptors = [] |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 53 | else: |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 54 | raise RuntimeError( |
| 55 | 'Exactly one of --out and --descriptor should be provided') |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 56 | |
| 57 | trace_path = args.trace_path |
| 58 | |
| 59 | if trace_path.endswith('.py'): |
| 60 | serialize_python_trace(trace_descriptor_path, trace_path, sys.stdout.buffer) |
| 61 | elif trace_path.endswith('.textproto'): |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 62 | serialize_textproto_trace(trace_descriptor_path, extension_descriptors, |
| 63 | trace_path, sys.stdout.buffer) |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 64 | else: |
| 65 | raise RuntimeError('Invalid extension for unserialized trace file') |
| 66 | |
| 67 | return 0 |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | sys.exit(main()) |