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'.
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.
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.
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.
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
):
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:
82 changed_files_set
= None
89 'chrome/third_party/chromevox/third_party/closure-library/'
91 sources
= ReadSources(roots
, need_source_text
=True,
92 exclude
=[re
.compile('testing')])
93 work_pool
= pool
.Pool(len(_TOP_LEVEL_SCRIPTS
))
96 for top_level
in _TOP_LEVEL_SCRIPTS
:
97 tl_files
, externs
= top_level
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
)):
105 print 'Compiling %s' % ','.join(tl_files
)
106 results
.append([tl_files
,
107 work_pool
.apply_async(
109 args
=[ordered_paths
, externs
])])
110 for result
in results
:
112 success
, output
= result
[1].get()
114 ret_output
+= '\nFrom compiling %s:\n%s\n' % (','.join(tl_files
),
119 work_pool
.terminate()
123 return (ret_success
, ret_output
)
127 parser
= optparse
.OptionParser(description
=__doc__
)
128 parser
.usage
= '%prog [<changed_file>...]'
129 _
, args
= parser
.parse_args()
133 changed_paths
= (os
.path
.relpath(p
) for p
in args
)
134 success
, output
= CheckChromeVox(changed_paths
)
137 return int(not success
)
140 if __name__
== '__main__':