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.
6 from ast
import literal_eval
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
))
15 from sys
import path
as sys_path
16 sys_path
.insert(0, os
.path
.join(_HERE
, '..'))
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',
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',
41 'content/renderer/resources',
43 'extensions/renderer',
44 'extensions/test/data',
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
)
60 def js_files_and_deps_in_dir(js_dir
):
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
)
73 f
= os
.path
.abspath(f
)
74 if f
not in line_cache
:
75 line_cache
[f
] = len(open(f
, 'r').read().splitlines())
78 # All the files that are already compiled.
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']
87 gyp_file
= os
.path
.join(closure_dir
, g
.replace(':*', ''))
88 targets
= literal_eval(open(gyp_file
, 'r').read())['targets']
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
)
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
])
101 compiled_lines
= sum(map(num_lines
, compiled
))
102 print 'compiled: %d files, %d lines' % (len(compiled
), compiled_lines
)
104 # Find and calculate the line count of all .js files in the wanted or needed
105 # resource directories.
108 for n
in _NEED_TO_COMPILE
:
109 files
.update(js_files_and_deps_in_dir(n
))
111 need_lines
= sum(map(num_lines
, files
))
112 print 'need: %d files, %d lines' % (len(files
), need_lines
)
114 need_done
= float(compiled_lines
) / need_lines
* 100
115 print '%.2f%% done with the code we need to compile' % need_done
117 for w
in _WANT_TO_COMPILE
:
118 files
.update(js_files_and_deps_in_dir(w
))
120 want_lines
= sum(map(num_lines
, files
))
121 print 'want: %d files, %d lines' % (len(files
), want_lines
)
123 want_done
= float(compiled_lines
) / want_lines
* 100
124 print '%.2f%% done with the code we want to compile' % want_done
127 if __name__
== '__main__':