• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
24from proto_utils import serialize_python_trace, serialize_textproto_trace
25
26
27def main():
28  parser = argparse.ArgumentParser()
29  parser.add_argument(
30      '--out',
31      type=str,
32      help='out directory to search for trace descriptor')
33  parser.add_argument(
34      '--descriptor', type=str, help='path to the trace descriptor')
35  parser.add_argument('trace_path', type=str, help='path of trace to serialize')
36  args = parser.parse_args()
37
38  if args.out and not args.descriptor:
39    trace_protos_path = os.path.join(args.out, 'gen', 'protos', 'perfetto',
40                                     'trace')
41    chrome_extension_descriptor_path = os.path.join(
42        args.out, 'gen', 'protos', 'third_party', 'chromium',
43        'chrome_track_event.descriptor')
44    trace_descriptor_path = os.path.join(trace_protos_path, 'trace.descriptor')
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    ]
50  elif args.descriptor and not args.out:
51    trace_descriptor_path = args.descriptor
52    extension_descriptors = []
53  else:
54    raise RuntimeError(
55        'Exactly one of --out and --descriptor should be provided')
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'):
62    serialize_textproto_trace(trace_descriptor_path, extension_descriptors,
63                              trace_path, sys.stdout.buffer)
64  else:
65    raise RuntimeError('Invalid extension for unserialized trace file')
66
67  return 0
68
69
70if __name__ == '__main__':
71  sys.exit(main())
72