Commit efd2e5d9 by Sander Smits

implemented plugin-based versioning

parent ddc13dba
......@@ -6,7 +6,7 @@ COMPRESS_AUTO = getattr(settings, 'COMPRESS_AUTO', True)
COMPRESS_VERSION = getattr(settings, 'COMPRESS_VERSION', False)
COMPRESS_VERSION_PLACEHOLDER = getattr(settings, 'COMPRESS_VERSION_PLACEHOLDER', '?')
COMPRESS_VERSION_DEFAULT = getattr(settings, 'COMPRESS_VERSION_DEFAULT', '0')
COMPRESS_VERSION_METHOD = getattr(settings, 'COMPRESS_VERSION_METHOD', 'hash')
COMPRESS_VERSIONING = getattr(settings, 'COMPRESS_VERSIONING', 'compress.versioning.mtime.MTimeVersioning')
COMPRESS_CSS_FILTERS = getattr(settings, 'COMPRESS_CSS_FILTERS', ['compress.filters.csstidy.CSSTidyFilter'])
COMPRESS_JS_FILTERS = getattr(settings, 'COMPRESS_JS_FILTERS', ['compress.filters.jsmin.JSMinFilter'])
......
......@@ -22,7 +22,7 @@ class Command(NoArgsCommand):
for name, css in settings.COMPRESS_CSS.items():
u, version = needs_update(css['output_filename'],
css['source_filenames'], settings.COMPRESS_VERSION_METHOD)
css['source_filenames'])
if (force or u) or verbosity >= 2:
msg = 'CSS Group \'%s\'' % name
......@@ -38,7 +38,7 @@ class Command(NoArgsCommand):
for name, js in settings.COMPRESS_JS.items():
u, version = needs_update(js['output_filename'],
js['source_filenames'], settings.COMPRESS_VERSION_METHOD)
js['source_filenames'])
if (force or u) or verbosity >= 2:
msg = 'JavaScript Group \'%s\'' % name
......
......@@ -42,7 +42,7 @@ class CompressedCSSNode(template.Node):
if settings.COMPRESS_AUTO:
u, version = needs_update(css['output_filename'],
css['source_filenames'], settings.COMPRESS_VERSION_METHOD)
css['source_filenames'])
if u:
filter_css(css)
......@@ -73,7 +73,7 @@ class CompressedJSNode(template.Node):
if settings.COMPRESS_AUTO:
u, version = needs_update(js['output_filename'],
js['source_filenames'], settings.COMPRESS_VERSION_METHOD)
js['source_filenames'])
if u:
filter_js(js)
......
......@@ -9,22 +9,22 @@ from django.dispatch import dispatcher
from compress.conf import settings
from compress.signals import css_filtered, js_filtered
def get_filter(compressor_class):
def get_class(class_string):
"""
Convert a string version of a function name to the callable object.
"""
if not hasattr(compressor_class, '__bases__'):
if not hasattr(class_string, '__bases__'):
try:
compressor_class = compressor_class.encode('ascii')
mod_name, class_name = get_mod_func(compressor_class)
class_string = class_string.encode('ascii')
mod_name, class_name = get_mod_func(class_string)
if class_name != '':
compressor_class = getattr(__import__(mod_name, {}, {}, ['']), class_name)
class_string = getattr(__import__(mod_name, {}, {}, ['']), class_name)
except (ImportError, AttributeError):
raise Exception('Failed to import filter %s' % compressor_class)
raise Exception('Failed to import filter %s' % class_string)
return compressor_class
return class_string
def get_mod_func(callback):
"""
......@@ -38,28 +38,21 @@ def get_mod_func(callback):
return callback, ''
return callback[:dot], callback[dot+1:]
def needs_update(output_file, source_files, method):
def needs_update(output_file, source_files, verbosity=0):
"""
Scan the source files for changes and returns True if the output_file needs to be updated.
"""
version = get_version(source_files, method)
version = get_version(source_files)
on = get_output_filename(output_file, version)
compressed_file_full = media_root(on)
if not os.path.exists(compressed_file_full):
return True, version
# Check if the output file is outdated
if method == 'hash':
ph = settings.COMPRESS_VERSION_PLACEHOLDER
of = output_file
phi = of.index(ph)
old_version = on[phi:phi+len(ph)-len(of)]
return (version != old_version), version
else:
return (os.stat(compressed_file_full).st_mtime < mtime), mtime
update_needed = getattr(get_class(settings.COMPRESS_VERSIONING)(verbose=(verbosity >= 2)), 'needs_update')(output_file, source_files, version)
return update_needed
def media_root(filename):
"""
......@@ -96,24 +89,12 @@ def get_output_filename(filename, version):
else:
return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, settings.COMPRESS_VERSION_DEFAULT)
def get_version(source_files, method):
if method == 'hash':
import cStringIO
buf = concat(source_files)
s = cStringIO.StringIO(buf)
version = getmd5(s)
s.close()
return version
else:
mtime = max_mtime(source_files)
try:
return str(int(mtime))
except ValueError:
return str(mtime)
def get_version(source_files, verbosity=0):
version = getattr(get_class(settings.COMPRESS_VERSIONING)(verbose=(verbosity >= 2)), 'get_version')(source_files)
return version
def remove_files(path, filename, verbosity=0):
regex = re.compile(r'^%s$' % (os.path.basename(get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'\d+'))))
regex = re.compile(r'^%s$' % (os.path.basename(get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'[A-Za-z0-9]+'))))
for f in os.listdir(path):
if regex.match(f):
if verbosity >= 1:
......@@ -124,8 +105,7 @@ def remove_files(path, filename, verbosity=0):
def filter_common(obj, verbosity, filters, attr, separator, signal):
output = concat(obj['source_filenames'], separator)
filename = get_output_filename(obj['output_filename'], get_version(obj['source_filenames'],
settings.COMPRESS_VERSION_METHOD))
filename = get_output_filename(obj['output_filename'], get_version(obj['source_filenames']))
if settings.COMPRESS_VERSION:
remove_files(os.path.dirname(media_root(filename)), obj['output_filename'], verbosity)
......@@ -134,7 +114,7 @@ def filter_common(obj, verbosity, filters, attr, separator, signal):
print "Saving %s" % filename
for f in filters:
output = getattr(get_filter(f)(verbose=(verbosity >= 2)), attr)(output)
output = getattr(get_class(f)(verbose=(verbosity >= 2)), attr)(output)
save_file(filename, output)
signal.send(None)
......@@ -143,14 +123,4 @@ def filter_css(css, verbosity=0):
return filter_common(css, verbosity, filters=settings.COMPRESS_CSS_FILTERS, attr='filter_css', separator='', signal=css_filtered)
def filter_js(js, verbosity=0):
return filter_common(js, verbosity, filters=settings.COMPRESS_JS_FILTERS, attr='filter_js', separator=';', signal=js_filtered)
def getmd5(f, CHUNK=2**16):
import md5
m = md5.new()
while 1:
chunk = f.read(CHUNK)
if not chunk:
break
m.update(chunk)
return m.hexdigest()
\ No newline at end of file
return filter_common(js, verbosity, filters=settings.COMPRESS_JS_FILTERS, attr='filter_js', separator='', signal=js_filtered)
class VersioningBase(object):
def __init__(self, verbose):
self.verbose = verbose
def get_version(self, source_files):
raise NotImplementedError
def needs_update(self, output_file, source_files, version):
raise NotImplementedError
class VersioningError(Exception):
"""
This exception is raised when version creation fails
"""
pass
\ No newline at end of file
import cStringIO
import md5
import os
from compress.conf import settings
from compress.utils import concat, get_output_filename
from compress.versioning.base import VersioningBase
def get_md5(f, CHUNK=2**16):
m = md5.new()
while 1:
chunk = f.read(CHUNK)
if not chunk:
break
m.update(chunk)
return m.hexdigest()
class MD5Versioning(VersioningBase):
def get_version(self, source_files):
buf = concat(source_files)
s = cStringIO.StringIO(buf)
version = get_md5(s)
s.close()
return version
def needs_update(self, output_file, source_files, version):
output_file_name = get_output_filename(output_file, version)
ph = settings.COMPRESS_VERSION_PLACEHOLDER
of = output_file
try:
phi = of.index(ph)
old_version = output_file_name[phi:phi+len(ph)-len(of)]
return (version != old_version), version
except ValueError:
# no placeholder found, do not update, manual update if needed
return False, version
\ No newline at end of file
import os
from compress.utils import get_output_filename, media_root
from compress.versioning.base import VersioningBase
def max_mtime(files):
return int(max([os.stat(media_root(f)).st_mtime for f in files]))
class MTimeVersioning(VersioningBase):
def get_version(self, source_files):
mtime = max_mtime(source_files)
try:
return str(int(mtime))
except ValueError:
return str(mtime)
def needs_update(self, output_file, source_files, version):
on = get_output_filename(output_file, version)
compressed_file_full = media_root(on)
return (os.stat(compressed_file_full).st_mtime < version), version
\ 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