LGTM. Thanks, Jose
On Wed, Oct 02, 2013 at 02:33:01PM +0200, Klaus Aehlig wrote: > The new 'gnt-cluster upgrade' command will get a Ganeti version as > argument. So provide a function able to parse it. > > Signed-off-by: Klaus Aehlig <[email protected]> > --- > Makefile.am | 1 + > lib/utils/__init__.py | 1 + > lib/utils/version.py | 27 ++++++++++++++++++++- > test/py/ganeti.utils.version_unittest.py | 40 > ++++++++++++++++++++++++++++++++ > 4 files changed, 68 insertions(+), 1 deletion(-) > create mode 100755 test/py/ganeti.utils.version_unittest.py > > diff --git a/Makefile.am b/Makefile.am > index 685e7eb..b2296f0 100644 > --- a/Makefile.am > +++ b/Makefile.am > @@ -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 \ > diff --git a/lib/utils/__init__.py b/lib/utils/__init__.py > index 96655a0..4e5b3bd 100644 > --- a/lib/utils/__init__.py > +++ b/lib/utils/__init__.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 * > > > diff --git a/lib/utils/version.py b/lib/utils/version.py > index 26aa36c..20381b7 100644 > --- a/lib/utils/version.py > +++ b/lib/utils/version.py > @@ -1,4 +1,4 @@ > -#!/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 > diff --git a/test/py/ganeti.utils.version_unittest.py > b/test/py/ganeti.utils.version_unittest.py > new file mode 100755 > index 0000000..7df0c62 > --- /dev/null > +++ b/test/py/ganeti.utils.version_unittest.py > @@ -0,0 +1,40 @@ > +#!/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() > -- > 1.8.4 > -- Jose Antonio Lopes Ganeti Engineering Google Germany GmbH Dienerstr. 12, 80331, München Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschäftsführer: Graham Law, Christine Elizabeth Flores Steuernummer: 48/725/00206 Umsatzsteueridentifikationsnummer: DE813741370
