Commit 2df0ec93 by Timothée Peignier

use django storage everywhere and add a staticfiles finder

parent c8ed240d
......@@ -2,6 +2,7 @@ import os
import subprocess
from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class
......@@ -24,7 +25,7 @@ class Compiler(object):
compiled_content = compiler.compile_file(content)
self.save_file(new_path, compiled_content)
except CompilerError:
if not os.path.exists(new_path):
if not storage.exists(new_path):
raise
paths[index] = new_path
return paths
......@@ -34,15 +35,15 @@ class Compiler(object):
return '.'.join((path[0], extension))
def read_file(self, path):
f = open(path, 'rb')
content = f.read()
f.close()
file = storage.open(path, 'rb')
content = file.read()
file.close()
return content
def save_file(self, path, content):
f = open(path, 'w')
f.write(content)
f.close()
file = storage.open(path, 'wb')
file.write(content)
file.close()
class CompilerBase(object):
......@@ -56,9 +57,9 @@ class CompilerBase(object):
raise NotImplementedError
def save_file(self, path, content):
f = open(path, 'w')
f.write(content)
f.close()
file = storage.open(path, 'wb')
file.write(content)
file.close()
return path
......
......@@ -4,6 +4,7 @@ import subprocess
import urlparse
from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class
URL_DETECTOR = r'url\([\'"]?([^\s)]+\.[a-z]+)[\'"]?\)'
......@@ -77,9 +78,9 @@ class Compressor(object):
def read_file(self, path):
"""Read file content in binary mode"""
f = open(path, 'rb')
content = f.read()
f.close()
file = storage.open(path, mode='rb')
content = file.read()
file.close()
return content
......
......@@ -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_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.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
from compress.conf import settings
from compress.compilers import Compiler
from compress.compressors import Compressor
from compress.versioning import Versioning
from compress.signals import css_compressed, js_compressed
from compress.storage import storage
from compress.versioning import Versioning
class Packager(object):
......@@ -65,12 +66,9 @@ class Packager(object):
return self.pack(package, self.compressor.compress_js, js_compressed)
def save_file(self, filename, content):
dirname = os.path.dirname(filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
fd = open(filename, 'wb+')
fd.write(content)
fd.close()
file = storage.open(filename, mode='wb+')
file.write(content)
file.close()
def create_packages(self, config):
packages = {}
......@@ -81,7 +79,8 @@ class Packager(object):
paths = []
for path in config[name]['source_filenames']:
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]['output'] = config[name]['output_filename']
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
import re
from compress.conf import settings
from compress.storage import storage
from compress.utils import to_class
......@@ -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)])
regex = re.compile(r'^%s$' % self.output_filename(filename, r'([A-Za-z0-9]+)'))
versions = []
for f in sorted(os.listdir(path), reverse=True):
for f in sorted(storage.listdir(path), reverse=True):
version = regex.match(f)
if version and version.groups():
versions.append(version.group(1))
......@@ -43,7 +44,7 @@ class Versioning(object):
def need_update(self, output_file, paths):
version = self.version(paths)
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 getattr(self.versionner, 'need_update')(output_file, paths, version)
......@@ -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)])
path = os.path.dirname(filename)
regex = re.compile(r'^%s$' % os.path.basename(self.output_filename(filename, r'([A-Za-z0-9]+)')))
if os.path.exists(path):
for f in os.listdir(path):
if storage.exists(path):
for f in storage.listdir(path):
if regex.match(f):
if self.verbose:
print "Removing outdated file %s" % f
os.unlink(os.path.join(path, f))
storage.delete(os.path.join(path, f))
class VersioningBase(object):
......
......@@ -2,6 +2,7 @@ import cStringIO
from hashlib import md5, sha1
from compress.conf import settings
from compress.storage import storage
from compress.versioning import VersioningBase
......@@ -26,9 +27,9 @@ class HashVersioningBase(VersioningBase):
def read_file(self, path):
"""Read file content in binary mode"""
f = open(path, 'rb')
content = f.read()
f.close()
file = storage.open(path, 'rb')
content = file.read()
file.close()
return content
def version(self, paths):
......
import os
import time
from compress.storage import storage
from compress.versioning import VersioningBase
......@@ -7,9 +8,9 @@ class MTimeVersioning(VersioningBase):
def version(self, paths):
# Return the modification time for the newest source file
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):
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