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