Commit 2df0ec93 by Timothée Peignier

use django storage everywhere and add a staticfiles finder

parent c8ed240d
...@@ -2,6 +2,7 @@ import os ...@@ -2,6 +2,7 @@ import os
import subprocess import subprocess
from compress.conf import settings from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class from compress.utils import to_class
...@@ -24,7 +25,7 @@ class Compiler(object): ...@@ -24,7 +25,7 @@ class Compiler(object):
compiled_content = compiler.compile_file(content) compiled_content = compiler.compile_file(content)
self.save_file(new_path, compiled_content) self.save_file(new_path, compiled_content)
except CompilerError: except CompilerError:
if not os.path.exists(new_path): if not storage.exists(new_path):
raise raise
paths[index] = new_path paths[index] = new_path
return paths return paths
...@@ -34,15 +35,15 @@ class Compiler(object): ...@@ -34,15 +35,15 @@ class Compiler(object):
return '.'.join((path[0], extension)) return '.'.join((path[0], extension))
def read_file(self, path): def read_file(self, path):
f = open(path, 'rb') file = storage.open(path, 'rb')
content = f.read() content = file.read()
f.close() file.close()
return content return content
def save_file(self, path, content): def save_file(self, path, content):
f = open(path, 'w') file = storage.open(path, 'wb')
f.write(content) file.write(content)
f.close() file.close()
class CompilerBase(object): class CompilerBase(object):
...@@ -56,9 +57,9 @@ class CompilerBase(object): ...@@ -56,9 +57,9 @@ class CompilerBase(object):
raise NotImplementedError raise NotImplementedError
def save_file(self, path, content): def save_file(self, path, content):
f = open(path, 'w') file = storage.open(path, 'wb')
f.write(content) file.write(content)
f.close() file.close()
return path return path
......
...@@ -4,6 +4,7 @@ import subprocess ...@@ -4,6 +4,7 @@ import subprocess
import urlparse import urlparse
from compress.conf import settings from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class from compress.utils import to_class
URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+)[\'"]?\)' URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+)[\'"]?\)'
...@@ -77,9 +78,9 @@ class Compressor(object): ...@@ -77,9 +78,9 @@ class Compressor(object):
def read_file(self, path): def read_file(self, path):
"""Read file content in binary mode""" """Read file content in binary mode"""
f = open(path, 'rb') file = storage.open(path, mode='rb')
content = f.read() content = file.read()
f.close() file.close()
return content return content
......
...@@ -15,6 +15,8 @@ COMPRESS_VERSION_DEFAULT = getattr(settings, 'COMPRESS_VERSION_DEFAULT', '0') ...@@ -15,6 +15,8 @@ COMPRESS_VERSION_DEFAULT = getattr(settings, 'COMPRESS_VERSION_DEFAULT', '0')
COMPRESS_VERSION_REMOVE_OLD = getattr(settings, 'COMPRESS_VERSION_REMOVE_OLD', True) COMPRESS_VERSION_REMOVE_OLD = getattr(settings, 'COMPRESS_VERSION_REMOVE_OLD', True)
COMPRESS_VERSIONING = getattr(settings, 'COMPRESS_VERSIONING', 'compress.versioning.mtime.MTimeVersioning') COMPRESS_VERSIONING = getattr(settings, 'COMPRESS_VERSIONING', 'compress.versioning.mtime.MTimeVersioning')
COMPRESS_STORAGE = getattr(settings, 'COMPRESS_STORAGE',
'compress.storage.CompressStorage')
COMPRESS_CSS_COMPRESSORS = getattr(settings, 'COMPRESS_CSS_COMPRESSORS', [ COMPRESS_CSS_COMPRESSORS = getattr(settings, 'COMPRESS_CSS_COMPRESSORS', [
'compress.compressors.csstidy.YUICompressor' 'compress.compressors.csstidy.YUICompressor'
......
from django.contrib.staticfiles.finders import BaseStorageFinder
from compress.storage import CompressStorage
class CompressFinder(BaseStorageFinder):
storage = CompressStorage
...@@ -5,8 +5,9 @@ import urlparse ...@@ -5,8 +5,9 @@ import urlparse
from compress.conf import settings from compress.conf import settings
from compress.compilers import Compiler from compress.compilers import Compiler
from compress.compressors import Compressor from compress.compressors import Compressor
from compress.versioning import Versioning
from compress.signals import css_compressed, js_compressed from compress.signals import css_compressed, js_compressed
from compress.storage import storage
from compress.versioning import Versioning
class Packager(object): class Packager(object):
...@@ -65,12 +66,9 @@ class Packager(object): ...@@ -65,12 +66,9 @@ class Packager(object):
return self.pack(package, self.compressor.compress_js, js_compressed) return self.pack(package, self.compressor.compress_js, js_compressed)
def save_file(self, filename, content): def save_file(self, filename, content):
dirname = os.path.dirname(filename) file = storage.open(filename, mode='wb+')
if not os.path.exists(dirname): file.write(content)
os.makedirs(dirname) file.close()
fd = open(filename, 'wb+')
fd.write(content)
fd.close()
def create_packages(self, config): def create_packages(self, config):
packages = {} packages = {}
...@@ -81,7 +79,8 @@ class Packager(object): ...@@ -81,7 +79,8 @@ class Packager(object):
paths = [] paths = []
for path in config[name]['source_filenames']: for path in config[name]['source_filenames']:
full_path = os.path.join(settings.COMPRESS_ROOT, path) full_path = os.path.join(settings.COMPRESS_ROOT, path)
paths.extend([os.path.normpath(path) for path in glob.glob(full_path)]) paths.extend([os.path.normpath(path)
for path in glob.glob(full_path)])
packages[name]['paths'] = paths packages[name]['paths'] = paths
packages[name]['output'] = config[name]['output_filename'] packages[name]['output'] = config[name]['output_filename']
packages[name]['context'] = {} packages[name]['context'] = {}
......
import os
from datetime import datetime
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.functional import LazyObject
from compress.conf import settings
class CompressStorage(FileSystemStorage):
def __init__(self, location=None, base_url=None, *args, **kwargs):
if location is None:
location = settings.COMPRESS_ROOT
if base_url is None:
base_url = settings.COMPRESS_URL
super(CompressStorage, self).__init__(location, base_url, *args, **kwargs)
def accessed_time(self, name):
return datetime.fromtimestamp(os.path.getatime(self.path(name)))
def created_time(self, name):
return datetime.fromtimestamp(os.path.getctime(self.path(name)))
def modified_time(self, name):
return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
class DefaultStorage(LazyObject):
def _setup(self):
self._wrapped = get_storage_class(settings.COMPRESS_STORAGE)()
storage = DefaultStorage()
...@@ -2,6 +2,7 @@ import os ...@@ -2,6 +2,7 @@ import os
import re import re
from compress.conf import settings from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class from compress.utils import to_class
...@@ -20,7 +21,7 @@ class Versioning(object): ...@@ -20,7 +21,7 @@ class Versioning(object):
filename = settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]) filename = settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_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 = [] versions = []
for f in sorted(os.listdir(path), reverse=True): for f in sorted(storage.listdir(path), reverse=True):
version = regex.match(f) version = regex.match(f)
if version and version.groups(): if version and version.groups():
versions.append(version.group(1)) versions.append(version.group(1))
...@@ -43,7 +44,7 @@ class Versioning(object): ...@@ -43,7 +44,7 @@ class Versioning(object):
def need_update(self, output_file, paths): def need_update(self, output_file, paths):
version = self.version(paths) version = self.version(paths)
output_file = self.output_filename(output_file, version) output_file = self.output_filename(output_file, version)
if not os.path.exists(self.relative_path(output_file)): if not storage.exists(self.relative_path(output_file)):
return True, version return True, version
return getattr(self.versionner, 'need_update')(output_file, paths, version) return getattr(self.versionner, 'need_update')(output_file, paths, version)
...@@ -53,12 +54,12 @@ class Versioning(object): ...@@ -53,12 +54,12 @@ class Versioning(object):
filename = settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]) filename = settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)])
path = os.path.dirname(filename) path = os.path.dirname(filename)
regex = re.compile(r'^%s$' % os.path.basename(self.output_filename(filename, r'([A-Za-z0-9]+)'))) regex = re.compile(r'^%s$' % os.path.basename(self.output_filename(filename, r'([A-Za-z0-9]+)')))
if os.path.exists(path): if storage.exists(path):
for f in os.listdir(path): for f in storage.listdir(path):
if regex.match(f): if regex.match(f):
if self.verbose: if self.verbose:
print "Removing outdated file %s" % f print "Removing outdated file %s" % f
os.unlink(os.path.join(path, f)) storage.delete(os.path.join(path, f))
class VersioningBase(object): class VersioningBase(object):
......
...@@ -2,6 +2,7 @@ import cStringIO ...@@ -2,6 +2,7 @@ import cStringIO
from hashlib import md5, sha1 from hashlib import md5, sha1
from compress.conf import settings from compress.conf import settings
from compress.storage import storage
from compress.versioning import VersioningBase from compress.versioning import VersioningBase
...@@ -26,9 +27,9 @@ class HashVersioningBase(VersioningBase): ...@@ -26,9 +27,9 @@ class HashVersioningBase(VersioningBase):
def read_file(self, path): def read_file(self, path):
"""Read file content in binary mode""" """Read file content in binary mode"""
f = open(path, 'rb') file = storage.open(path, 'rb')
content = f.read() content = file.read()
f.close() file.close()
return content return content
def version(self, paths): def version(self, paths):
......
import os import time
from compress.storage import storage
from compress.versioning import VersioningBase from compress.versioning import VersioningBase
...@@ -7,9 +8,9 @@ class MTimeVersioning(VersioningBase): ...@@ -7,9 +8,9 @@ class MTimeVersioning(VersioningBase):
def version(self, paths): def version(self, paths):
# Return the modification time for the newest source file # Return the modification time for the newest source file
return str(max( return str(max(
[int(os.stat(path).st_mtime) for path in paths] [int(time.mktime(storage.modified_time(path).timetuple())) for path in paths]
)) ))
def need_update(self, output_file, paths, version): def need_update(self, output_file, paths, version):
output_filename = self.output_filename(output_file, version) output_filename = self.output_filename(output_file, version)
return (int(os.stat(output_filename).st_mtime) < int(version)), version return (int(time.mktime(storage.modified_time(output_filename).timetuple())) < 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