Commit 29eee6f8 by Timothée Peignier

allow to bust version cache without compressing files

parent c5dbf437
...@@ -49,7 +49,7 @@ class Compressor(object): ...@@ -49,7 +49,7 @@ class Compressor(object):
return to_class(settings.PIPELINE_CSS_COMPRESSOR) return to_class(settings.PIPELINE_CSS_COMPRESSOR)
css_compressor = property(css_compressor) css_compressor = property(css_compressor)
def compress_js(self, paths, templates=None, asset_url=None): def compress_js(self, paths, templates=None, asset_url=None, **kwargs):
"""Concatenate and compress JS files""" """Concatenate and compress JS files"""
js = self.concatenate(paths) js = self.concatenate(paths)
if templates: if templates:
...@@ -58,7 +58,7 @@ class Compressor(object): ...@@ -58,7 +58,7 @@ class Compressor(object):
js = getattr(self.js_compressor(verbose=self.verbose), 'compress_js')(js) js = getattr(self.js_compressor(verbose=self.verbose), 'compress_js')(js)
return js return js
def compress_css(self, paths, variant=None, asset_url=None): def compress_css(self, paths, variant=None, asset_url=None, **kwargs):
"""Concatenate and compress CSS files""" """Concatenate and compress CSS files"""
css = self.concatenate_and_rewrite(paths, variant) css = self.concatenate_and_rewrite(paths, variant)
css = getattr(self.css_compressor(verbose=self.verbose), 'compress_css')(css) css = getattr(self.css_compressor(verbose=self.verbose), 'compress_css')(css)
......
...@@ -10,18 +10,31 @@ class Command(BaseCommand): ...@@ -10,18 +10,31 @@ class Command(BaseCommand):
default=False, default=False,
help='Force update of all files, even if the source files are older than the current compressed file.' help='Force update of all files, even if the source files are older than the current compressed file.'
), ),
make_option('--dry-run',
action='store_false',
default=True,
help='Don\'t attempt to update files.'
),
make_option('--no-cache-bust',
dest='bust_cache',
action='store_false',
default=True,
help='Don\'t update version cache.'
)
) )
help = 'Updates and compresses CSS and JS on-demand, without restarting Django' help = 'Updates and compresses CSS and JS on-demand'
args = '<group>' args = '<group>'
def handle(self, group=None, **options): def handle(self, group=None, **options):
from pipeline.packager import Packager from pipeline.packager import Packager
packager = Packager( packager = Packager(
sync=True,
force=options.get('force', False), force=options.get('force', False),
verbose=int(options.get('verbosity', 1)) >= 2 verbose=int(options.get('verbosity', 1)) >= 2
) )
sync = options.get('dry_run', True),
bust_cache = options.get('bust_cache', True)
for package_name in packager.packages['css']: for package_name in packager.packages['css']:
if group and package_name != group: if group and package_name != group:
continue continue
...@@ -31,7 +44,7 @@ class Command(BaseCommand): ...@@ -31,7 +44,7 @@ class Command(BaseCommand):
message = "CSS Group '%s'" % package_name message = "CSS Group '%s'" % package_name
print message print message
print len(message) * '-' print len(message) * '-'
packager.pack_stylesheets(package) packager.pack_stylesheets(package, sync=sync, bust_cache=bust_cache)
for package_name in packager.packages['js']: for package_name in packager.packages['js']:
if group and package_name != group: if group and package_name != group:
...@@ -42,4 +55,4 @@ class Command(BaseCommand): ...@@ -42,4 +55,4 @@ class Command(BaseCommand):
message = "JS Group '%s'" % package_name message = "JS Group '%s'" % package_name
print message print message
print len(message) * '-' print len(message) * '-'
packager.pack_javascripts(package) packager.pack_javascripts(package, sync=sync, bust_cache=bust_cache)
...@@ -13,10 +13,9 @@ from pipeline.versioning import Versioning ...@@ -13,10 +13,9 @@ from pipeline.versioning import Versioning
class Packager(object): class Packager(object):
def __init__(self, force=False, sync=False, verbose=False, css_packages=None, js_packages=None): def __init__(self, force=False, verbose=False, css_packages=None, js_packages=None):
self.force = force self.force = force
self.verbose = verbose self.verbose = verbose
self.sync = sync
self.compressor = Compressor(verbose) self.compressor = Compressor(verbose)
self.versioning = Versioning(verbose) self.versioning = Versioning(verbose)
self.compiler = Compiler(verbose) self.compiler = Compiler(verbose)
...@@ -43,16 +42,16 @@ class Packager(object): ...@@ -43,16 +42,16 @@ class Packager(object):
return urlparse.urljoin(settings.PIPELINE_URL, return urlparse.urljoin(settings.PIPELINE_URL,
self.compressor.relative_path(filename)[1:]) self.compressor.relative_path(filename)[1:])
def pack_stylesheets(self, package): def pack_stylesheets(self, package, **kwargs):
variant = package.get('variant', None) variant = package.get('variant', None)
return self.pack(package, self.compressor.compress_css, css_compressed, return self.pack(package, self.compressor.compress_css, css_compressed,
variant=variant) variant=variant, **kwargs)
def compile(self, paths): def compile(self, paths):
return self.compiler.compile(paths) return self.compiler.compile(paths)
def pack(self, package, compress, signal, **kwargs): def pack(self, package, compress, signal, sync=False, **kwargs):
if settings.PIPELINE_AUTO or self.force or self.sync: if settings.PIPELINE_AUTO or self.force or sync:
need_update, version = self.versioning.need_update( need_update, version = self.versioning.need_update(
package['output'], package['paths']) package['output'], package['paths'])
if need_update or self.force: if need_update or self.force:
...@@ -68,14 +67,14 @@ class Packager(object): ...@@ -68,14 +67,14 @@ class Packager(object):
content = compress(paths, content = compress(paths,
asset_url=self.individual_url(output_filename), **kwargs) asset_url=self.individual_url(output_filename), **kwargs)
self.save_file(output_filename, content) self.save_file(output_filename, content)
signal.send(sender=self, package=package, version=version)
else: else:
filename_base, filename = os.path.split(package['output']) filename_base, filename = os.path.split(package['output'])
version = self.versioning.version_from_file(filename_base, filename) version = self.versioning.version_from_file(filename_base, filename)
signal.send(sender=self, package=package, version=version, **kwargs)
return self.versioning.output_filename(package['output'], version) return self.versioning.output_filename(package['output'], version)
def pack_javascripts(self, package): def pack_javascripts(self, package, **kwargs):
return self.pack(package, self.compressor.compress_js, js_compressed, templates=package['templates']) return self.pack(package, self.compressor.compress_js, js_compressed, templates=package['templates'], **kwargs)
def pack_templates(self, package): def pack_templates(self, package):
return self.compressor.compile_templates(package['templates']) return self.compressor.compile_templates(package['templates'])
......
...@@ -12,7 +12,9 @@ js_compressed = Signal(providing_args=["package", "version"]) ...@@ -12,7 +12,9 @@ js_compressed = Signal(providing_args=["package", "version"])
@receiver(css_compressed) @receiver(css_compressed)
@receiver(js_compressed) @receiver(js_compressed)
def invalidate_cache(sender, package, version, **kwargs): def invalidate_cache(sender, package, version, bust_cache=False, **kwargs):
filename_base, filename = os.path.split(package['output']) filename_base, filename = os.path.split(package['output'])
if bust_cache:
print "bust_cache"
cache.set("pipeline:%s" % filename, str(version), cache.set("pipeline:%s" % filename, str(version),
settings.PIPELINE_CACHE_TIMEOUT) settings.PIPELINE_CACHE_TIMEOUT)
...@@ -25,7 +25,7 @@ class Versioning(object): ...@@ -25,7 +25,7 @@ class Versioning(object):
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part) filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part)
for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)]) for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)])
regex = re.compile(r'^%s$' % self.output_filename(filename, r'([A-Za-z0-9]+)')) regex = re.compile(r'^%s$' % self.output_filename(filename, r'([A-Za-z0-9]+)'))
for f in storage.listdir(path)[1]: for f in sorted(storage.listdir(path)[1], reverse=True):
match = regex.match(f) match = regex.match(f)
if match and match.groups(): if match and match.groups():
version = match.group(1) version = match.group(1)
......
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