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