Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / tools / check_chromevox.py
1 #!/usr/bin/env python
2
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 '''Uses the closure compiler to check the ChromeVox javascript files.
8
9 With no arguments, checks all ChromeVox scripts.  If any arguments are
10 specified, only scripts that include any of the specified files will be
11 compiled.  A useful argument list is the output of the command
12 'git diff --name-only --relative'.
13 '''
14
15 import optparse
16 import os
17 import re
18 import sys
19
20 from multiprocessing import pool
21
22 from jsbundler import Bundle, CalcDeps, ReadSources
23 from jscompilerwrapper import RunCompiler
24
25 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
26 _CHROME_SOURCE_DIR = os.path.normpath(
27     os.path.join(_SCRIPT_DIR, *[os.path.pardir] * 6))
28
29
30 def CVoxPath(path='.'):
31   '''Converts a path relative to the top-level chromevox directory to a
32   path relative to the current directory.
33   '''
34   return os.path.relpath(os.path.join(_SCRIPT_DIR, '..', path))
35
36
37 # Externs common to many ChromeVox scripts.
38 _COMMON_EXTERNS = [
39     CVoxPath('common/externs.js'),
40     CVoxPath('common/chrome_extension_externs.js'),
41     CVoxPath('chromevox/background/externs.js'),
42     CVoxPath('chromevox/injected/externs.js'),
43     CVoxPath('liblouis_nacl/externs.js'),
44     CVoxPath('host/chrome/externs.js')]
45
46 # List of top-level scripts and externs that we can check.
47 _TOP_LEVEL_SCRIPTS = [
48     [[CVoxPath('chromevox/background/kbexplorer_loader.js')],
49      [CVoxPath('common/chrome_extension_externs.js')]],
50     [[CVoxPath('chromevox/background/loader.js')], _COMMON_EXTERNS],
51     [[CVoxPath('chromevox/background/options_loader.js')], _COMMON_EXTERNS],
52     [[CVoxPath('chromevox/injected/loader.js')], _COMMON_EXTERNS],
53     [[CVoxPath('cvox2/background/loader.js')], _COMMON_EXTERNS],
54     ]
55
56
57 def _Compile(js_files, externs):
58   try:
59     return RunCompiler(js_files, externs)
60   except KeyboardInterrupt:
61     return (False, 'KeyboardInterrupt')
62
63
64 def CheckChromeVox(changed_files=None):
65   if changed_files is not None:
66     changed_files_set = frozenset(
67         (os.path.relpath(path) for path in changed_files))
68     if len(changed_files_set) == 0:
69       return (True, '')
70   else:
71     changed_files_set = None
72   ret_success = True
73   ret_output = ''
74   roots = [CVoxPath(),
75            os.path.relpath(
76                os.path.join(
77                    _CHROME_SOURCE_DIR,
78                    'chrome/third_party/chromevox/third_party/closure-library/'
79                    'closure/goog'))]
80   sources = ReadSources(roots, need_source_text=True,
81                         exclude=[re.compile('testing')])
82   work_pool = pool.Pool(len(_TOP_LEVEL_SCRIPTS))
83   try:
84     results = []
85     for top_level in _TOP_LEVEL_SCRIPTS:
86       tl_files, externs = top_level
87       bundle = Bundle()
88       CalcDeps(bundle, sources, tl_files)
89       bundle.Add((sources[name] for name in tl_files))
90       ordered_paths = list(bundle.GetInPaths())
91       if (changed_files_set is not None and
92           changed_files_set.isdisjoint(ordered_paths + externs)):
93         continue
94       print 'Compiling %s' % ','.join(tl_files)
95       results.append([tl_files,
96                       work_pool.apply_async(
97                           _Compile,
98                           args=[ordered_paths, externs])])
99     for result in results:
100       tl_files = result[0]
101       success, output = result[1].get()
102       if not success:
103         ret_output += '\nFrom compiling %s:\n%s\n' % (','.join(tl_files),
104                                                       output)
105         ret_success = False
106     work_pool.close()
107   except:
108     work_pool.terminate()
109     raise
110   finally:
111     work_pool.join()
112   return (ret_success, ret_output)
113
114
115 def main():
116   parser = optparse.OptionParser(description=__doc__)
117   parser.usage = '%prog [<changed_file>...]'
118   _, args = parser.parse_args()
119
120   changed_paths = None
121   if len(args) > 0:
122     changed_paths = (os.path.relpath(p) for p in args)
123   success, output = CheckChromeVox(changed_paths)
124   if len(output) > 0:
125     print output
126   return int(not success)
127
128
129 if __name__ == '__main__':
130   sys.exit(main())