Adopted code from Django's development version to derive a PEP386-compliant version number from VERSION. We shouldn't reinvent the wheel. Includes support to add a git timestamp for dev snapshots if a .git directory is present.

This commit is contained in:
Dirk Eschler 2012-11-16 00:33:32 +01:00
parent ae09abf76b
commit fcb56c9003
3 changed files with 60 additions and 41 deletions

View file

@ -24,12 +24,11 @@ try:
# for |version| and |release|, also used in various other places throughout
# the built documents.
#
# The short X.Y version.
#version = '0.4'
version = modeltranslation.get_version(pep386=False, short=True)
# The full version, including alpha/beta/rc tags.
#release = '0.4.0-rc1'
release = modeltranslation.get_version(pep386=False)
# The short X.Y version (e.g.'0.5').
version = '.'.join(str(i) for i in modeltranslation.VERSION[:2])
# The full PEP386-compliant version number version, including
# normalized alpha/beta/rc/dev tags (e.g. '0.5a1').
release = modeltranslation.get_version()
except ImportError:
version = 'dev'
release = 'dev'

View file

@ -1,44 +1,64 @@
# -*- coding: utf-8 -*-
VERSION = (0, 4, 0, 'final', 0)
"""
Version code adopted from Django development version.
https://github.com/django/django
"""
VERSION = (0, 5, 0, 'alpha', 0)
def get_version(version=None, pep386=True, short=False):
def get_version(version=None):
"""
Derives a version number as string from VERSION.
If the ``pep386`` parameter is ``True`` (default) the returned version will
be PEP386-compliant (e.g. 0.4.0c1), else the release style naming
will be used (e.g. 0.4.0-rc1).
If the ``short`` parameter is ``True``, the release style naming version
will omit the patch level and sub (e.g. 0.4). This is for example used for
sphinx.
Returns a PEP 386-compliant version number from VERSION.
"""
if version is None:
version = VERSION
assert len(version) == 5
assert version[3] in ('alpha', 'beta', 'rc', 'final')
from modeltranslation import VERSION as version
else:
assert len(version) == 5
assert version[3] in ('alpha', 'beta', 'rc', 'final')
if pep386:
# Now build the two parts of the version number:
# main = X.Y[.Z]
# sub = {a|b|c}N - for alpha, beta and rc releases
parts = 2 if version[2] == 0 else 3
main = '.'.join(str(x) for x in version[:parts])
sub = ''
if version[3] == 'alpha' and version[4] == 0:
sub = 'a%d' % (version[4])
elif version[3] != 'final':
mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
sub = mapping[version[3]] + str(version[4])
return main + sub
# Now build the two parts of the version number:
# main = X.Y[.Z]
# sub = .devN - for pre-alpha releases
# | {a|b|c}N - for alpha, beta and rc releases
if short:
return '%d.%d'.format(version[0], version[1])
if version[3] != 'final':
return '%d.%d.%d-%s%d' % (
version[0], version[1], version[2], version[3], version[4])
return '%d.%d.%d' % (version[0], version[1], version[2])
parts = 2 if version[2] == 0 else 3
main = '.'.join(str(x) for x in version[:parts])
sub = ''
if version[3] == 'alpha' and version[4] == 0:
git_changeset = get_git_changeset()
if git_changeset:
sub = '.dev%s' % git_changeset
elif version[3] != 'final':
mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
sub = mapping[version[3]] + str(version[4])
return str(main + sub)
__version__ = get_version()
def get_git_changeset():
"""
Returns a numeric identifier of the latest git changeset.
The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
This value isn't guaranteed to be unique, but collisions are very unlikely,
so it's sufficient for generating the development version numbers.
TODO: Check if we can rely on services like read-the-docs to pick this up.
"""
import datetime
import os
import subprocess
repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
git_log = subprocess.Popen(
'git log --pretty=format:%ct --quiet -1 HEAD', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True, cwd=repo_dir,
universal_newlines=True)
timestamp = git_log.communicate()[0]
try:
timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
except ValueError:
return None
return timestamp.strftime('%Y%m%d%H%M%S')

View file

@ -2,7 +2,7 @@
from distutils.core import setup
# Dynamically calculate the version based on modeltranslation.VERSION.
version = __import__('modeltranslation').get_version(pep386=False)
version = __import__('modeltranslation').get_version()
setup(