import subprocess
from django.conf import settings
from compress.filter_base import FilterBase, FilterError
BINARY = getattr(settings, 'COMPRESS_CLOSURE_BINARY', 'java -jar compiler.jar')
JS_ARGUMENTS = getattr(settings, 'COMPRESS_CLOSURE_JS_ARGUMENTS', '')
class GoogleClosureCompilerFilter(FilterBase):
def filter_common(self, content, arguments):
command = BINARY
for argument in arguments:
command += ' --' + argument + ' ' + arguments[argument]
if self.verbose:
command += ' --verbose'
p = subprocess.Popen(command, shell = True, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)
p.stdin.write(content)
p.stdin.close()
filtered_css = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply Google Closure Compiler filter'
raise FilterError(err)
if self.verbose:
print err
return filtered_css
def filter_js(self, js):
return self.filter_common(js, JS_ARGUMENTS)
Comments
In its current state this snippet results in deadlock with large amounts of input js files (see warning here: http://docs.python.org/library/subprocess.html#subprocess.Popen.wait)
This can be avoided by using p.communicate instead of p.stdout.read().
To make this change lines 22-28 should be removed and this line inserted in their place:
filtered_css, err = p.communicate()
#
I added this code to: Google Closure Compiler now for django: https://github.com/macmichael01/django-gcc with some modifications. I have also enabled API requests so that you don't need jar file. I made note of the author in the authors file.
#