Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions compress/templatetags/compressed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def render_common(template_name, obj, filename, request, version):

context = obj.get('extra_context', {})
prefix = context.get('prefix', None)
if filename.startswith('http://'):
if filename.startswith('http://') or filename.startswith('https://'):
context['url'] = filename
else:
context['url'] = compress_url(filename, request, prefix)

return template.loader.render_to_string(template_name, context)

def render_css(css, filename, request, version=None):
Expand Down Expand Up @@ -48,15 +48,15 @@ def render(self, context):
version = None

if settings.COMPRESS_AUTO:
u, version = needs_update(css['output_filename'],
u, version = needs_update(css['output_filename'],
css['source_filenames'])
if u:
filter_css(css)
elif not css.get('extra_context', {}).get('prefix', None):
filename_base, filename = os.path.split(css['output_filename'])
path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename)

return render_css(css, css['output_filename'], request, version)
else:
# output source files
Expand All @@ -79,24 +79,24 @@ def render(self, context):
js = settings.COMPRESS_JS[js_name]
except KeyError:
return '' # fail silently, do not return anything if an invalid group is specified

if 'external_urls' in js:
r = ''
for url in js['external_urls']:
r += render_js(js, url)
r += render_js(js, url, context['request'])
return r

request = context['request']
if settings.COMPRESS:

version = None

if settings.COMPRESS_AUTO:
u, version = needs_update(js['output_filename'],
u, version = needs_update(js['output_filename'],
js['source_filenames'])
if u:
filter_js(js)
elif not js.get('extra_context', {}).get('prefix', None):
elif not js.get('extra_context', {}).get('prefix', None):
filename_base, filename = os.path.split(js['output_filename'])
path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename)
Expand Down
32 changes: 19 additions & 13 deletions compress/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def needs_update(output_file, source_files, verbosity=0):
"""

version = get_version(source_files)

on = get_output_filename(output_file, version)
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

Expand All @@ -65,7 +65,7 @@ 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, request, prefix=None):
if prefix:
return prefix + urlquote(url)
Expand All @@ -81,10 +81,13 @@ def concat(filenames, separator=''):
"""
r = ''
for filename in filenames:
fd = open(compress_source(filename), 'rb')
r += fd.read()
r += separator
fd.close()
try:
fd = open(compress_source(filename), 'rb')
r += fd.read()
r += separator
fd.close()
except IOError:
pass
return r

def save_file(filename, contents):
Expand All @@ -96,35 +99,38 @@ def save_file(filename, contents):
fd.close()

def get_output_filename(filename, version):
if settings.COMPRESS_VERSION and version is not None:
return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, version)
if settings.COMPRESS_VERSION:
if version is not None:
return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, version)
else:
return filename
else:
return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, settings.COMPRESS_VERSION_DEFAULT)

def get_version(source_files, verbosity=0):
version = getattr(get_class(settings.COMPRESS_VERSIONING)(), 'get_version')(source_files)
return version

def get_version_from_file(path, filename):
regex = re.compile(r'^%s$' % (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):
result = regex.match(f)
if result and result.groups():
return result.groups()[0]

def remove_files(path, filename, verbosity=0):
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'[A-Za-z0-9]+'))))
if os.path.exists(path):
for f in os.listdir(path):
if regex.match(f):
if verbosity >= 1:
print "Removing outdated file %s" % f

os.unlink(os.path.join(path, f))

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']))

if settings.COMPRESS_VERSION:
Expand Down
9 changes: 7 additions & 2 deletions compress/versioning/mtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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(compress_source(f)).st_mtime) for f in source_files]))
times = []
for f in source_files:
try:
times.append(int(os.stat(compress_source(f)).st_mtime))
except OSError:
pass
return str(max(times))

def needs_update(self, output_file, source_files, version):

Expand Down