Commit 930bbc4d by Timothée Peignier

fix writinh trouble with FinderStorage

parent cc66882b
import os
import subprocess
from django.core.files.base import ContentFile
from django.utils.encoding import smart_str
from pipeline.conf import settings
from pipeline.storage import storage
from pipeline.storage import default_storage
from pipeline.utils import to_class
......@@ -22,10 +25,10 @@ class Compiler(object):
new_path = self.output_path(path, compiler.output_extension)
content = self.read_file(path)
try:
compiled_content = compiler.compile_file(content, storage.path(path))
compiled_content = compiler.compile_file(content, default_storage.path(path))
self.save_file(new_path, compiled_content)
except CompilerError:
if not storage.exists(new_path) or not settings.PIPELINE:
if not default_storage.exists(new_path) or not settings.PIPELINE:
raise
paths[index] = new_path
return paths
......@@ -35,15 +38,13 @@ class Compiler(object):
return '.'.join((path[0], extension))
def read_file(self, path):
file = storage.open(path, 'rb')
file = default_storage.open(path, 'rb')
content = file.read()
file.close()
return content
def save_file(self, path, content):
file = storage.open(path, 'wb')
file.write(content)
file.close()
return default_storage.save(path, ContentFile(smart_str(content)))
class CompilerBase(object):
......@@ -57,10 +58,7 @@ class CompilerBase(object):
raise NotImplementedError
def save_file(self, path, content):
file = storage.open(path, 'wb')
file.write(content)
file.close()
return path
return default_storage.save(path, ContentFile(smart_str(content)))
class CompilerError(Exception):
......
......@@ -6,8 +6,8 @@ import subprocess
from itertools import takewhile
from pipeline.conf import settings
from pipeline.storage import storage
from pipeline.utils import filepath_to_uri, to_class, relpath
from pipeline.storage import default_storage
from pipeline.utils import to_class, relpath
MAX_IMAGE_SIZE = 32700
......@@ -159,7 +159,7 @@ class Compressor(object):
font = ext in FONT_EXTS
if not variant:
return False
if not (re.search(EMBEDDABLE, path) and storage.exists(path)):
if not (re.search(EMBEDDABLE, path) and default_storage.exists(path)):
return False
if not ext in EMBED_EXTS:
return False
......@@ -217,19 +217,19 @@ class Compressor(object):
given the path of the stylesheet that contains it.
"""
if os.path.isabs(path):
path = os.path.join(storage.location, path)
path = os.path.join(default_storage.location, path)
else:
path = os.path.join(start, path)
return os.path.normpath(path)
def relative_path(self, absolute_path):
"""Rewrite paths relative to the output stylesheet path"""
absolute_path = self.absolute_path(absolute_path, storage.location)
return os.path.join(os.sep, relpath(absolute_path, storage.location))
absolute_path = self.absolute_path(absolute_path, default_storage.location)
return os.path.join(os.sep, relpath(absolute_path, default_storage.location))
def read_file(self, path):
"""Read file content in binary mode"""
file = storage.open(path, 'rb')
file = default_storage.open(path, 'rb')
content = file.read()
file.close()
return content
......
......@@ -2,7 +2,7 @@ import os
import re
import fnmatch
from pipeline.storage import storage
from pipeline.storage import default_storage
__all__ = ["glob", "iglob"]
......@@ -49,7 +49,7 @@ def iglob(pathname):
def glob1(dirname, pattern):
try:
directories, files = storage.listdir(dirname)
directories, files = default_storage.listdir(dirname)
names = directories + files
except NotImplementedError:
return []
......@@ -59,7 +59,7 @@ def glob1(dirname, pattern):
def glob0(dirname, basename):
if storage.exists(os.path.join(dirname, basename)):
if default_storage.exists(os.path.join(dirname, basename)):
return [basename]
return []
......
......@@ -6,7 +6,7 @@ from pipeline.compilers import Compiler
from pipeline.compressors import Compressor
from pipeline.glob import glob
from pipeline.signals import css_compressed, js_compressed
from pipeline.storage import storage
from pipeline.storage import default_storage
class Package(object):
......@@ -85,7 +85,7 @@ class Packager(object):
)
def individual_url(self, filename):
return storage.url(filename)
return default_storage.url(filename)
def pack_stylesheets(self, package, **kwargs):
return self.pack(package, self.compressor.compress_css, css_compressed,
......@@ -113,7 +113,7 @@ class Packager(object):
return self.compressor.compile_templates(package.templates)
def save_file(self, path, content):
return storage.save(path, ContentFile(smart_str(content)))
return default_storage.save(path, ContentFile(smart_str(content)))
def create_packages(self, config):
packages = {}
......
import os
try:
from staticfiles import finders
from staticfiles.storage import CachedStaticFilesStorage, StaticFilesStorage
......@@ -65,6 +67,20 @@ class BaseFinderStorage(PipelineStorage):
exists = super(BaseFinderStorage, self).exists(name)
return exists
def listdir(self, path):
for finder in finders.get_finders():
for storage in finder.storages.values():
try:
return storage.listdir(path)
except OSError:
pass
def _save(self, name, content):
for finder in finders.get_finders():
for path, storage in finder.list([]):
if os.path.dirname(name) in path:
return storage._save(name, content)
class PipelineFinderStorage(BaseFinderStorage):
finders = finders
......@@ -75,4 +91,4 @@ class DefaultStorage(LazyObject):
self._wrapped = get_storage_class(settings.PIPELINE_STORAGE)()
storage = DefaultStorage()
default_storage = DefaultStorage()
......@@ -7,7 +7,7 @@ from django.test import TestCase
from pipeline.compressors import Compressor, TEMPLATE_FUNC
from pipeline.compressors.yui import YUICompressor
from pipeline.storage import storage
from pipeline.storage import default_storage as storage
class CompressorTest(TestCase):
......
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