Integrate tool to parse versions
Change-Id: I74825cf0c878d8a65e61ebe3929309a5b4aefe95
This commit is contained in:
parent
756b739a23
commit
eb331a722a
|
@ -33,6 +33,7 @@ from tobiko.common import _skip
|
|||
from tobiko.common import _testcase
|
||||
from tobiko.common import _time
|
||||
from tobiko.common import _utils
|
||||
from tobiko.common import _version
|
||||
from tobiko.common import _yaml
|
||||
from tobiko.common import _background
|
||||
|
||||
|
@ -149,6 +150,13 @@ true_seconds = _time.true_seconds
|
|||
|
||||
get_short_hostname = _utils.get_short_hostname
|
||||
|
||||
InvalidVersion = _version.InvalidVersion
|
||||
VersionType = _version.VersionType
|
||||
Version = _version.Version
|
||||
match_version = _version.match_version
|
||||
parse_version = _version.parse_version
|
||||
get_version = _version.get_version
|
||||
|
||||
dump_yaml = _yaml.dump_yaml
|
||||
load_yaml = _yaml.load_yaml
|
||||
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
# Copyright 2022 Red Hat
|
||||
#
|
||||
# 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 __future__ import absolute_import
|
||||
|
||||
import re
|
||||
import typing
|
||||
|
||||
from packaging import version as _version
|
||||
|
||||
from tobiko import _exception
|
||||
|
||||
|
||||
VERSION_PATTERN = re.compile(r"[0-9]*\.[0-9]*\.[0-9]*")
|
||||
|
||||
|
||||
class InvalidVersion(_exception.TobikoException):
|
||||
message = "invalid version: {text}"
|
||||
|
||||
|
||||
VERSION_CLASSES = _version.LegacyVersion, _version.Version
|
||||
Version = typing.Union[_version.LegacyVersion,
|
||||
_version.Version]
|
||||
VersionType = typing.Union[Version, str]
|
||||
|
||||
|
||||
def get_version(obj: VersionType):
|
||||
if isinstance(obj, VERSION_CLASSES):
|
||||
return obj
|
||||
elif isinstance(obj, str):
|
||||
return parse_version(obj)
|
||||
else:
|
||||
raise TypeError(f'Cannot get version from object {obj!r}')
|
||||
|
||||
|
||||
def parse_version(text: str) -> VersionType:
|
||||
match = VERSION_PATTERN.search(text.strip())
|
||||
if match is None:
|
||||
raise InvalidVersion(text=text)
|
||||
return _version.parse(match.group())
|
||||
|
||||
|
||||
def match_version(actual: VersionType,
|
||||
min_version: VersionType = None,
|
||||
max_version: VersionType = None) -> bool:
|
||||
actual = get_version(actual)
|
||||
if min_version is not None:
|
||||
min_version = get_version(min_version)
|
||||
if actual < min_version:
|
||||
return False
|
||||
if max_version is not None:
|
||||
max_version = get_version(max_version)
|
||||
if actual >= max_version:
|
||||
return False
|
||||
return True
|
|
@ -0,0 +1,65 @@
|
|||
# Copyright 2022 Red Hat
|
||||
#
|
||||
# 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 __future__ import absolute_import
|
||||
|
||||
import unittest
|
||||
|
||||
from packaging import version
|
||||
|
||||
import tobiko
|
||||
|
||||
|
||||
class TestVersion(unittest.TestCase):
|
||||
|
||||
def test_parse_version(self):
|
||||
self.assertEqual(version.parse('1.2.3'),
|
||||
tobiko.parse_version('1.2.3'))
|
||||
self.assertEqual(version.parse('3.2.1'),
|
||||
tobiko.parse_version(' a b c 10 3.2.1 25.2.1'))
|
||||
|
||||
def test_parse_version_with_invalid(self):
|
||||
with self.assertRaises(tobiko.InvalidVersion):
|
||||
tobiko.parse_version('1.2')
|
||||
|
||||
def test_get_version_with_srt(self):
|
||||
self.assertEqual(version.parse('3.2.1'),
|
||||
tobiko.get_version(' a b c 10 3.2.1 25.2.1'))
|
||||
|
||||
def test_get_version_with_version(self):
|
||||
reference = version.parse('3.2.1')
|
||||
self.assertIs(reference,
|
||||
tobiko.get_version(reference))
|
||||
|
||||
def test_match_version_with_str(self):
|
||||
self.assertTrue(tobiko.match_version('1.2.3'))
|
||||
|
||||
def test_match_version_with_version(self):
|
||||
reference = version.parse('3.2.1')
|
||||
self.assertTrue(tobiko.match_version(reference))
|
||||
|
||||
def test_match_version_min_version(self):
|
||||
self.assertTrue(tobiko.match_version('1.0.0',
|
||||
min_version='0.9.9'))
|
||||
self.assertTrue(tobiko.match_version('1.0.0',
|
||||
min_version='1.0.0'))
|
||||
self.assertFalse(tobiko.match_version('1.0.0',
|
||||
min_version='1.0.1'))
|
||||
|
||||
def test_match_version_max_version(self):
|
||||
self.assertFalse(tobiko.match_version('1.0.0',
|
||||
max_version='0.9.9'))
|
||||
self.assertFalse(tobiko.match_version('1.0.0',
|
||||
max_version='1.0.0'))
|
||||
self.assertTrue(tobiko.match_version('1.0.0',
|
||||
max_version='1.0.1'))
|
Loading…
Reference in New Issue