Revision 1b45f4e5

b/lib/constants.py
29 29
OS_API_VERSION = 5
30 30
EXPORT_VERSION = 0
31 31

  
32

  
32 33
# Format for CONFIG_VERSION:
33 34
#   01 03 0123 = 01030123
34 35
#   ^^ ^^ ^^^^
......
37 38
#   + Major version
38 39
#
39 40
# It stored as an integer. Make sure not to write an octal number.
40
#
41

  
42
# BuildVersion and SplitVersion must be in here because we can't import other
43
# modules. The cfgupgrade tool must be able to read and write version numbers
44
# and thus requires these functions. To avoid code duplication, they're kept in
45
# here.
46

  
47
def BuildVersion(major, minor, revision):
48
  """Calculates int version number from major, minor and revision numbers.
49

  
50
  Returns: int representing version number
51

  
52
  """
53
  assert isinstance(major, int)
54
  assert isinstance(minor, int)
55
  assert isinstance(revision, int)
56
  return (1000000 * major +
57
            10000 * minor +
58
                1 * revision)
59

  
60

  
61
def SplitVersion(version):
62
  """Splits version number stored in an int.
63

  
64
  Returns: tuple; (major, minor, revision)
65

  
66
  """
67
  assert isinstance(version, int)
68

  
69
  (major, remainder) = divmod(version, 1000000)
70
  (minor, revision) = divmod(remainder, 10000)
71

  
72
  return (major, minor, revision)
73

  
74

  
41 75
CONFIG_MAJOR = int(_autoconf.VERSION_MAJOR)
42 76
CONFIG_MINOR = int(_autoconf.VERSION_MINOR)
43 77
CONFIG_REVISION = 0
44
CONFIG_VERSION = (
45
  1000000 * CONFIG_MAJOR +
46
    10000 * CONFIG_MINOR +
47
        1 * CONFIG_REVISION)
78
CONFIG_VERSION = BuildVersion(CONFIG_MAJOR, CONFIG_MINOR, CONFIG_REVISION)
48 79

  
49 80
# file paths
50 81
DATA_DIR = _autoconf.LOCALSTATEDIR + "/lib/ganeti"
b/test/ganeti.constants_unittest.py
40 40
    self.failUnless(constants.CONFIG_VERSION >= 0 and
41 41
                    constants.CONFIG_VERSION <= 99999999)
42 42

  
43
    self.failUnless(constants.BuildVersion(0, 0, 0) == 0)
44
    self.failUnless(constants.BuildVersion(10, 10, 1010) == 10101010)
45
    self.failUnless(constants.BuildVersion(12, 34, 5678) == 12345678)
46
    self.failUnless(constants.BuildVersion(99, 99, 9999) == 99999999)
47

  
48
    self.failUnless(constants.SplitVersion(00000000) == (0, 0, 0))
49
    self.failUnless(constants.SplitVersion(10101010) == (10, 10, 1010))
50
    self.failUnless(constants.SplitVersion(12345678) == (12, 34, 5678))
51
    self.failUnless(constants.SplitVersion(99999999) == (99, 99, 9999))
52
    self.failUnless(constants.SplitVersion(constants.CONFIG_VERSION) ==
53
                    (constants.CONFIG_MAJOR, constants.CONFIG_MINOR,
54
                     constants.CONFIG_REVISION))
55

  
43 56

  
44 57
if __name__ == '__main__':
45 58
  unittest.main()

Also available in: Unified diff