Commit f5e8443c by Timothée Peignier

factorize subprocess based filters

parent a01f1275
import subprocess
class FilterBase: class FilterBase:
def __init__(self, verbose): def __init__(self, verbose):
self.verbose = verbose self.verbose = verbose
...@@ -12,3 +15,27 @@ class FilterBase: ...@@ -12,3 +15,27 @@ class FilterBase:
class FilterError(Exception): class FilterError(Exception):
"""This exception is raised when a filter fails""" """This exception is raised when a filter fails"""
pass pass
class SubProcessFilter(FilterBase):
def execute_command(self, command, content):
pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
pipe.stdin.write(content)
pipe.stdin.close()
compressed_content = pipe.stdout.read()
pipe.stdout.close()
error = pipe.stderr.read()
pipe.stderr.close()
if pipe.wait() != 0:
if not error:
error = "Unable to apply %s filter" % self.__class__.__name__
raise FilterError(error)
if self.verbose:
print error
return compressed_content
import subprocess
from compress.conf import settings from compress.conf import settings
from compress.filters import FilterBase, FilterError from compress.filters import SubProcessFilter
class ClosureCompressorFilter(FilterBase): class ClosureCompressorFilter(SubProcessFilter):
def filter_js(self, js): def filter_js(self, js):
command = '%s %s' % (settings.COMPRESS_CLOSURE_BINARY, settings.COMPRESS_CLOSURE_ARGUMENTS) command = '%s %s' % (settings.COMPRESS_CLOSURE_BINARY, settings.COMPRESS_CLOSURE_ARGUMENTS)
if self.verbose: if self.verbose:
command += ' --verbose' command += ' --verbose'
return self.execute_command(command, js)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(js)
p.stdin.close()
filtered_js = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply Closure Compressor filter'
raise FilterError(err)
if self.verbose:
print err
return filtered_js
import subprocess
from compress.conf import settings from compress.conf import settings
from compress.filters import FilterBase, FilterError from compress.filters import SubProcessFilter
class UglifyJSCompressorFilter(FilterBase): class UglifyJSCompressorFilter(SubProcessFilter):
def filter_js(self, js): def filter_js(self, js):
command = '%s %s' % (settings.COMPRESS_UGLIFYJS_BINARY, settings.COMPRESS_UGLIFYJS_ARGUMENTS) command = '%s %s' % (settings.COMPRESS_UGLIFYJS_BINARY, settings.COMPRESS_UGLIFYJS_ARGUMENTS)
if self.verbose: if self.verbose:
command += ' --verbose' command += ' --verbose'
return self.execute_command(command, js)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(js)
p.stdin.close()
filtered_js = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply UglifyJS Compressor filter'
raise FilterError(err)
if self.verbose:
print err
return filtered_js
import subprocess
from compress.conf import settings from compress.conf import settings
from compress.filters import FilterBase, FilterError from compress.filters import SubProcessFilter
class YUICompressorFilter(FilterBase): class YUICompressorFilter(SubProcessFilter):
def filter_common(self, content, type_, arguments): def filter_common(self, content, type_, arguments):
command = '%s --type=%s %s' % (settings.COMPRESS_YUI_BINARY, type_, arguments) command = '%s --type=%s %s' % (settings.COMPRESS_YUI_BINARY, type_, arguments)
if self.verbose: if self.verbose:
command += ' --verbose' command += ' --verbose'
return self.execute_command(command, content)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(content)
p.stdin.close()
compressed_content = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply YUI Compressor filter'
raise FilterError(err)
if self.verbose:
print err
return compressed_content
def filter_js(self, js): def filter_js(self, js):
return self.filter_common(js, 'js', settings.COMPRESS_YUI_JS_ARGUMENTS) return self.filter_common(js, 'js', settings.COMPRESS_YUI_JS_ARGUMENTS)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment