Skip to content

Commit

Permalink
Merge branch 'integration'
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Altman committed Mar 19, 2009
2 parents 5038b5b + 0ee308a commit 1ce0fab
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
3 changes: 3 additions & 0 deletions compress/conf/settings.py
Expand Up @@ -2,6 +2,9 @@
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', '?')
Expand Down
8 changes: 4 additions & 4 deletions compress/templatetags/compressed.py
Expand Up @@ -5,7 +5,7 @@
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()

Expand All @@ -18,7 +18,7 @@ def render_common(template_name, obj, filename, version):
if filename.startswith('http://'):
context['url'] = filename
else:
context['url'] = media_url(filename, prefix)
context['url'] = compress_url(filename, prefix)

return template.loader.render_to_string(template_name, context)

Expand Down Expand Up @@ -51,7 +51,7 @@ def render(self, context):
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)
Expand Down Expand Up @@ -92,7 +92,7 @@ def render(self, context):
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)
Expand Down
29 changes: 16 additions & 13 deletions compress/utils.py
Expand Up @@ -46,45 +46,48 @@ 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

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=''):
"""
Concatenate the files from the list of the ``filenames``, ouput separated with ``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()

Expand Down Expand Up @@ -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:
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
Expand Down
7 changes: 4 additions & 3 deletions compress/versioning/mtime/__init__.py
@@ -1,19 +1,20 @@
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):

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

0 comments on commit 1ce0fab

Please sign in to comment.