xref: /aosp_15_r20/external/perfetto/tools/serialize_test_trace.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import argparse
21import os
22import sys
23
24ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
25sys.path.append(ROOT_DIR)
26
27from python.generators.diff_tests.utils import serialize_textproto_trace, serialize_python_trace
28
29
30def main():
31  parser = argparse.ArgumentParser()
32  parser.add_argument(
33      '--out', type=str, help='out directory to search for trace descriptor')
34  parser.add_argument(
35      '--descriptor', type=str, help='path to the trace descriptor')
36  parser.add_argument('trace_path', type=str, help='path of trace to serialize')
37  args = parser.parse_args()
38
39  if args.out and not args.descriptor:
40    trace_protos_path = os.path.join(args.out, 'gen', 'protos', 'perfetto',
41                                     'trace')
42    chrome_extension_descriptor_path = os.path.join(
43        args.out, 'gen', 'protos', 'third_party', 'chromium',
44        'chrome_track_event.descriptor')
45    trace_descriptor_path = os.path.join(trace_protos_path, 'trace.descriptor')
46    test_extensions_descriptor_path = os.path.join(
47        trace_protos_path, 'test_extensions.descriptor')
48    winscope_extensions_descriptor_path = os.path.join(trace_protos_path,
49                                                       'android',
50                                                       'winscope.descriptor')
51    extension_descriptors = [
52        chrome_extension_descriptor_path, test_extensions_descriptor_path,
53        winscope_extensions_descriptor_path
54    ]
55  elif args.descriptor and not args.out:
56    trace_descriptor_path = args.descriptor
57    extension_descriptors = []
58  else:
59    raise RuntimeError(
60        'Exactly one of --out and --descriptor should be provided')
61
62  trace_path = args.trace_path
63
64  if trace_path.endswith('.py'):
65    serialize_python_trace(ROOT_DIR, trace_descriptor_path, trace_path,
66                           sys.stdout.buffer)
67  elif trace_path.endswith('.textproto'):
68    serialize_textproto_trace(trace_descriptor_path, extension_descriptors,
69                              trace_path, sys.stdout.buffer)
70  else:
71    raise RuntimeError('Invalid extension for unserialized trace file')
72
73  return 0
74
75
76if __name__ == '__main__':
77  sys.exit(main())
78