Implemented PEP386-compatible version numbers. Thanks Jannis for the guidance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17357 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2012-01-08 15:05:15 +00:00
parent aa4e152296
commit 40f0ecc56a
7 changed files with 56 additions and 32 deletions

View File

@ -1,30 +1,30 @@
VERSION = (1, 4, 0, 'alpha', 1)
def get_version():
version = '%s.%s' % (VERSION[0], VERSION[1])
if VERSION[2]:
version = '%s.%s' % (version, VERSION[2])
if VERSION[3:] == ('alpha', 0):
version = '%s pre-alpha' % version
else:
if VERSION[3] != 'final':
version = '%s %s %s' % (version, VERSION[3], VERSION[4])
from django.utils.version import get_svn_revision
svn_rev = get_svn_revision()
if svn_rev != u'SVN-unknown':
version = "%s %s" % (version, svn_rev)
return version
def get_version(version=None):
"""Derives a PEP386-compliant version number from VERSION."""
if version is None:
version = VERSION
assert len(version) == 5
assert version[3] in ('alpha', 'beta', 'rc', 'final')
def get_distutils_version():
# Distutils expects a version number formatted as major.minor[.patch][sub]
parts = 5
if VERSION[3] == 'final':
parts = 3
if VERSION[2] == 0:
parts = 2
version = VERSION[:parts]
version = [str(x)[0] for x in version] # ['1', '4', '0', 'a', '1']
if parts > 2:
version[2:] = [''.join(version[2:])] # ['1', '4', '0a1']
version = '.'.join(version) # '1.4.0a1'
return version
# 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
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:
# At the toplevel, this would cause an import loop.
from django.utils.version import get_svn_revision
svn_revision = get_svn_revision()[4:]
if svn_revision != 'unknown':
sub = '.dev%s' % svn_revision
elif version[3] != 'final':
mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
sub = mapping[version[3]] + str(version[4])
return main + sub

View File

@ -4,12 +4,11 @@ from optparse import OptionParser, NO_DEFAULT
import imp
import warnings
import django
from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.utils.importlib import import_module
# For backwards compatibility: get_version() used to be in this module.
get_version = django.get_version
from django import get_version
# A cache of loaded commands, so that call_command
# doesn't have to reload every time it's called.

View File

@ -66,7 +66,7 @@ if len(sys.argv) > 1 and sys.argv[1] == 'bdist_wininst':
file_info[0] = '\\PURELIB\\%s' % file_info[0]
# Dynamically calculate the version based on django.VERSION.
version = __import__('django').get_distutils_version()
version = __import__('django').get_version()
setup(
name = "Django",

View File

@ -1177,8 +1177,7 @@ class CommandTypes(AdminScriptTestCase):
args = ['--version']
out, err = self.run_manage(args)
self.assertNoOutput(err)
# Only check the first part of the version number
self.assertOutput(out, get_version().split('-')[0])
self.assertOutput(out, get_version())
def test_help(self):
"--help is handled as a special case"

View File

View File

@ -0,0 +1,26 @@
import re
from django import get_version
from django.utils.unittest import TestCase
class VersionTests(TestCase):
def test_development(self):
ver_tuple = (1, 4, 0, 'alpha', 0)
# This will return a different result when it's run within or outside
# of a SVN checkout: 1.4.devNNNNN or 1.4.
ver_string = get_version(ver_tuple)
self.assertRegexpMatches(ver_string, r'1\.4(\.dev\d+)?')
def test_releases(self):
tuples_to_strings = (
((1, 4, 0, 'alpha', 1), '1.4a1'),
((1, 4, 0, 'beta', 1), '1.4b1'),
((1, 4, 0, 'rc', 1), '1.4c1'),
((1, 4, 0, 'final', 0), '1.4'),
((1, 4, 1, 'rc', 2), '1.4.1c2'),
((1, 4, 1, 'final', 0), '1.4.1'),
)
for ver_tuple, ver_string in tuples_to_strings:
self.assertEqual(get_version(ver_tuple), ver_string)