Commit 930bbc4d by Timothée Peignier

fix writinh trouble with FinderStorage

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