Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 40f0ecc56a23d35c2849f8e79276f6d8931412d1 1 parent aa4e152
Aymeric Augustin authored January 08, 2012
54  django/__init__.py
... ...
@@ -1,30 +1,30 @@
1 1
 VERSION = (1, 4, 0, 'alpha', 1)
2 2
 
3  
-def get_version():
4  
-    version = '%s.%s' % (VERSION[0], VERSION[1])
5  
-    if VERSION[2]:
6  
-        version = '%s.%s' % (version, VERSION[2])
7  
-    if VERSION[3:] == ('alpha', 0):
8  
-        version = '%s pre-alpha' % version
9  
-    else:
10  
-        if VERSION[3] != 'final':
11  
-            version = '%s %s %s' % (version, VERSION[3], VERSION[4])
12  
-    from django.utils.version import get_svn_revision
13  
-    svn_rev = get_svn_revision()
14  
-    if svn_rev != u'SVN-unknown':
15  
-        version = "%s %s" % (version, svn_rev)
16  
-    return version
  3
+def get_version(version=None):
  4
+    """Derives a PEP386-compliant version number from VERSION."""
  5
+    if version is None:
  6
+        version = VERSION
  7
+    assert len(version) == 5
  8
+    assert version[3] in ('alpha', 'beta', 'rc', 'final')
17 9
 
18  
-def get_distutils_version():
19  
-    # Distutils expects a version number formatted as major.minor[.patch][sub]
20  
-    parts = 5
21  
-    if VERSION[3] == 'final':
22  
-        parts = 3
23  
-        if VERSION[2] == 0:
24  
-            parts = 2
25  
-    version = VERSION[:parts]
26  
-    version = [str(x)[0] for x in version]      # ['1', '4', '0', 'a', '1']
27  
-    if parts > 2:
28  
-        version[2:] = [''.join(version[2:])]    # ['1', '4', '0a1']
29  
-    version = '.'.join(version)                 # '1.4.0a1'
30  
-    return version
  10
+    # Now build the two parts of the version number:
  11
+    # main = X.Y[.Z]
  12
+    # sub = .devN - for pre-alpha releases
  13
+    #     | {a|b|c}N - for alpha, beta and rc releases
  14
+
  15
+    parts = 2 if version[2] == 0 else 3
  16
+    main = '.'.join(str(x) for x in version[:parts])
  17
+
  18
+    sub = ''
  19
+    if version[3] == 'alpha' and version[4] == 0:
  20
+        # At the toplevel, this would cause an import loop.
  21
+        from django.utils.version import get_svn_revision
  22
+        svn_revision = get_svn_revision()[4:]
  23
+        if svn_revision != 'unknown':
  24
+            sub = '.dev%s' % svn_revision
  25
+
  26
+    elif version[3] != 'final':
  27
+        mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
  28
+        sub = mapping[version[3]] + str(version[4])
  29
+
  30
+    return main + sub
3  django/core/management/__init__.py
@@ -4,12 +4,11 @@
4 4
 import imp
5 5
 import warnings
6 6
 
7  
-import django
8 7
 from django.core.management.base import BaseCommand, CommandError, handle_default_options
9 8
 from django.utils.importlib import import_module
10 9
 
11 10
 # For backwards compatibility: get_version() used to be in this module.
12  
-get_version = django.get_version
  11
+from django import get_version
13 12
 
14 13
 # A cache of loaded commands, so that call_command
15 14
 # doesn't have to reload every time it's called.
2  setup.py
@@ -66,7 +66,7 @@ def fullsplit(path, result=None):
66 66
         file_info[0] = '\\PURELIB\\%s' % file_info[0]
67 67
 
68 68
 # Dynamically calculate the version based on django.VERSION.
69  
-version = __import__('django').get_distutils_version()
  69
+version = __import__('django').get_version()
70 70
 
71 71
 setup(
72 72
     name = "Django",
3  tests/regressiontests/admin_scripts/tests.py
@@ -1177,8 +1177,7 @@ def test_version(self):
1177 1177
         args = ['--version']
1178 1178
         out, err = self.run_manage(args)
1179 1179
         self.assertNoOutput(err)
1180  
-        # Only check the first part of the version number
1181  
-        self.assertOutput(out, get_version().split('-')[0])
  1180
+        self.assertOutput(out, get_version())
1182 1181
 
1183 1182
     def test_help(self):
1184 1183
         "--help is handled as a special case"
0  tests/regressiontests/version/__init__.py
No changes.
0  tests/regressiontests/version/models.py
No changes.
26  tests/regressiontests/version/tests.py
... ...
@@ -0,0 +1,26 @@
  1
+import re
  2
+
  3
+from django import get_version
  4
+from django.utils.unittest import TestCase
  5
+
  6
+class VersionTests(TestCase):
  7
+
  8
+    def test_development(self):
  9
+        ver_tuple = (1, 4, 0, 'alpha', 0)
  10
+        # This will return a different result when it's run within or outside
  11
+        # of a SVN checkout: 1.4.devNNNNN or 1.4.
  12
+        ver_string = get_version(ver_tuple)
  13
+        self.assertRegexpMatches(ver_string, r'1\.4(\.dev\d+)?')
  14
+
  15
+    def test_releases(self):
  16
+        tuples_to_strings = (
  17
+            ((1, 4, 0, 'alpha', 1), '1.4a1'),
  18
+            ((1, 4, 0, 'beta', 1), '1.4b1'),
  19
+            ((1, 4, 0, 'rc', 1), '1.4c1'),
  20
+            ((1, 4, 0, 'final', 0), '1.4'),
  21
+            ((1, 4, 1, 'rc', 2), '1.4.1c2'),
  22
+            ((1, 4, 1, 'final', 0), '1.4.1'),
  23
+        )
  24
+        for ver_tuple, ver_string in tuples_to_strings:
  25
+            self.assertEqual(get_version(ver_tuple), ver_string)
  26
+

0 notes on commit 40f0ecc

Please sign in to comment.
Something went wrong with that request. Please try again.