Commit 5f0d598b by ara818

Add versioning based on Git revisions. This provides a fast way to form…

Add versioning based on Git revisions. This provides a fast way to form Javascript versions that will be consistent when multiservers try to form versioned files.
parent 6f131907
...@@ -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