Commits

Anonymous committed af03990

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

Comments (0)

Files changed (7)

django/__init__.py

 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

django/core/management/__init__.py

 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.
         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",

tests/regressiontests/admin_scripts/tests.py

         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"
Add a comment to this file

tests/regressiontests/version/__init__.py

Empty file added.

Add a comment to this file

tests/regressiontests/version/models.py

Empty file added.

tests/regressiontests/version/tests.py

+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)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.