From fcb56c900355d6909489a3b0a83edb013fb8c506 Mon Sep 17 00:00:00 2001 From: Dirk Eschler Date: Fri, 16 Nov 2012 00:33:32 +0100 Subject: [PATCH] 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. --- docs/modeltranslation/conf.py | 11 ++--- modeltranslation/__init__.py | 88 +++++++++++++++++++++-------------- setup.py | 2 +- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/docs/modeltranslation/conf.py b/docs/modeltranslation/conf.py index 12160fd..e9eb939 100644 --- a/docs/modeltranslation/conf.py +++ b/docs/modeltranslation/conf.py @@ -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' diff --git a/modeltranslation/__init__.py b/modeltranslation/__init__.py index e84e699..77598d7 100644 --- a/modeltranslation/__init__.py +++ b/modeltranslation/__init__.py @@ -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') diff --git a/setup.py b/setup.py index 5727081..b823636 100755 --- a/setup.py +++ b/setup.py @@ -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(