Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / chromevox / tools / check_chromevox.py
blob1c0f8c99daf47a1fa30223476feea0ad9a6a40a4
1 #!/usr/bin/env python
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.
7 '''Uses the closure compiler to check the ChromeVox javascript files.
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 '''
15 import optparse
16 import os
17 import re
18 import sys
20 from multiprocessing import pool
22 from jsbundler import Bundle, CalcDeps, ReadSources
23 from jscompilerwrapper import RunCompiler
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))
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))
37 def ChromeRootPath(path='.'):
38 '''Converts a path relative to the top-level chromevox directory to a
39 path relative to the current directory.
40 '''
41 return os.path.relpath(os.path.join(_CHROME_SOURCE_DIR, path))
44 # Name of chrome extensions externs file.
45 _CHROME_EXTENSIONS_EXTERNS = (
46 ChromeRootPath('third_party/closure_compiler/externs/chrome_extensions.js'))
48 # Externs common to many ChromeVox scripts.
49 _COMMON_EXTERNS = [
50 CVoxPath('common/externs.js'),
51 CVoxPath('common/chrome_extension_externs.js'),
52 CVoxPath('chromevox/background/externs.js'),
53 CVoxPath('chromevox/injected/externs.js'),
54 CVoxPath('host/chrome/externs.js'),
55 _CHROME_EXTENSIONS_EXTERNS]
57 # List of top-level scripts and externs that we can check.
58 _TOP_LEVEL_SCRIPTS = [
59 [[CVoxPath('chromevox/background/kbexplorer_loader.js')],
60 [_CHROME_EXTENSIONS_EXTERNS]],
61 [[CVoxPath('chromevox/background/loader.js')], _COMMON_EXTERNS],
62 [[CVoxPath('chromevox/background/options_loader.js')], _COMMON_EXTERNS],
63 [[CVoxPath('chromevox/injected/loader.js')], _COMMON_EXTERNS],
64 [[CVoxPath('cvox2/background/loader.js')], _COMMON_EXTERNS],
68 def _Compile(js_files, externs):
69 try:
70 return RunCompiler(js_files, externs)
71 except KeyboardInterrupt:
72 return (False, 'KeyboardInterrupt')
75 def CheckChromeVox(changed_files=None):
76 if changed_files is not None:
77 changed_files_set = frozenset(
78 (os.path.relpath(path) for path in changed_files))
79 if len(changed_files_set) == 0:
80 return (True, '')
81 else:
82 changed_files_set = None
83 ret_success = True
84 ret_output = ''
85 roots = [CVoxPath(),
86 os.path.relpath(
87 os.path.join(
88 _CHROME_SOURCE_DIR,
89 'chrome/third_party/chromevox/third_party/closure-library/'
90 'closure/goog'))]
91 sources = ReadSources(roots, need_source_text=True,
92 exclude=[re.compile('testing')])
93 work_pool = pool.Pool(len(_TOP_LEVEL_SCRIPTS))
94 try:
95 results = []
96 for top_level in _TOP_LEVEL_SCRIPTS:
97 tl_files, externs = top_level
98 bundle = Bundle()
99 CalcDeps(bundle, sources, tl_files)
100 bundle.Add((sources[name] for name in tl_files))
101 ordered_paths = list(bundle.GetInPaths())
102 if (changed_files_set is not None and
103 changed_files_set.isdisjoint(ordered_paths + externs)):
104 continue
105 print 'Compiling %s' % ','.join(tl_files)
106 results.append([tl_files,
107 work_pool.apply_async(
108 _Compile,
109 args=[ordered_paths, externs])])
110 for result in results:
111 tl_files = result[0]
112 success, output = result[1].get()
113 if not success:
114 ret_output += '\nFrom compiling %s:\n%s\n' % (','.join(tl_files),
115 output)
116 ret_success = False
117 work_pool.close()
118 except:
119 work_pool.terminate()
120 raise
121 finally:
122 work_pool.join()
123 return (ret_success, ret_output)
126 def main():
127 parser = optparse.OptionParser(description=__doc__)
128 parser.usage = '%prog [<changed_file>...]'
129 _, args = parser.parse_args()
131 changed_paths = None
132 if len(args) > 0:
133 changed_paths = (os.path.relpath(p) for p in args)
134 success, output = CheckChromeVox(changed_paths)
135 if len(output) > 0:
136 print output
137 return int(not success)
140 if __name__ == '__main__':
141 sys.exit(main())