OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 from ast import literal_eval | 6 from ast import literal_eval |
7 import os | 7 import os |
8 | 8 |
9 | 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 | |
10 # High priority code to compile. | 15 # High priority code to compile. |
11 _NEED_TO_COMPILE = [ | 16 _NEED_TO_COMPILE = map(_FROM_SRC, [ |
12 'chrome/browser/resources', | 17 'chrome/browser/resources', |
13 'chrome/browser/ui/webui', | 18 'chrome/browser/ui/webui', |
14 'ui/webui/resources/js', | 19 'ui/webui/resources/js', |
15 ] | 20 ]) |
16 | 21 |
17 | 22 |
18 # Code that we'd eventually like to compile. | 23 # Code that we'd eventually like to compile. |
19 _WANT_TO_COMPILE = [ | 24 _WANT_TO_COMPILE = map(_FROM_SRC, [ |
20 'chrome/renderer/resources', | 25 'chrome/renderer/resources', |
21 'chrome/test/data', | 26 'chrome/test/data', |
22 'content/renderer/resources', | 27 'content/renderer/resources', |
23 'content/test/data', | 28 'content/test/data', |
24 'extensions/renderer', | 29 'extensions/renderer', |
25 'extensions/test/data', | 30 'extensions/test/data', |
26 'remoting', | 31 'remoting', |
27 'ui/file_manager', | 32 'ui/file_manager', |
28 'ui/keyboard', | 33 'ui/keyboard', |
29 ] | 34 ]) |
35 | |
36 | |
37 _GIT_IGNORE = open(_FROM_SRC('.gitignore')).read().splitlines() | |
38 _IGNORE_DIRS = tuple(map(_FROM_SRC, map(lambda p: p[1:], _GIT_IGNORE))) | |
Tyler Breisacher (Chromium)
2014/08/27 19:01:27
Why do you need to tuple-ize it? Seems like a list
Dan Beam
2014/08/27 19:03:59
https://docs.python.org/2/library/stdtypes.html#st
| |
39 _IGNORE_DIRS = filter(os.path.isdir, _IGNORE_DIRS) | |
40 _RELEVANT_JS = lambda f: f.endswith('.js') and not f.startswith(_IGNORE_DIRS) | |
30 | 41 |
31 | 42 |
32 def main(): | 43 def main(): |
33 here = os.path.dirname(__file__) | |
34 src_root = os.path.join(here, '..', '..', '..') | |
35 | |
36 line_cache = {} | 44 line_cache = {} |
37 | 45 |
38 def js_files_in_dir(js_dir): | 46 def js_files_in_dir(js_dir): |
39 found_files = set() | 47 found_files = set() |
40 for root, dirs, files in os.walk(js_dir): | 48 for root, dirs, files in os.walk(js_dir): |
41 js = filter(lambda f: f.endswith('.js'), files) | 49 abs_files = [os.path.abspath(os.path.join(root, f)) for f in files] |
42 found_files.update([os.path.abspath(os.path.join(root, f)) for f in js]) | 50 found_files.update(filter(_RELEVANT_JS, abs_files)) |
43 return found_files | 51 return found_files |
44 | 52 |
45 def num_lines(f): | 53 def num_lines(f): |
54 f = os.path.abspath(f) | |
46 if f not in line_cache: | 55 if f not in line_cache: |
47 line_cache[f] = len(open(f, 'r').read().splitlines()) | 56 line_cache[f] = len(open(f, 'r').read().splitlines()) |
48 return line_cache[f] | 57 return line_cache[f] |
49 | 58 |
50 # All the files that are already compiled. | 59 # All the files that are already compiled. |
51 compiled = set() | 60 compiled = set() |
52 | 61 |
53 closure_dir = os.path.join(here, '..') | 62 closure_dir = os.path.join(_HERE, '..') |
54 root_gyp = os.path.join(closure_dir, 'compiled_resources.gyp') | 63 root_gyp = os.path.join(closure_dir, 'compiled_resources.gyp') |
55 root_contents = open(root_gyp, 'r').read() | 64 root_contents = open(root_gyp, 'r').read() |
56 gyp_files = literal_eval(root_contents)['targets'][0]['dependencies'] | 65 gyp_files = literal_eval(root_contents)['targets'][0]['dependencies'] |
57 | 66 |
58 for g in gyp_files: | 67 for g in gyp_files: |
59 src_to_closure = os.path.relpath(src_root, closure_dir) | 68 gyp_file = os.path.join(closure_dir, g.replace(':*', '')) |
60 rel_file = os.path.relpath(g.replace(':*', ''), src_to_closure) | 69 targets = literal_eval(open(gyp_file, 'r').read())['targets'] |
61 abs_file = os.path.abspath(rel_file) | |
62 targets = literal_eval(open(abs_file, 'r').read())['targets'] | |
63 | 70 |
64 for target in targets: | 71 for target in targets: |
65 abs_dir = os.path.dirname(abs_file) | 72 gyp_dir = os.path.dirname(gyp_file) |
66 target_file = os.path.join(abs_dir, target['target_name'] + '.js') | 73 target_file = os.path.join(gyp_dir, target['target_name'] + '.js') |
67 compiled.add(target_file) | 74 compiled.add(os.path.abspath(target_file)) |
68 | 75 |
69 if 'variables' in target and 'depends' in target['variables']: | 76 if 'variables' in target and 'depends' in target['variables']: |
70 depends = target['variables']['depends'] | 77 depends = target['variables']['depends'] |
71 rel_depends = [os.path.join(abs_dir, d) for d in depends] | 78 rel_depends = [os.path.join(gyp_dir, d) for d in depends] |
72 compiled.update([os.path.abspath(d) for d in rel_depends]) | 79 compiled.update([os.path.abspath(d) for d in rel_depends]) |
73 | 80 |
74 compiled_lines = sum(map(num_lines, compiled)) | 81 compiled_lines = sum(map(num_lines, compiled)) |
75 print 'compiled: %d files, %d lines' % (len(compiled), compiled_lines) | 82 print 'compiled: %d files, %d lines' % (len(compiled), compiled_lines) |
76 | 83 |
77 # Find and calculate the line count of all .js files in the wanted or needed | 84 # Find and calculate the line count of all .js files in the wanted or needed |
78 # resource directories. | 85 # resource directories. |
79 files = set() | 86 files = set() |
80 | 87 |
81 for n in _NEED_TO_COMPILE: | 88 for n in _NEED_TO_COMPILE: |
82 files.update(js_files_in_dir(os.path.join(src_root, n))) | 89 files.update(js_files_in_dir(n)) |
83 | 90 |
84 need_lines = sum(map(num_lines, files)) | 91 need_lines = sum(map(num_lines, files)) |
85 print 'need: %d files, %d lines' % (len(files), need_lines) | 92 print 'need: %d files, %d lines' % (len(files), need_lines) |
86 | 93 |
87 need_done = float(compiled_lines) / need_lines * 100 | 94 need_done = float(compiled_lines) / need_lines * 100 |
88 print '%.2f%% done with the code we need to compile' % need_done | 95 print '%.2f%% done with the code we need to compile' % need_done |
89 | 96 |
90 for w in _WANT_TO_COMPILE: | 97 for w in _WANT_TO_COMPILE: |
91 files.update(js_files_in_dir(os.path.join(src_root, w))) | 98 files.update(js_files_in_dir(w)) |
92 | 99 |
93 want_lines = sum(map(num_lines, files)) | 100 want_lines = sum(map(num_lines, files)) |
94 print 'want: %d files, %d lines' % (len(files), want_lines) | 101 print 'want: %d files, %d lines' % (len(files), want_lines) |
95 | 102 |
96 want_done = float(compiled_lines) / want_lines * 100 | 103 want_done = float(compiled_lines) / want_lines * 100 |
97 print '%.2f%% done with the code we want to compile' % want_done | 104 print '%.2f%% done with the code we want to compile' % want_done |
98 | 105 |
99 | 106 |
100 if __name__ == '__main__': | 107 if __name__ == '__main__': |
101 main() | 108 main() |
OLD | NEW |