Use version convert methods from oslo_utils.versionutils

oslo_utils provide version convert methods in versionutis[1],
these methods work well on Python 2.7 and Python 3.4,
so don't need maintain them.

[1]https://github.com/openstack/oslo.utils/blob/master/oslo_utils/versionutils.py

Change-Id: Ifc4b7f5c7e42bce9f5bbf8a961fd32506cca98eb
This commit is contained in:
ChangBo Guo(gcb) 2015-11-30 22:25:49 +08:00
parent cad06aa85f
commit a9752e281a
4 changed files with 9 additions and 83 deletions

View File

@ -17,17 +17,6 @@
"""Utilities and helper functions."""
import functools
import logging
import six
from oslo_versionedobjects._i18n import _
from oslo_versionedobjects import exception
LOG = logging.getLogger(__name__)
# ISO 8601 extended time format with microseconds
_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
@ -38,29 +27,3 @@ def isotime(at):
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
st += ('Z' if tz == 'UTC' else tz)
return st
def convert_version_to_int(version):
try:
if isinstance(version, six.string_types):
version = convert_version_to_tuple(version)
if isinstance(version, tuple):
return functools.reduce(lambda x, y: (x * 1000) + y, version)
except Exception:
msg = _("Provided version %s is invalid.") % version
raise exception.VersionedObjectsException(msg)
def convert_version_to_str(version_int):
version_numbers = []
factor = 1000
while version_int != 0:
version_number = version_int - (version_int // factor * factor)
version_numbers.insert(0, str(version_number))
version_int = version_int // factor
return '.'.join(map(str, version_numbers))
def convert_version_to_tuple(version_str):
return tuple(int(part) for part in version_str.split('.'))

View File

@ -23,10 +23,10 @@ import warnings
import oslo_messaging as messaging
from oslo_utils import encodeutils
from oslo_utils import excutils
from oslo_utils import versionutils as vutils
import six
from oslo_versionedobjects._i18n import _, _LE
from oslo_versionedobjects import _utils as utils
from oslo_versionedobjects import exception
from oslo_versionedobjects import fields as obj_fields
from oslo_versionedobjects.openstack.common import versionutils
@ -527,8 +527,8 @@ class VersionedObject(object):
"""
if target_version is None:
target_version = self.VERSION
if (utils.convert_version_to_tuple(target_version) >
utils.convert_version_to_tuple(self.VERSION)):
if (vutils.convert_version_to_tuple(target_version) >
vutils.convert_version_to_tuple(self.VERSION)):
raise exception.InvalidTargetVersion(version=target_version)
primitive = dict()
for name, field in self.fields.items():
@ -1114,10 +1114,10 @@ def _get_subobject_version(tgt_version, relationships, backport_func):
version
:returns: The version we need to convert the subobject to
"""
tgt = utils.convert_version_to_tuple(tgt_version)
tgt = vutils.convert_version_to_tuple(tgt_version)
for index, versions in enumerate(relationships):
parent, child = versions
parent = utils.convert_version_to_tuple(parent)
parent = vutils.convert_version_to_tuple(parent)
if tgt < parent:
if index == 0:
# We're backporting to a version of the parent that did

View File

@ -25,10 +25,10 @@ import hashlib
import inspect
import logging
import mock
from oslo_utils import versionutils as vutils
import six
import fixtures
from oslo_versionedobjects import _utils as utils
from oslo_versionedobjects import base
from oslo_versionedobjects import fields
@ -238,7 +238,7 @@ class ObjectVersionChecker(object):
return expected, actual
def _test_object_compatibility(self, obj_class):
version = utils.convert_version_to_tuple(obj_class.VERSION)
version = vutils.convert_version_to_tuple(obj_class.VERSION)
for n in range(version[1] + 1):
test_version = '%d.%d' % (version[0], n)
LOG.info('testing obj: %s version: %s' %
@ -261,8 +261,8 @@ class ObjectVersionChecker(object):
last_my_version = (0, 0)
last_child_version = (0, 0)
for my_version, child_version in versions:
_my_version = utils.convert_version_to_tuple(my_version)
_ch_version = utils.convert_version_to_tuple(child_version)
_my_version = vutils.convert_version_to_tuple(my_version)
_ch_version = vutils.convert_version_to_tuple(child_version)
assert (last_my_version < _my_version
and last_child_version <= _ch_version), \
('Object %s relationship '

View File

@ -1,37 +0,0 @@
# Copyright 2011 Justin Santa Barbara
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from oslo_versionedobjects import _utils as utils
from oslo_versionedobjects import exception
from oslo_versionedobjects import test
CONF = cfg.CONF
class VersionTestCase(test.TestCase):
def test_convert_version_to_int(self):
self.assertEqual(utils.convert_version_to_int('6.2.0'), 6002000)
self.assertEqual(utils.convert_version_to_int((6, 4, 3)), 6004003)
self.assertEqual(utils.convert_version_to_int((5, )), 5)
self.assertRaises(exception.VersionedObjectsException,
utils.convert_version_to_int, '5a.6b')
def test_convert_version_to_string(self):
self.assertEqual(utils.convert_version_to_str(6007000), '6.7.0')
self.assertEqual(utils.convert_version_to_str(4), '4')
def test_convert_version_to_tuple(self):
self.assertEqual(utils.convert_version_to_tuple('6.7.0'), (6, 7, 0))