1#!/usr/bin/python
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from ast import literal_eval
7import os
8
9
10_HERE = os.path.dirname(__file__)
11_SRC_ROOT = os.path.join(_HERE, '..', '..', '..')
12_FROM_SRC = lambda p: os.path.abspath(os.path.join(_SRC_ROOT, p))
13
14
15from sys import path as sys_path
16sys_path.insert(0, os.path.join(_HERE, '..'))
17import processor
18
19
20# High priority code to compile.
21_NEED_TO_COMPILE = map(_FROM_SRC, [
22  'chrome/browser/resources/bookmark_manager',
23  'chrome/browser/resources/downloads',
24  'chrome/browser/resources/extensions',
25  'chrome/browser/resources/help',
26  'chrome/browser/resources/history',
27  'chrome/browser/resources/ntp4',
28  'chrome/browser/resources/options',
29  'chrome/browser/resources/print_preview',
30  'chrome/browser/resources/uber',
31  'ui/webui/resources/js',
32])
33
34
35# Code that we'd eventually like to compile.
36_WANT_TO_COMPILE = map(_FROM_SRC, [
37  'chrome/browser/resources',
38  'chrome/browser/ui/webui',
39  'chrome/renderer/resources',
40  'chrome/test/data',
41  'content/renderer/resources',
42  'content/test/data',
43  'extensions/renderer',
44  'extensions/test/data',
45  'remoting',
46  'ui/file_manager',
47  'ui/keyboard',
48])
49
50
51_GIT_IGNORE = open(_FROM_SRC('.gitignore')).read().splitlines()
52_IGNORE_DIRS = tuple(map(_FROM_SRC, map(lambda p: p[1:], _GIT_IGNORE)))
53_IGNORE_DIRS = filter(os.path.isdir, _IGNORE_DIRS)
54_RELEVANT_JS = lambda f: f.endswith('.js') and not f.startswith(_IGNORE_DIRS)
55
56
57def main():
58  line_cache = {}
59
60  def js_files_and_deps_in_dir(js_dir):
61    found_files = set()
62
63    for root, dirs, files in os.walk(js_dir):
64      abs_files = [os.path.abspath(os.path.join(root, f)) for f in files]
65      relevant_files = filter(_RELEVANT_JS, abs_files)
66      found_files.update(relevant_files)
67      for f in relevant_files:
68        found_files.update(processor.Processor(f).included_files)
69
70    return found_files
71
72  def num_lines(f):
73    f = os.path.abspath(f)
74    if f not in line_cache:
75      line_cache[f] = len(open(f, 'r').read().splitlines())
76    return line_cache[f]
77
78  # All the files that are already compiled.
79  compiled = set()
80
81  closure_dir = os.path.join(_HERE, '..')
82  root_gyp = os.path.join(closure_dir, 'compiled_resources.gyp')
83  root_contents = open(root_gyp, 'r').read()
84  gyp_files = literal_eval(root_contents)['targets'][0]['dependencies']
85
86  for g in gyp_files:
87    gyp_file = os.path.join(closure_dir, g.replace(':*', ''))
88    targets = literal_eval(open(gyp_file, 'r').read())['targets']
89
90    for target in targets:
91      gyp_dir = os.path.dirname(gyp_file)
92      target_file = os.path.join(gyp_dir, target['target_name'] + '.js')
93      compiled.add(os.path.abspath(target_file))
94      compiled.update(processor.Processor(target_file).included_files)
95
96      if 'variables' in target and 'depends' in target['variables']:
97        depends = target['variables']['depends']
98        rel_depends = [os.path.join(gyp_dir, d) for d in depends]
99        compiled.update([os.path.abspath(d) for d in rel_depends])
100
101  compiled_lines = sum(map(num_lines, compiled))
102  print 'compiled: %d files, %d lines' % (len(compiled), compiled_lines)
103
104  # Find and calculate the line count of all .js files in the wanted or needed
105  # resource directories.
106  files = set()
107
108  for n in _NEED_TO_COMPILE:
109    files.update(js_files_and_deps_in_dir(n))
110
111  need_lines = sum(map(num_lines, files))
112  print 'need: %d files, %d lines' % (len(files), need_lines)
113
114  need_done = float(compiled_lines) / need_lines * 100
115  print '%.2f%% done with the code we need to compile' % need_done
116
117  for w in _WANT_TO_COMPILE:
118    files.update(js_files_and_deps_in_dir(w))
119
120  want_lines = sum(map(num_lines, files))
121  print 'want: %d files, %d lines' % (len(files), want_lines)
122
123  want_done = float(compiled_lines) / want_lines * 100
124  print '%.2f%% done with the code we want to compile' % want_done
125
126
127if __name__ == '__main__':
128  main()
129