diff --git a/compress/templatetags/compressed.py b/compress/templatetags/compressed.py index 59818c8..c5809f1 100644 --- a/compress/templatetags/compressed.py +++ b/compress/templatetags/compressed.py @@ -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): @@ -48,7 +48,7 @@ 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) @@ -56,7 +56,7 @@ def render(self, context): 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 @@ -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) diff --git a/compress/utils.py b/compress/utils.py index b6e7139..ffcbef4 100644 --- a/compress/utils.py +++ b/compress/utils.py @@ -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 @@ -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) @@ -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): @@ -96,15 +99,18 @@ 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): @@ -112,19 +118,19 @@ def get_version_from_file(path, filename): 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: diff --git a/compress/versioning/mtime/__init__.py b/compress/versioning/mtime/__init__.py index 62a0ad7..0e414b5 100644 --- a/compress/versioning/mtime/__init__.py +++ b/compress/versioning/mtime/__init__.py @@ -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):