1# Copyright (c) 2013 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
8tracing_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
9                                            '..', '..'))
10if tracing_path not in sys.path:
11  sys.path.append(tracing_path)
12
13from tracing import tracing_project
14
15
16FILE_GROUPS = ["tracing_css_files",
17               "tracing_js_html_files",
18               "tracing_img_files"]
19
20
21def GetFileGroupFromFileName(filename):
22   extension = os.path.splitext(filename)[1]
23   return {
24       '.css': 'tracing_css_files',
25       '.html': 'tracing_js_html_files',
26       '.js': 'tracing_js_html_files',
27       '.png': 'tracing_img_files'
28   }[extension]
29
30
31def CheckListedFilesSorted(src_file, group_name, listed_files):
32  sorted_files = sorted(listed_files)
33  if sorted_files != listed_files:
34    mismatch = ''
35    for i in range(len(listed_files)):
36      if listed_files[i] != sorted_files[i]:
37        mismatch = listed_files[i]
38        break
39    what_is = '  ' + '\n  '.join(listed_files)
40    what_should_be = '  ' + '\n  '.join(sorted_files)
41    return '''In group {0} from file {1}, filenames aren't sorted.
42
43First mismatch:
44  {2}
45
46Current listing:
47{3}
48
49Correct listing:
50{4}\n\n'''.format(group_name, src_file, mismatch, what_is, what_should_be)
51  else:
52    return ''
53
54
55def GetKnownFiles():
56  p = tracing_project.TracingProject()
57  m = p.loader.LoadModule(module_name='ui.extras.about_tracing.about_tracing')
58  absolute_filenames = m.GetAllDependentFilenamesRecursive(
59      include_raw_scripts=False)
60
61  return list(set([os.path.relpath(f, p.tracing_root_path)
62                   for f in absolute_filenames]))
63
64
65def CheckCommon(file_name, listed_files):
66  tracing_project.TracingProject()
67
68  known_files = GetKnownFiles()
69  u = set(listed_files).union(set(known_files))
70  i = set(listed_files).intersection(set(known_files))
71  diff = list(u - i)
72
73  if len(diff) == 0:
74    return ''
75
76  error = 'Entries in ' + file_name + ' do not match files on disk:\n'
77  in_file_only = list(set(listed_files) - set(known_files))
78  in_known_only = list(set(known_files) - set(listed_files))
79
80  if len(in_file_only) > 0:
81    error += '  In file only:\n    ' + '\n    '.join(sorted(in_file_only))
82  if len(in_known_only) > 0:
83    if len(in_file_only) > 0:
84      error += '\n\n'
85    error += '  On disk only:\n    ' + '\n    '.join(sorted(in_known_only))
86
87  if in_file_only:
88    error += (
89        '\n\n'
90        '  Note: only files actually used in about:tracing should\n'
91        '  be listed in the build files. Try running build/update_gyp_and_gn\n'
92        '  to update the files automatically.')
93
94  return error
95