Commit 14ee919f by Timothée Peignier

improve storage use

parent 1ce6a1ac
import os
import subprocess
from django.core.files.base import ContentFile
from pipeline.conf import settings
from pipeline.storage import storage
from pipeline.utils import to_class
......@@ -41,9 +43,7 @@ class Compiler(object):
return content
def save_file(self, path, content):
file = storage.open(path, 'wb')
file.write(content)
file.close()
return storage.save(path, ContentFile(content))
class CompilerBase(object):
......
......@@ -94,9 +94,8 @@ class Compressor(object):
def template_name(self, path, base):
name = os.path.basename(path)
base = os.path.abspath(base)
if base:
name = re.sub(r"^%s\/(.*)%s$" % (
name = re.sub(r"^%s\/?(.*)%s$" % (
re.escape(base), re.escape(settings.PIPELINE_TEMPLATE_EXT)
), r"\1", path)
return re.sub(r"[\/\\]", "_", name)
......@@ -154,7 +153,7 @@ class Compressor(object):
paths = {}
def mhtml(match):
path = match.group(1)
if not path in paths:
if not path in paths:
paths[path] = "%s-%s" % (match.start(), os.path.basename(path))
return "url(mhtml:%s!%s)" % (asset_url, paths[path])
css = re.sub(URL_REPLACER, mhtml, css)
......@@ -239,5 +238,4 @@ class SubProcessCompressor(CompressorBase):
if self.verbose:
print error
return compressed_content
......@@ -2,6 +2,8 @@ import glob
import os
import urlparse
from django.core.files.base import ContentFile
from pipeline.conf import settings
from pipeline.compilers import Compiler
from pipeline.compressors import Compressor
......@@ -54,7 +56,7 @@ class Packager(object):
self.versioning.cleanup(package['output'])
if self.verbose or self.force:
print "Version: %s" % version
print "Saving: %s" % self.compressor.relative_path(output_filename)
print "Saving: %s" % output_filename
paths = self.compile(package['paths'])
content = compress(paths,
asset_url=self.individual_url(output_filename), **kwargs)
......@@ -72,9 +74,7 @@ class Packager(object):
return self.compressor.compile_templates(package['templates'])
def save_file(self, filename, content):
file = storage.open(filename, mode='wb+')
file.write(content)
file.close()
return storage.save(filename, ContentFile(content))
def create_packages(self, config):
packages = {}
......@@ -89,7 +89,7 @@ class Packager(object):
for path in config[name]['source_filenames']:
full_path = os.path.join(settings.PIPELINE_ROOT, path)
for path in glob.glob(full_path):
path = os.path.normpath(path).replace(settings.PIPELINE_ROOT, '')
path = os.path.relpath(path, settings.PIPELINE_ROOT)
if not path in paths:
paths.append(path)
packages[name]['paths'] = [path for path in paths if not path.endswith(settings.PIPELINE_TEMPLATE_EXT)]
......
......@@ -30,21 +30,16 @@ class Versioning(object):
def output_filename(self, filename, version):
if settings.PIPELINE_VERSION and version is not None:
output_filename = filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
return filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
version)
else:
output_filename = filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
return filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
settings.PIPELINE_VERSION_DEFAULT)
output_path = os.path.join(settings.PIPELINE_ROOT, output_filename)
return os.path.normpath(output_path)
def relative_path(self, filename):
return os.path.join(settings.PIPELINE_ROOT, filename)
def need_update(self, output_file, paths):
version = self.version(paths)
output_file = self.output_filename(output_file, version)
if not storage.exists(self.relative_path(output_file)):
if not storage.exists(output_file):
return True, version
return getattr(self.versionner, 'need_update')(output_file, paths, 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