summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py')
-rwxr-xr-xchromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/chromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py b/chromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py
new file mode 100755
index 00000000000..b97dc31d281
--- /dev/null
+++ b/chromium/chrome/browser/resources/chromeos/chromevox/tools/jscompilerwrapper.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''Uses the closure compiler to check syntax and semantics of a js module
+with dependencies.'''
+
+import os
+import re
+import subprocess
+import sys
+
+_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
+_CHROME_SOURCE_DIR = os.path.normpath(
+ os.path.join(
+ _SCRIPT_DIR, *[os.path.pardir] * 6))
+
+# Compiler path.
+_CLOSURE_COMPILER_JAR = os.path.join(
+ _CHROME_SOURCE_DIR, 'third_party', 'WebKit', 'Source', 'devtools',
+ 'scripts', 'closure', 'compiler.jar')
+
+# List of compilation errors to enable with the --jscomp_errors flag.
+_JSCOMP_ERRORS = [ 'accessControls', 'checkTypes', 'checkVars', 'invalidCasts',
+ 'missingProperties', 'undefinedNames', 'undefinedVars',
+ 'visibility' ]
+
+_java_executable = 'java'
+
+
+def _Error(msg):
+ print >>sys.stderr, msg
+ sys.exit(1)
+
+
+def _ExecuteCommand(args, ignore_exit_status=False):
+ try:
+ return subprocess.check_output(args, stderr=subprocess.STDOUT)
+ except subprocess.CalledProcessError as e:
+ if ignore_exit_status and e.returncode > 0:
+ return e.output
+ _Error('%s\nCommand \'%s\' returned non-zero exit status %d' %
+ (e.output, ' '.join(e.cmd), e.returncode))
+ except (OSError, IOError) as e:
+ _Error('Error executing %s: %s' % (_java_executable, str(e)))
+
+
+def _CheckJava():
+ global _java_executable
+ java_home = os.environ.get('JAVAHOME')
+ if java_home is not None:
+ _java_executable = os.path.join(java_home, 'bin', 'java')
+ output = _ExecuteCommand([_java_executable, '-version'])
+ match = re.search(r'version "(?:\d+)\.(\d+)', output)
+ if match is None or int(match.group(1)) < 7:
+ _error('Java 7 or later is required: \n%s' % output)
+
+_CheckJava()
+
+
+def RunCompiler(js_files, externs=[]):
+ args = [_java_executable, '-jar', _CLOSURE_COMPILER_JAR]
+ args.extend(['--compilation_level', 'SIMPLE_OPTIMIZATIONS'])
+ args.extend(['--jscomp_error=%s' % error for error in _JSCOMP_ERRORS])
+ args.extend(['--externs=%s' % extern for extern in externs])
+ args.extend(['--js=%s' % js for js in js_files])
+ args.extend(['--js_output_file', '/dev/null'])
+ output = _ExecuteCommand(args, ignore_exit_status=True)
+ success = len(output) == 0
+ return success, output