closure_library_jscompiler.py
Go to the documentation of this file.
00001 # Copyright 2010 The Closure Library Authors. All Rights Reserved.
00002 #
00003 # Licensed under the Apache License, Version 2.0 (the "License");
00004 # you may not use this file except in compliance with the License.
00005 # You may obtain a copy of the License at
00006 #
00007 #      http://www.apache.org/licenses/LICENSE-2.0
00008 #
00009 # Unless required by applicable law or agreed to in writing, software
00010 # distributed under the License is distributed on an "AS-IS" BASIS,
00011 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 # See the License for the specific language governing permissions and
00013 # limitations under the License.
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 # Pulls a version number from the first line of 'java -version'
00024 # See http://java.sun.com/j2se/versioning_naming.html to learn more about the
00025 # command's output format.
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   # User friendly version check.
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


websocket_gui
Author(s): Benoit Lescot and Stéphane Magnenat
autogenerated on Sat Dec 28 2013 17:46:48