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 os
6import parse_deps
7import re
8
9srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))
10
11def GritCheck():
12  filenames = [os.path.join(srcdir, x) for x in [
13      "base.js",
14      "about_tracing/profiling_view.js"]]
15
16  grit_files = []
17  load_sequence = parse_deps.calc_load_sequence(filenames, srcdir)
18  for module in load_sequence:
19    for style_sheet in module.style_sheets:
20      # I'm assuming we only have url()'s associated with images
21      grit_files.extend(re.findall(
22          'url\((?:["|\']?)([^"\'()]*)(?:["|\']?)\)',
23          style_sheet.contents))
24
25  for idx, filename in enumerate(grit_files):
26    while filename.startswith("../"):
27      filename = filename[3:]
28    grit_files[idx] = "src/" + filename
29
30  known_images = []
31  for (dirpath, dirnames, filenames) in os.walk('src/images'):
32    for name in filenames:
33      known_images.append(os.path.join(dirpath, name))
34
35  u = set(grit_files).union(set(known_images))
36  i = set(grit_files).intersection(set(known_images))
37  diff = list(u - i)
38
39  if len(diff) == 0:
40    return ''
41
42  error = 'Entries in CSS url()s do not match files in src/images:\n'
43  in_grit_only = list(set(grit_files) - set(known_images))
44  in_known_only = list(set(known_images) - set(grit_files))
45
46  if len(in_grit_only) > 0:
47    error += ' In CSS urls()s only:\n   ' + '\n   '.join(sorted(in_grit_only))
48  if len(in_known_only) > 0:
49    if len(in_grit_only) > 0:
50      error += '\n\n'
51    error += ' In src/images only:\n   ' + '\n   '.join(sorted(in_known_only))
52
53  return error
54