1# Copyright (c) 2015 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
7
8
9def _AddToPathIfNeeded(path):
10  if path not in sys.path:
11    sys.path.insert(0, path)
12
13
14def UpdateSysPathIfNeeded():
15  p = DashboardProject()
16
17  _AddToPathIfNeeded(p.catapult_third_party_path)
18  _AddToPathIfNeeded(p.catapult_path)
19  _AddToPathIfNeeded(p.tracing_root_path)
20  import tracing_project
21  tracing_project.UpdateSysPathIfNeeded()
22
23
24def _FindAllFilesRecursive(source_paths):
25  assert isinstance(source_paths, list)
26  all_filenames = set()
27  for source_path in source_paths:
28    for dirpath, _, filenames in os.walk(source_path):
29      for f in filenames:
30        if f.startswith('.'):
31          continue
32        x = os.path.abspath(os.path.join(dirpath, f))
33        all_filenames.add(x)
34  return all_filenames
35
36
37def _IsFilenameATest(x):
38  if x.endswith('-test.html'):
39    return True
40  if x.endswith('_test.html'):
41    return True
42  return False
43
44
45class DashboardProject(object):
46  catapult_path = os.path.abspath(
47      os.path.join(os.path.dirname(__file__), '..'))
48
49  catapult_third_party_path = os.path.join(catapult_path, 'third_party')
50
51  dashboard_root_path = os.path.join(catapult_path, 'dashboard')
52  dashboard_src_path = os.path.join(dashboard_root_path, 'dashboard')
53  dashboard_test_data_path = os.path.join(dashboard_root_path, 'test_data')
54  dashboard_polymer_path = os.path.join(catapult_third_party_path, 'polymer')
55
56  tracing_root_path = os.path.join(catapult_path, 'tracing')
57
58  def __init__(self):
59    self._source_paths = None
60
61  @property
62  def source_paths(self):
63    # We lazily init source_paths to resolve this cyclic dependency
64    # (See perf_insights_project.py).
65    if self._source_paths is None:
66      self._source_paths = []
67      self._source_paths.append(self.dashboard_root_path)
68      self._source_paths.append(self.dashboard_polymer_path)
69      self._source_paths.append(self.catapult_third_party_path)
70
71      import tracing_project as tracing_project_module
72      tracing_project = tracing_project_module.TracingProject()
73      self._source_paths.extend(tracing_project.source_paths)
74
75    return self._source_paths
76
77  def FindAllTestModuleRelPaths(self, pred=None):
78    if pred is None:
79      pred = lambda x: True
80    all_filenames = _FindAllFilesRecursive([self.dashboard_src_path])
81    test_module_filenames = [x for x in all_filenames if
82                             _IsFilenameATest(x) and pred(x)]
83    test_module_filenames.sort()
84
85    return [os.path.relpath(x, self.dashboard_root_path)
86            for x in test_module_filenames]
87