Commit 9662e69f by Timothée Peignier

Merge remote branch 'ara818/master'

parents f7917eca 4532912e
...@@ -17,6 +17,13 @@ COMPRESS_JS_FILTERS = getattr(settings, 'COMPRESS_JS_FILTERS', ['compress.filter ...@@ -17,6 +17,13 @@ COMPRESS_JS_FILTERS = getattr(settings, 'COMPRESS_JS_FILTERS', ['compress.filter
COMPRESS_CSS = getattr(settings, 'COMPRESS_CSS', {}) COMPRESS_CSS = getattr(settings, 'COMPRESS_CSS', {})
COMPRESS_JS = getattr(settings, 'COMPRESS_JS', {}) COMPRESS_JS = getattr(settings, 'COMPRESS_JS', {})
COMPRESS_YUI_BINARY = getattr(settings, 'COMPRESS_YUI_BINARY', 'java -jar yuicompressor.jar')
COMPRESS_YUI_CSS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_CSS_ARGUMENTS', '')
COMPRESS_YUI_JS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_JS_ARGUMENTS', '')
COMPRESS_CLOSURE_BINARY = getattr(settings, 'COMPRESS_CLOSURE_BINARY', 'java -jar compiler.jar')
COMPRESS_CLOSURE_JS_ARGUMENTS = getattr(settings, 'COMPRESS_CLOSURE_JS_ARGUMENTS', '')
if COMPRESS_CSS_FILTERS is None: if COMPRESS_CSS_FILTERS is None:
COMPRESS_CSS_FILTERS = [] COMPRESS_CSS_FILTERS = []
......
import subprocess
from compress.conf import settings
from compress.filter_base import FilterBase, FilterError
class ClosureCompressorFilter(FilterBase):
def filter_common(self, content, type_, arguments):
command = '%s %s' % (settings.COMPRESS_CLOSURE_BINARY, arguments)
if self.verbose:
command += ' --verbose'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, \
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(content)
p.stdin.close()
filtered_css = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
p.stderr.close()
if p.wait() != 0:
if not err:
err = 'Unable to apply Closure Compressor filter'
raise FilterError(err)
if self.verbose:
print err
return filtered_css
def filter_js(self, js):
return self.filter_common(js, 'js', settings.COMPRESS_CLOSURE_JS_ARGUMENTS)
\ No newline at end of file
import subprocess import subprocess
from django.conf import settings from compress.conf import settings
from compress.filter_base import FilterBase, FilterError from compress.filter_base import FilterBase, FilterError
BINARY = getattr(settings, 'COMPRESS_YUI_BINARY', 'java -jar yuicompressor.jar')
CSS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_CSS_ARGUMENTS', '')
JS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_JS_ARGUMENTS', '')
class YUICompressorFilter(FilterBase): class YUICompressorFilter(FilterBase):
def filter_common(self, content, type_, arguments): def filter_common(self, content, type_, arguments):
command = '%s --type=%s %s' % (BINARY, type_, arguments) command = '%s --type=%s %s' % (settings.COMPRESS_YUI_BINARY, type_, arguments)
if self.verbose: if self.verbose:
command += ' --verbose' command += ' --verbose'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, \
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.write(content) p.stdin.write(content)
p.stdin.close() p.stdin.close()
...@@ -38,7 +34,7 @@ class YUICompressorFilter(FilterBase): ...@@ -38,7 +34,7 @@ class YUICompressorFilter(FilterBase):
return filtered_css return filtered_css
def filter_js(self, js): def filter_js(self, js):
return self.filter_common(js, 'js', JS_ARGUMENTS) return self.filter_common(js, 'js', settings.COMPRESS_YUI_JS_ARGUMENTS)
def filter_css(self, css): def filter_css(self, css):
return self.filter_common(css, 'css', CSS_ARGUMENTS) return self.filter_common(css, 'css', settings.COMPRESS_YUI_CSS_ARGUMENTS)
\ No newline at end of file \ No newline at end of file
...@@ -59,7 +59,7 @@ class CompressedCSSNode(template.Node): ...@@ -59,7 +59,7 @@ class CompressedCSSNode(template.Node):
css['source_filenames']) css['source_filenames'])
if u: if u:
filter_css(css) filter_css(css)
elif not css.get('extra_context', {}).get('prefix', None): else:
filename_base, filename = os.path.split(css['output_filename']) filename_base, filename = os.path.split(css['output_filename'])
path_name = compress_root(filename_base) path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename) version = get_version_from_file(path_name, filename)
...@@ -100,7 +100,7 @@ class CompressedJSNode(template.Node): ...@@ -100,7 +100,7 @@ class CompressedJSNode(template.Node):
js['source_filenames']) js['source_filenames'])
if u: if u:
filter_js(js) filter_js(js)
elif not js.get('extra_context', {}).get('prefix', None): else:
filename_base, filename = os.path.split(js['output_filename']) filename_base, filename = os.path.split(js['output_filename'])
path_name = compress_root(filename_base) path_name = compress_root(filename_base)
version = get_version_from_file(path_name, filename) version = get_version_from_file(path_name, filename)
......
...@@ -37,6 +37,17 @@ def get_mod_func(callback): ...@@ -37,6 +37,17 @@ def get_mod_func(callback):
except ValueError: except ValueError:
return callback, '' return callback, ''
return callback[:dot], callback[dot+1:] return callback[:dot], callback[dot+1:]
def get_hexdigest(plaintext):
"""
Create a hexdigest from a plaintext string
"""
try:
import hashlib
return hashlib.sha1(plaintext).hexdigest()
except ImportError:
import sha
return sha.new(plaintext).hexdigest()
def needs_update(output_file, source_files, verbosity=0): def needs_update(output_file, source_files, verbosity=0):
""" """
......
import os
from compress.conf import settings
from compress.utils import get_output_filename, get_hexdigest, compress_source
from compress.versioning.base import VersioningBase, VersioningError
try:
import git
except ImportError:
raise VersioningError("Must have GitPython package installed to use git versioning")
class GitVersioningBase(VersioningBase):
def needs_update(self, output_file, source_files, version):
output_file_name = get_output_filename(output_file, version)
ph = settings.COMPRESS_VERSION_PLACEHOLDER
of = output_file
try:
phi = of.index(ph)
old_version = output_file_name[phi:phi+len(ph)-len(of)]
return (version != old_version), version
except ValueError:
# no placeholder found, do not update, manual update if needed
return False, version
class GitRevVersioning(GitVersioningBase):
"""
Version as hash of revision of all files in sources_files list.
"""
def get_version(self, source_files):
repo = git.Repo(compress_source(source_files[0]))
kwargs = {'max_count' : 1}
commit_revs = []
for f in source_files:
commit = [i for i in repo.iter_commits(paths=compress_source(f), **kwargs)][0]
commit_revs.append(commit.name_rev)
return get_hexdigest(', '.join(commit_revs))[0:16]
class GitHeadRevVersioning(GitVersioningBase):
"""
Version as hash of latest revision in HEAD. Assumes all sources_files in same git repo.
"""
def get_version(self, source_files):
f = source_files[0]
repo = git.Repo(compress_source(f))
return get_hexdigest(repo.head.commit.name_rev)[0:16]
\ No newline at end of file
...@@ -26,6 +26,33 @@ To generate SHA-1 version strings, put this in your settings.py: ...@@ -26,6 +26,33 @@ To generate SHA-1 version strings, put this in your settings.py:
COMPRESS_VERSIONING = 'compress.versioning.hash.SHA1Versioning' COMPRESS_VERSIONING = 'compress.versioning.hash.SHA1Versioning'
</code></pre> </code></pre>
h3. Git version strings
Versions formed on git revisions in codebase. Provides a fast way to check if any of your files changed that
will be consistent across multiple web servers so that they all generate the same version numbers for each
set of source files, assuming their git repositories are all in sync.
Assumes deployment is git repositiory. Requires GitPython 0.2.0.
GitPython 0.3.0 uses an async library that does not currently play well with Django. To install using Git just do
pip install GitPython==0.2.0-beta1.
h4. Git revision version strings.
To generate versions based on revision of every file in your source file list, put this in your settings.py:
<pre><code>
COMPRESS_VERSIONING = 'compress.versioning.git.GitVersioningBase'
</code></pre>
h4.Git HEAD last revision version strings.
To generate versions based on the latest revision of HEAD in your git repository (which assumes all of your source files are in the
same repository), put this in your settings.py:
<pre><code>
COMPRESS_VERSIONING = 'compress.versioning.git.GitHeadRevVersioning'
</code></pre>
h3. Write your own versioning class h3. Write your own versioning class
To write your own versioning class, you can subclass one of the available versioning classes. To write your own versioning class, you can subclass one of the available versioning classes.
......
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