Commit 1eda3dd3 authored by Klaus Aehlig's avatar Klaus Aehlig

Add utility function to parse version strings

The new 'gnt-cluster upgrade' command will get a Ganeti version as
argument. So provide a function able to parse it.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarJose Lopes <jabolopes@google.com>
parent cbb533f4
......@@ -1456,6 +1456,7 @@ python_tests = \
test/py/ganeti.utils.retry_unittest.py \
test/py/ganeti.utils.storage_unittest.py \
test/py/ganeti.utils.text_unittest.py \
test/py/ganeti.utils.version_unittest.py \
test/py/ganeti.utils.wrapper_unittest.py \
test/py/ganeti.utils.x509_unittest.py \
test/py/ganeti.utils_unittest.py \
......
......@@ -56,6 +56,7 @@ from ganeti.utils.retry import *
from ganeti.utils.storage import *
from ganeti.utils.text import *
from ganeti.utils.wrapper import *
from ganeti.utils.version import *
from ganeti.utils.x509 import *
......
#!/usr/bin/python
#
#
# Copyright (C) 2013 Google Inc.
......@@ -21,6 +21,10 @@
"""Version utilities."""
import re
_FULL_VERSION_RE = re.compile(r"(\d+)\.(\d+)\.(\d+)")
_SHORT_VERSION_RE = re.compile(r"(\d+)\.(\d+)")
# Format for CONFIG_VERSION:
# 01 03 0123 = 01030123
......@@ -36,6 +40,7 @@
# and thus requires these functions. To avoid code duplication, they're kept in
# here.
def BuildVersion(major, minor, revision):
"""Calculates int version number from major, minor and revision numbers.
......@@ -62,3 +67,23 @@ def SplitVersion(version):
(minor, revision) = divmod(remainder, 10000)
return (major, minor, revision)
def ParseVersion(versionstring):
"""Parses a version string.
@param versionstring: the version string to parse
@type versionstring: string
@rtype: tuple or None
@return: (major, minor, revision) if parsable, None otherwise.
"""
m = _FULL_VERSION_RE.match(versionstring)
if m is not None:
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
m = _SHORT_VERSION_RE.match(versionstring)
if m is not None:
return (int(m.group(1)), int(m.group(2)), 0)
return None
#!/usr/bin/python
#
# Copyright (C) 2013 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Script for unittesting the version utility functions"""
import unittest
from ganeti.utils import version
import testutils
class ParseVersionTest(unittest.TestCase):
def testParseVersion(self):
self.assertEquals(version.ParseVersion("2.10"), (2, 10, 0))
self.assertEquals(version.ParseVersion("2.10.1"), (2, 10, 1))
self.assertEquals(version.ParseVersion("2.10.1~beta2"), (2, 10, 1))
self.assertEquals(version.ParseVersion("2"), None)
self.assertEquals(version.ParseVersion("pink bunny"), None)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment