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 | |
Anna Mayzner | f60f53d | 2023-01-13 15:46:00 +0000 | [diff] [blame] | 24 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 25 | sys.path.append(ROOT_DIR) |
| 26 | |
| 27 | from python.generators.diff_tests.utils import serialize_textproto_trace, serialize_python_trace |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def main(): |
| 31 | parser = argparse.ArgumentParser() |
| 32 | parser.add_argument( |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 33 | '--out', type=str, help='out directory to search for trace descriptor') |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 34 | parser.add_argument( |
| 35 | '--descriptor', type=str, help='path to the trace descriptor') |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 36 | parser.add_argument('trace_path', type=str, help='path of trace to serialize') |
| 37 | args = parser.parse_args() |
| 38 | |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 39 | if args.out and not args.descriptor: |
| 40 | trace_protos_path = os.path.join(args.out, 'gen', 'protos', 'perfetto', |
| 41 | 'trace') |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 42 | chrome_extension_descriptor_path = os.path.join( |
| 43 | args.out, 'gen', 'protos', 'third_party', 'chromium', |
| 44 | 'chrome_track_event.descriptor') |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 45 | trace_descriptor_path = os.path.join(trace_protos_path, 'trace.descriptor') |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 46 | test_extensions_descriptor_path = os.path.join( |
| 47 | trace_protos_path, 'test_extensions.descriptor') |
| 48 | extension_descriptors = [ |
| 49 | chrome_extension_descriptor_path, test_extensions_descriptor_path |
| 50 | ] |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 51 | elif args.descriptor and not args.out: |
| 52 | trace_descriptor_path = args.descriptor |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 53 | extension_descriptors = [] |
Lalit Maganti | df50009 | 2020-07-16 23:56:24 +0100 | [diff] [blame] | 54 | else: |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 55 | raise RuntimeError( |
| 56 | 'Exactly one of --out and --descriptor should be provided') |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 57 | |
| 58 | trace_path = args.trace_path |
| 59 | |
| 60 | if trace_path.endswith('.py'): |
Anna Mayzner | f60f53d | 2023-01-13 15:46:00 +0000 | [diff] [blame] | 61 | serialize_python_trace(ROOT_DIR, trace_descriptor_path, trace_path, |
| 62 | sys.stdout.buffer) |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 63 | elif trace_path.endswith('.textproto'): |
Andrew Shulaev | 94d1bf2 | 2021-01-15 15:30:42 +0000 | [diff] [blame] | 64 | serialize_textproto_trace(trace_descriptor_path, extension_descriptors, |
| 65 | trace_path, sys.stdout.buffer) |
Lalit Maganti | 5eacf42 | 2020-06-30 13:51:23 +0100 | [diff] [blame] | 66 | else: |
| 67 | raise RuntimeError('Invalid extension for unserialized trace file') |
| 68 | |
| 69 | return 0 |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | sys.exit(main()) |