diff --git a/django/__init__.py b/django/__init__.py index 6467c033bb..3417c7158a 100644 --- a/django/__init__.py +++ b/django/__init__.py @@ -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 diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 367263955c..bb03082be0 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -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. diff --git a/setup.py b/setup.py index af18af78ff..975ddcb255 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 948eb3a659..0a7c56c86e 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -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" diff --git a/tests/regressiontests/version/__init__.py b/tests/regressiontests/version/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/version/models.py b/tests/regressiontests/version/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/version/tests.py b/tests/regressiontests/version/tests.py new file mode 100644 index 0000000000..1a67483c1f --- /dev/null +++ b/tests/regressiontests/version/tests.py @@ -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) +