Commit 334e3dd2 by Timothée Peignier

improve and cache version_from_file calculations

parent 2aaf293f
from django.dispatch import Signal import os
from django.core.cache import cache
from django.dispatch import Signal, receiver
css_compressed = Signal(providing_args=["package", "version"]) css_compressed = Signal(providing_args=["package", "version"])
js_compressed = Signal(providing_args=["package", "version"]) js_compressed = Signal(providing_args=["package", "version"])
@receiver(css_compressed)
@receiver(js_compressed)
def invalidate_cache(sender, package, version, **kwargs):
filename_base, filename = os.path.split(package['output'])
cache.set("pipeline:%s" % filename, str(version))
import os import os
import re import re
from django.core.cache import cache
from pipeline.conf import settings from pipeline.conf import settings
from pipeline.storage import storage from pipeline.storage import storage
from pipeline.utils import to_class from pipeline.utils import to_class
...@@ -18,15 +20,18 @@ class Versioning(object): ...@@ -18,15 +20,18 @@ class Versioning(object):
return getattr(self.versioner, 'version')(paths) return getattr(self.versioner, 'version')(paths)
def version_from_file(self, path, filename): def version_from_file(self, path, filename):
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)]) version = cache.get("pipeline:%s" % filename)
if not version:
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part)
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]+)'))
versions = [] for f in storage.listdir(path)[1]:
for f in sorted(storage.listdir(path)[1], reverse=True): match = regex.match(f)
version = regex.match(f) if match and match.groups():
if version and version.groups(): version = match.group(1)
versions.append(version.group(1)) break
versions.sort() cache.set("pipeline:%s" % filename, version)
return versions[-1] return str(version)
def output_filename(self, filename, version): def output_filename(self, filename, version):
if settings.PIPELINE_VERSION and version is not None: if settings.PIPELINE_VERSION and version is not None:
...@@ -46,7 +51,8 @@ class Versioning(object): ...@@ -46,7 +51,8 @@ class Versioning(object):
return # Nothing to delete here return # Nothing to delete here
path = os.path.dirname(filename) path = os.path.dirname(filename)
filename = os.path.basename(filename) filename = os.path.basename(filename)
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)]) filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part)
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]+)'))
try: try:
for f in storage.listdir(path)[1]: for f in storage.listdir(path)[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