Commit 18aefa70 by Bryan Chow

Merge from lincolnloop/master.

parents c0e92749 2414bf61
......@@ -2,6 +2,9 @@ from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
COMPRESS = getattr(settings, 'COMPRESS', not settings.DEBUG)
COMPRESS_SOURCE = getattr(settings, 'COMPRESS_SOURCE', settings.MEDIA_ROOT)
COMPRESS_ROOT = getattr(settings, 'COMPRESS_ROOT', settings.MEDIA_ROOT)
COMPRESS_URL = getattr(settings, 'COMPRESS_URL', settings.MEDIA_URL)
COMPRESS_AUTO = getattr(settings, 'COMPRESS_AUTO', True)
COMPRESS_VERSION = getattr(settings, 'COMPRESS_VERSION', False)
COMPRESS_VERSION_PLACEHOLDER = getattr(settings, 'COMPRESS_VERSION_PLACEHOLDER', '?')
......
......@@ -5,7 +5,7 @@ from django import template
from django.conf import settings as django_settings
from compress.conf import settings
from compress.utils import media_root, media_url, needs_update, filter_css, filter_js, get_output_filename, get_version, get_version_from_file
from compress.utils import compress_root, compress_url, needs_update, filter_css, filter_js, get_output_filename, get_version, get_version_from_file
register = template.Library()
......@@ -28,7 +28,7 @@ def render_common(obj, filename, version):
if filename.startswith('http://'):
context['url'] = filename
else:
context['url'] = media_url(filename, prefix)
context['url'] = compress_url(filename, prefix)
return obj['template'].render(context)
......@@ -61,7 +61,7 @@ class CompressedCSSNode(template.Node):
filter_css(css)
else:
filename_base, filename = os.path.split(css['output_filename'])
path_name = media_root(filename_base)
path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename)
return render_css(css, css['output_filename'], version)
......@@ -102,7 +102,7 @@ class CompressedJSNode(template.Node):
filter_js(js)
else:
filename_base, filename = os.path.split(js['output_filename'])
path_name = media_root(filename_base)
path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename)
return render_js(js, js['output_filename'], version)
......
......@@ -46,7 +46,7 @@ def needs_update(output_file, source_files, verbosity=0):
version = get_version(source_files)
on = get_output_filename(output_file, version)
compressed_file_full = media_root(on)
compressed_file_full = compress_root(on)
if not os.path.exists(compressed_file_full):
return True, version
......@@ -54,16 +54,22 @@ def needs_update(output_file, source_files, verbosity=0):
update_needed = getattr(get_class(settings.COMPRESS_VERSIONING)(), 'needs_update')(output_file, source_files, version)
return update_needed
def media_root(filename):
def compress_root(filename):
"""
Return the full path to ``filename``. ``filename`` is a relative path name in MEDIA_ROOT
Return the full path to ``filename``. ``filename`` is a relative path name in COMPRESS_ROOT
"""
return os.path.join(django_settings.MEDIA_ROOT, filename)
return os.path.join(settings.COMPRESS_ROOT, filename)
def media_url(url, prefix=None):
def compress_source(filename):
"""
Return the full path to ``filename``. ``filename`` is a relative path name in COMPRESS_SOURCE
"""
return os.path.join(settings.COMPRESS_SOURCE, filename)
def compress_url(url, prefix=None):
if prefix:
return prefix + urlquote(url)
return django_settings.MEDIA_URL + urlquote(url)
return settings.COMPRESS_URL + urlquote(url)
def concat(filenames, separator=''):
"""
......@@ -71,20 +77,17 @@ def concat(filenames, separator=''):
"""
r = ''
for filename in filenames:
fd = open(media_root(filename), 'rb')
fd = open(compress_source(filename), 'rb')
r += fd.read()
r += separator
fd.close()
return r
def max_mtime(files):
return int(max([os.stat(media_root(f)).st_mtime for f in files]))
def save_file(filename, contents):
dirname = os.path.dirname(media_root(filename))
dirname = os.path.dirname(compress_root(filename))
if not os.path.exists(dirname):
os.makedirs(dirname)
fd = open(media_root(filename), 'wb+')
fd = open(compress_root(filename), 'wb+')
fd.write(contents)
fd.close()
......@@ -121,7 +124,7 @@ def filter_common(obj, verbosity, filters, attr, separator, signal):
filename = get_output_filename(obj['output_filename'], get_version(obj['source_filenames']))
if settings.COMPRESS_VERSION and settings.COMPRESS_VERSION_REMOVE_OLD:
remove_files(os.path.dirname(media_root(filename)), obj['output_filename'], verbosity)
remove_files(os.path.dirname(compress_root(filename)), obj['output_filename'], verbosity)
if verbosity >= 1:
print "Saving %s" % filename
......
import os
from compress.utils import get_output_filename, media_root
from compress.utils import get_output_filename, compress_source, compress_root
from compress.versioning.base import VersioningBase
class MTimeVersioning(VersioningBase):
......@@ -8,12 +8,13 @@ class MTimeVersioning(VersioningBase):
def get_version(self, source_files):
# Return the modification time for the newest source file
return str(max([int(os.stat(media_root(f)).st_mtime) for f in source_files]))
return str(max(
[int(os.stat(compress_source(f)).st_mtime) for f in source_files]))
def needs_update(self, output_file, source_files, version):
output_file_name = get_output_filename(output_file, version)
compressed_file_full = media_root(output_file_name)
compressed_file_full = compress_root(output_file_name)
return (int(os.stat(compressed_file_full).st_mtime) < int(version)), version
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