Commit b027c4d7 by Timothée Peignier

remove versionning class

parent 1d5f564a
import os
import re
from django.core.cache import cache
from pipeline.conf import settings
from pipeline.storage import storage
from pipeline.utils import to_class
class Versioning(object):
def __init__(self, verbose=False):
self.verbose = verbose
def versioner(self):
return to_class(settings.PIPELINE_VERSIONING)(self)
versioner = property(versioner)
def version(self, paths):
return getattr(self.versioner, 'version')(paths)
def version_from_file(self, path, filename, force=False):
version = cache.get("pipeline:%s" % filename)
if (not version) or force:
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part)
for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)])
regex = re.compile(r'^%s$' % self.output_filename(filename, r'([A-Za-z0-9]+)'))
for f in sorted(storage.listdir(path)[1], reverse=True):
match = regex.match(f)
if match and match.groups():
version = match.group(1)
break
cache.set("pipeline:%s" % filename, version, settings.PIPELINE_CACHE_TIMEOUT)
return str(version)
def output_filename(self, filename, version):
if settings.PIPELINE_VERSION and version is not None:
return filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
version)
else:
return filename.replace(settings.PIPELINE_VERSION_PLACEHOLDER,
settings.PIPELINE_VERSION_DEFAULT)
def need_update(self, output_file, paths):
version = self.version(paths)
output_file = self.output_filename(output_file, version)
return getattr(self.versioner, 'need_update')(output_file, paths, version)
def cleanup(self, filename):
if not (settings.PIPELINE_VERSION and settings.PIPELINE_VERSION_REMOVE_OLD):
return # Nothing to delete here
path = os.path.dirname(filename)
filename = os.path.basename(filename)
filename = settings.PIPELINE_VERSION_PLACEHOLDER.join([re.escape(part)
for part in filename.split(settings.PIPELINE_VERSION_PLACEHOLDER)])
regex = re.compile(r'^%s$' % self.output_filename(filename, r'([A-Za-z0-9]+)'))
try:
for f in storage.listdir(path)[1]:
if regex.match(f):
if self.verbose:
print "Removing outdated file %s" % f
storage.delete(os.path.join(path, f))
except EnvironmentError:
# We can't use exists() first because some backends (S3) have no concept of directories.
pass
class VersioningBase(object):
def __init__(self, versioning):
self.versioning = versioning
def output_filename(self, filename, version):
return self.versioning.output_filename(filename, version)
def version(self, source_files):
raise NotImplementedError
def needs_update(self, output_file, paths, version):
raise NotImplementedError
class VersioningError(Exception):
"""This exception is raised when version creation fails"""
pass
import cStringIO
from hashlib import md5, sha1
from pipeline.conf import settings
from pipeline.storage import storage
from pipeline.versioning import VersioningBase
class HashVersioningBase(VersioningBase):
def __init__(self, versioning, hash_method):
super(HashVersioningBase, self).__init__(versioning)
self.hash_method = hash_method
def need_update(self, output_file, paths, version):
output_file_name = self.output_filename(output_file, version)
placeholder = settings.PIPELINE_VERSION_PLACEHOLDER
try:
placeholder_index = output_file.index(placeholder)
old_version = output_file_name[placeholder_index:placeholder_index + len(placeholder) - len(output_file)]
return (version != old_version), version
except ValueError:
# no placeholder found, do not update, manual update if needed
return False, version
def concatenate(self, paths):
"""Concatenate together a list of files"""
return '\n'.join([self.read_file(path) for path in paths])
def read_file(self, path):
"""Read file content in binary mode"""
file = storage.open(path, 'rb')
content = file.read()
file.close()
return content
def version(self, paths):
buf = self.concatenate(paths)
s = cStringIO.StringIO(buf)
version = self.get_hash(s)
s.close()
return version
def get_hash(self, f, CHUNK=2 ** 16):
m = self.hash_method()
while 1:
chunk = f.read(CHUNK)
if not chunk:
break
m.update(chunk)
return m.hexdigest()
class MD5Versioning(HashVersioningBase):
def __init__(self, versioning):
super(MD5Versioning, self).__init__(versioning, md5)
class SHA1Versioning(HashVersioningBase):
def __init__(self, versioning):
super(SHA1Versioning, self).__init__(versioning, sha1)
import time
from pipeline.storage import storage
from pipeline.versioning import VersioningBase
class MTimeVersioning(VersioningBase):
def version(self, paths):
# Return the modification time for the newest source file
return str(max(
[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)
try:
modified_time = storage.modified_time(output_filename)
except Exception:
return True, version
return (int(time.mktime(modified_time.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