2010-06-23 22:04:16 -07:00
|
|
|
# Copyright 2010 United States Government as represented by the
|
2010-06-23 23:15:06 -07:00
|
|
|
# Administrator of the National Aeronautics and Space Administration.
|
2011-02-23 12:05:49 -08:00
|
|
|
# Copyright 2011 Justin Santa Barbara
|
2010-06-23 22:04:16 -07:00
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2010-05-27 23:05:26 -07:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
2010-06-23 22:04:16 -07:00
|
|
|
# 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.
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2011-04-20 12:08:22 -07:00
|
|
|
"""Utilities and helper functions."""
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2015-02-04 15:59:34 +01:00
|
|
|
import functools
|
2015-02-02 16:24:14 -05:00
|
|
|
import logging
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2013-10-25 16:03:53 +08:00
|
|
|
import six
|
2013-02-15 22:30:16 +00:00
|
|
|
|
2015-02-02 16:24:14 -05:00
|
|
|
from oslo_versionedobjects._i18n import _
|
|
|
|
from oslo_versionedobjects import exception
|
2011-12-10 14:01:17 -08:00
|
|
|
|
2012-12-12 07:14:12 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2013-07-15 10:53:10 +05:30
|
|
|
|
|
|
|
def convert_version_to_int(version):
|
2013-10-07 14:47:42 +05:30
|
|
|
try:
|
2014-01-10 11:43:37 +08:00
|
|
|
if isinstance(version, six.string_types):
|
2013-10-07 14:47:42 +05:30
|
|
|
version = convert_version_to_tuple(version)
|
2014-01-10 11:43:37 +08:00
|
|
|
if isinstance(version, tuple):
|
2015-02-04 15:59:34 +01:00
|
|
|
return functools.reduce(lambda x, y: (x * 1000) + y, version)
|
2013-10-07 14:47:42 +05:30
|
|
|
except Exception:
|
2015-07-13 16:19:57 -07:00
|
|
|
msg = _("Provided version %s is invalid.") % version
|
2015-02-03 11:52:41 +01:00
|
|
|
raise exception.VersionedObjectsException(msg)
|
2013-10-07 14:47:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
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))
|
2015-02-04 15:59:34 +01:00
|
|
|
version_int = version_int // factor
|
2013-10-07 14:47:42 +05:30
|
|
|
|
2015-02-04 15:59:34 +01:00
|
|
|
return '.'.join(map(str, version_numbers))
|
2013-10-07 14:47:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
def convert_version_to_tuple(version_str):
|
|
|
|
return tuple(int(part) for part in version_str.split('.'))
|