• 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
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    extension_descriptors = [
49        chrome_extension_descriptor_path, test_extensions_descriptor_path
50    ]
51  elif args.descriptor and not args.out:
52    trace_descriptor_path = args.descriptor
53    extension_descriptors = []
54  else:
55    raise RuntimeError(
56        'Exactly one of --out and --descriptor should be provided')
57
58  trace_path = args.trace_path
59
60  if trace_path.endswith('.py'):
61    serialize_python_trace(ROOT_DIR, trace_descriptor_path, trace_path,
62                           sys.stdout.buffer)
63  elif trace_path.endswith('.textproto'):
64    serialize_textproto_trace(trace_descriptor_path, extension_descriptors,
65                              trace_path, sys.stdout.buffer)
66  else:
67    raise RuntimeError('Invalid extension for unserialized trace file')
68
69  return 0
70
71
72if __name__ == '__main__':
73  sys.exit(main())
74