Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Utility to use the Closure Compiler CLI from Python."""
00016
00017 import distutils.version
00018 import logging
00019 import re
00020 import subprocess
00021
00022
00023
00024
00025
00026 _VERSION_REGEX = re.compile('"([0-9][.0-9]*)')
00027
00028
00029 def _GetJavaVersion():
00030 """Returns the string for the current version of Java installed."""
00031 proc = subprocess.Popen(['java', '-version'], stderr=subprocess.PIPE)
00032 unused_stdoutdata, stderrdata = proc.communicate()
00033 version_line = stderrdata.splitlines()[0]
00034 return _VERSION_REGEX.search(version_line).group(1)
00035
00036
00037 def Compile(compiler_jar_path, source_paths, flags=None):
00038 """Prepares command-line call to Closure Compiler.
00039
00040 Args:
00041 compiler_jar_path: Path to the Closure compiler .jar file.
00042 source_paths: Source paths to build, in order.
00043 flags: A list of additional flags to pass on to Closure Compiler.
00044
00045 Returns:
00046 The compiled source, as a string, or None if compilation failed.
00047 """
00048
00049
00050 if not (distutils.version.LooseVersion(_GetJavaVersion()) >=
00051 distutils.version.LooseVersion('1.6')):
00052 logging.error('Closure Compiler requires Java 1.6 or higher. '
00053 'Please visit http://www.java.com/getjava')
00054 return
00055
00056 args = ['java', '-jar', compiler_jar_path]
00057 for path in source_paths:
00058 args += ['--js', path]
00059
00060 if flags:
00061 args += flags
00062
00063 logging.info('Compiling with the following command: %s', ' '.join(args))
00064
00065 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
00066 stdoutdata, unused_stderrdata = proc.communicate()
00067
00068 if proc.returncode != 0:
00069 return
00070
00071 return stdoutdata