Commit 14ee919f by Timothée Peignier

improve storage use

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