1# Copyright (c) 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import sys 6import os 7import re 8 9 10def _AddToPathIfNeeded(path): 11 if path not in sys.path: 12 sys.path.insert(0, path) 13 14 15def UpdateSysPathIfNeeded(): 16 p = TracingProject() 17 _AddToPathIfNeeded(p.catapult_path) 18 _AddToPathIfNeeded(p.py_vulcanize_path) 19 _AddToPathIfNeeded(p.vinn_path) 20 21 _AddToPathIfNeeded(os.path.join(p.catapult_third_party_path, 'WebOb')) 22 _AddToPathIfNeeded(os.path.join(p.catapult_third_party_path, 'Paste')) 23 _AddToPathIfNeeded(os.path.join(p.catapult_third_party_path, 'six')) 24 _AddToPathIfNeeded(os.path.join(p.catapult_third_party_path, 'webapp2')) 25 26 27def _FindAllFilesRecursive(source_paths): 28 assert isinstance(source_paths, list) 29 all_filenames = set() 30 for source_path in source_paths: 31 for dirpath, _, filenames in os.walk(source_path): 32 for f in filenames: 33 if f.startswith('.'): 34 continue 35 x = os.path.abspath(os.path.join(dirpath, f)) 36 all_filenames.add(x) 37 return all_filenames 38 39def _IsFilenameATest(x): 40 if x.endswith('_test.js'): 41 return True 42 43 if x.endswith('_test.html'): 44 return True 45 46 if x.endswith('_unittest.js'): 47 return True 48 49 if x.endswith('_unittest.html'): 50 return True 51 52 # TODO(nduca): Add content test? 53 return False 54 55 56class TracingProject(object): 57 catapult_path = os.path.abspath( 58 os.path.join(os.path.dirname(__file__), os.path.pardir)) 59 60 tracing_root_path = os.path.join(catapult_path, 'tracing') 61 trace_processor_root_path = os.path.join(catapult_path, 'trace_processor') 62 tracing_src_path = os.path.join(tracing_root_path, 'tracing') 63 extras_path = os.path.join(tracing_src_path, 'extras') 64 ui_extras_path = os.path.join(tracing_src_path, 'ui', 'extras') 65 66 catapult_third_party_path = os.path.join(catapult_path, 'third_party') 67 polymer_path = os.path.join(catapult_third_party_path, 'polymer') 68 69 tracing_third_party_path = os.path.join(tracing_root_path, 'third_party') 70 py_vulcanize_path = os.path.join(catapult_third_party_path, 'py_vulcanize') 71 vinn_path = os.path.join(catapult_third_party_path, 'vinn') 72 73 jszip_path = os.path.join(tracing_third_party_path, 'jszip') 74 75 glmatrix_path = os.path.join( 76 tracing_third_party_path, 'gl-matrix', 'dist') 77 78 mannwhitneyu_path = os.path.join( 79 tracing_third_party_path, 'mannwhitneyu') 80 81 ui_path = os.path.join(tracing_src_path, 'ui') 82 d3_path = os.path.join(tracing_third_party_path, 'd3') 83 chai_path = os.path.join(tracing_third_party_path, 'chai') 84 mocha_path = os.path.join(tracing_third_party_path, 'mocha') 85 oboe_path = os.path.join(tracing_third_party_path, 'oboe') 86 87 mre_path = os.path.join(tracing_src_path, 'mre') 88 89 metrics_path = os.path.join(tracing_src_path, 'metrics') 90 diagnostics_path = os.path.join(tracing_src_path, 'value', 'diagnostics') 91 92 value_ui_path = os.path.join(tracing_src_path, 'value', 'ui') 93 metrics_ui_path = os.path.join(tracing_src_path, 'metrics', 'ui') 94 95 test_data_path = os.path.join(tracing_root_path, 'test_data') 96 skp_data_path = os.path.join(tracing_root_path, 'skp_data') 97 98 rjsmin_path = os.path.join( 99 tracing_third_party_path, 'tvcm', 'third_party', 'rjsmin') 100 rcssmin_path = os.path.join( 101 tracing_third_party_path, 'tvcm', 'third_party', 'rcssmin') 102 103 def __init__(self): 104 self.source_paths = [] 105 self.source_paths.append(self.tracing_root_path) 106 self.source_paths.append(self.polymer_path) 107 self.source_paths.append(self.tracing_third_party_path) 108 self.source_paths.append(self.mre_path) 109 self.source_paths.append(self.jszip_path) 110 self.source_paths.append(self.glmatrix_path) 111 self.source_paths.append(self.mannwhitneyu_path) 112 self.source_paths.append(self.d3_path) 113 self.source_paths.append(self.chai_path) 114 self.source_paths.append(self.mocha_path) 115 self.source_paths.append(self.oboe_path) 116 117 def CreateVulcanizer(self): 118 from py_vulcanize import project as project_module 119 return project_module.Project(self.source_paths) 120 121 def IsD8CompatibleFile(self, filename): 122 if filename.startswith(self.ui_path): 123 return False 124 125 if filename.startswith(self.value_ui_path): 126 return False 127 128 if filename.startswith(self.metrics_ui_path): 129 return False 130 131 return True 132 133 def FindAllTestModuleRelPaths(self, pred=None): 134 if pred is None: 135 pred = lambda x: True 136 137 all_filenames = _FindAllFilesRecursive([self.tracing_src_path]) 138 test_module_filenames = [x for x in all_filenames if 139 _IsFilenameATest(x) and pred(x)] 140 test_module_filenames.sort() 141 142 return [os.path.relpath(x, self.tracing_root_path) 143 for x in test_module_filenames] 144 145 def FindAllMetricsModuleRelPaths(self): 146 all_filenames = _FindAllFilesRecursive([self.tracing_src_path]) 147 all_metrics_module_filenames = [] 148 for x in all_filenames: 149 if x.startswith(self.metrics_path) and not _IsFilenameATest(x): 150 all_metrics_module_filenames.append(x) 151 all_metrics_module_filenames.sort() 152 return [os.path.relpath(x, self.tracing_root_path) 153 for x in all_metrics_module_filenames] 154 155 def FindAllDiagnosticsModuleRelPaths(self): 156 all_filenames = _FindAllFilesRecursive([self.tracing_src_path]) 157 all_diagnostics_module_filenames = [] 158 for x in all_filenames: 159 if x.startswith(self.diagnostics_path) and not _IsFilenameATest(x): 160 all_diagnostics_module_filenames.append(x) 161 all_diagnostics_module_filenames.sort() 162 return [os.path.relpath(x, self.tracing_root_path) 163 for x in all_diagnostics_module_filenames] 164 165 def FindAllD8TestModuleRelPaths(self): 166 return self.FindAllTestModuleRelPaths(pred=self.IsD8CompatibleFile) 167 168 def GetConfigNames(self): 169 config_files = [ 170 os.path.join(self.ui_extras_path, x) 171 for x in os.listdir(self.ui_extras_path) 172 if x.endswith('_config.html') 173 ] 174 175 config_files = [x for x in config_files if os.path.isfile(x)] 176 177 config_basenames = [os.path.basename(x) for x in config_files] 178 config_names = [re.match('(.+)_config.html$', x).group(1) 179 for x in config_basenames] 180 return config_names 181 182 def GetDefaultConfigName(self): 183 assert 'full' in self.GetConfigNames() 184 return 'full' 185 186 def AddConfigNameOptionToParser(self, parser): 187 choices = self.GetConfigNames() 188 parser.add_argument( 189 '--config', dest='config_name', 190 choices=choices, default=self.GetDefaultConfigName(), 191 help='Picks a browser config. Valid choices: %s' % ', '.join(choices)) 192 return choices 193 194 def GetModuleNameForConfigName(self, config_name): 195 return 'tracing.ui.extras.%s_config' % config_name 196 197