• 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
16import os
17import pathlib
18import sys
19
20ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
21
22
23def create_if_not_exists(path):
24  create = not os.path.exists(path)
25  if create:
26    print('Creating empty file {}'.format(os.path.relpath(path, ROOT_DIR)))
27    with open(path, 'a'):
28      pass
29  return create
30
31
32def stdout_write(*args, **kwargs):
33  sys.stdout.write(*args, **kwargs)
34  sys.stdout.flush()
35
36
37def main():
38  test_dir = os.path.join(ROOT_DIR, 'test', 'trace_processor')
39  include_index_path = os.path.join(test_dir, 'include_index')
40
41  if not os.path.exists(include_index_path):
42    print('Error: include index does not exist at {}'.format(
43        os.path.relpath(include_index_path, ROOT_DIR)))
44    return 1
45
46  existing_folders = []
47  with open(include_index_path, 'r') as include_index_file:
48    for line in include_index_file.readlines():
49      stripped = line.rstrip()
50      existing_folders.append(stripped.replace('/index', ''))
51
52  print('Pick a folder to add a test to. This can either be an existing '
53        'folder or a new one to create.')
54  print()
55  print('Picking the correct folder is important to the long term health '
56        'of trace processor. For help in this, please see the guidance at '
57        'http://perfetto.dev/docs/analysis/trace-processor#diff-tests')
58  print()
59  print('Existing folders: {}.'.format(existing_folders))
60  stdout_write('Folder: ')
61
62  chosen_folder = sys.stdin.readline().rstrip()
63  chosen_folder_path = os.path.abspath(os.path.join(test_dir, chosen_folder))
64  chosen_folder_path_rel_root = os.path.relpath(chosen_folder_path, ROOT_DIR)
65  if chosen_folder not in existing_folders:
66    print('Creating new folder {} and adding include to include_index file'
67          .format(chosen_folder))
68    os.mkdir(chosen_folder_path)
69
70    out_include_index = list(map(lambda x: x + '/index', existing_folders))
71    out_include_index.append(chosen_folder + '/index')
72    out_include_index.sort()
73
74    with open(include_index_path, 'w') as include_index_file:
75      include_index_file.write('\n'.join(out_include_index))
76      include_index_file.write('\n')
77
78  print()
79  stdout_write('Pick the type of trace to be added [proto/textproto/python]: ')
80  trace_type = sys.stdin.readline().rstrip()
81
82  print()
83  trace_file = ''
84  if trace_type == 'proto':
85    print('Proto traces should be added to the test_data GCS bucket '
86          'using tools/test_data upload')
87    stdout_write('Provide the name of the trace (including any '
88                 'extension) relative to test/data: ')
89
90    pb_file = sys.stdin.readline().rstrip()
91    pb_path = os.path.abspath(os.path.join(ROOT_DIR, 'test', 'data', pb_file))
92    if not os.path.exists(pb_path):
93      print('Error: provided pb file {} does not exist',
94            os.path.relpath(pb_path, ROOT_DIR))
95      return 1
96
97    trace_file = os.path.relpath(pb_path, chosen_folder_path)
98  elif trace_type == 'textproto':
99    print('Provide the path to the textproto trace relative to the '
100          'chosen folder {}'.format(chosen_folder_path_rel_root))
101    stdout_write(
102        'If the file does not already exist, an empty file will be created: ')
103
104    textproto_file = sys.stdin.readline().rstrip()
105    textproto_path = os.path.abspath(
106        os.path.join(chosen_folder_path, textproto_file))
107    create_if_not_exists(textproto_path)
108
109    trace_file = textproto_file
110  elif trace_type == 'python':
111    print(
112        'Provide the path to the Python trace '
113        'relative to the chosen folder {}'.format(chosen_folder_path_rel_root))
114    stdout_write(
115        'If the file does not already exist, an empty file will be created: ')
116
117    python_file = sys.stdin.readline().rstrip()
118    python_path = os.path.abspath(os.path.join(chosen_folder_path, python_file))
119    if create_if_not_exists(python_path):
120      print('For examples of how Python traces are constructed, '
121            'check the existing traces in test/trace_processor')
122
123    trace_file = python_file
124  else:
125    print('Error: unexpected trace type {}'.format(trace_type))
126    return 1
127
128  print()
129  print(
130      'Provide either the name of a built-in metric OR path to the file (with '
131      'extension .sql) relative to the chosen folder {}'.format(
132          chosen_folder_path_rel_root))
133  stdout_write(
134      'If the file does not already exist, an empty file will be created: ')
135
136  sql_file_or_metric = sys.stdin.readline().rstrip()
137  if sql_file_or_metric.endswith('.sql'):
138    sql_path = os.path.abspath(
139        os.path.join(chosen_folder_path, sql_file_or_metric))
140    create_if_not_exists(sql_path)
141
142  default_out_file = '{}_{}.out'.format(
143      pathlib.Path(trace_file).stem,
144      pathlib.Path(sql_file_or_metric).stem)
145
146  print()
147  print('Provide the name of the output file (or leave empty '
148        'to accept the default: {})'.format(default_out_file))
149  stdout_write(
150      'If the file does not already exist, an empty file will be created: ')
151  out_file = sys.stdin.readline().rstrip()
152  if not out_file:
153    out_file = default_out_file
154
155  out_path = os.path.abspath(os.path.join(chosen_folder_path, out_file))
156  create_if_not_exists(out_path)
157
158  print()
159  print('Appending test to index file')
160  with open(os.path.join(chosen_folder_path, 'index'), 'a') as index_file:
161    index_file.write('{} {} {}\n'.format(trace_file, sql_file_or_metric,
162                                         out_file))
163
164  return 0
165
166
167if __name__ == '__main__':
168  sys.exit(main())
169