Commit 96b356d7 by Timothée Peignier Committed by Timothée Peignier

change compiler interface, to allow compiler to deal with outdated file themselves

parent 05cf8044
......@@ -142,6 +142,8 @@ A custom compiler for an imaginary compiler called jam ::
def match_file(self, filename):
return filename.endswith('.jam')
def compile_file(self, content, path):
return jam.compile(content)
def compile_file(self, infile, outfile, outdated=False, force=False):
if not outdated and not force:
return # No need to recompiled file
return jam.compile(infile, outfile)
......@@ -6,9 +6,6 @@ try:
except ImportError:
from django.contrib.staticfiles import finders # noqa
from django.core.files.base import ContentFile
from django.utils.encoding import smart_str
from pipeline.conf import settings
from pipeline.storage import default_storage
from pipeline.utils import to_class
......@@ -24,20 +21,19 @@ class Compiler(object):
compilers = property(compilers)
def compile(self, paths, force=False):
for index, path in enumerate(paths):
for index, input_path in enumerate(paths):
for compiler in self.compilers:
compiler = compiler(self.verbose)
if compiler.match_file(path):
new_path = self.output_path(path, compiler.output_extension)
paths[index] = new_path
if not force and not self.is_outdated(path, new_path):
continue
if compiler.match_file(input_path):
output_path = self.output_path(input_path, compiler.output_extension)
paths[index] = output_path
try:
content = self.read_file(path)
compiled_content = compiler.compile_file(content, finders.find(path))
self.save_file(new_path, compiled_content)
infile = finders.find(input_path)
outfile = finders.find(output_path)
outdated = self.is_outdated(input_path, output_path)
compiler.compile_file(infile, outfile, outdated=outdated, force=force)
except CompilerError:
if not self.storage.exists(new_path) or not settings.PIPELINE:
if not self.storage.exists(output_path) or not settings.PIPELINE:
raise
return paths
......@@ -45,21 +41,12 @@ class Compiler(object):
path = os.path.splitext(path)
return '.'.join((path[0], extension))
def read_file(self, path):
file = self.storage.open(path, 'rb')
content = file.read()
file.close()
return content
def is_outdated(self, path, new_path):
def is_outdated(self, infile, outfile):
try:
return self.storage.modified_time(path) > self.storage.modified_time(new_path)
return self.storage.modified_time(infile) > self.storage.modified_time(outfile)
except (OSError, NotImplementedError):
return True
def save_file(self, path, content):
return self.storage.save(path, ContentFile(smart_str(content)))
class CompilerBase(object):
def __init__(self, verbose):
......@@ -68,7 +55,7 @@ class CompilerBase(object):
def match_file(self, filename):
raise NotImplementedError
def compile_file(self, content, path):
def compile_file(self, infile, outfile, outdated=False, force=False):
raise NotImplementedError
......
......@@ -8,9 +8,13 @@ class CoffeeScriptCompiler(SubProcessCompiler):
def match_file(self, path):
return path.endswith('.coffee')
def compile_file(self, content, path):
command = "%s -sc %s" % (
def compile_file(self, infile, outfile, outdated=False, force=False):
if not outdated and not force:
return # File doesn't need to be recompiled
command = "%s -c %s %s > %s" % (
settings.PIPELINE_COFFEE_SCRIPT_BINARY,
settings.PIPELINE_COFFEE_SCRIPT_ARGUMENTS
settings.PIPELINE_COFFEE_SCRIPT_ARGUMENTS,
infile,
outfile
)
return self.execute_command(command, content)
return self.execute_command(command)
import os.path
from os.path import dirname
from pipeline.conf import settings
from pipeline.compilers import SubProcessCompiler
......@@ -10,12 +10,11 @@ class LessCompiler(SubProcessCompiler):
def match_file(self, filename):
return filename.endswith('.less')
def compile_file(self, content, path):
command = '%s %s %s' % (
def compile_file(self, infile, outfile, outdated=False, force=False):
command = "%s %s %s %s" % (
settings.PIPELINE_LESS_BINARY,
settings.PIPELINE_LESS_ARGUMENTS,
path
infile,
outfile
)
cwd = os.path.dirname(path)
content = self.execute_command(command, cwd=cwd)
return content
return self.execute_command(command, cwd=dirname(infile))
import os.path
from os.path import dirname
from pipeline.conf import settings
from pipeline.compilers import SubProcessCompiler
......@@ -10,11 +10,11 @@ class SASSCompiler(SubProcessCompiler):
def match_file(self, filename):
return filename.endswith(('.scss', '.sass'))
def compile_file(self, content, path):
command = "%s --scss %s %s" % (
def compile_file(self, infile, outfile, outdated=False, force=False):
command = "%s %s --update %s:%s" % (
settings.PIPELINE_SASS_BINARY,
settings.PIPELINE_SASS_ARGUMENTS,
path
infile,
outfile
)
cwd = os.path.dirname(path)
return self.execute_command(command, cwd=cwd)
return self.execute_command(command, cwd=dirname(infile))
import os.path
from os.path import dirname
from pipeline.conf import settings
from pipeline.compilers import SubProcessCompiler
......@@ -10,10 +10,13 @@ class StylusCompiler(SubProcessCompiler):
def match_file(self, filename):
return filename.endswith('.styl')
def compile_file(self, content, path):
command = "%s %s" % (
def compile_file(self, infile, outfile, outdated=False, force=False):
if not outdated and not force:
return # File doesn't need to be recompiled
command = "%s %s < %s > %s" % (
settings.PIPELINE_STYLUS_BINARY,
settings.PIPELINE_STYLUS_ARGUMENTS,
infile,
outfile
)
cwd = os.path.dirname(path)
return self.execute_command(command, content, cwd=cwd)
return self.execute_command(command, cwd=dirname(infile))
tox
flake8
\ No newline at end of file
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