From 4f9fffff3b4e5c06370ded9c2ad8eeb904b1ccaf Mon Sep 17 00:00:00 2001 From: mprahl Date: Thu, 11 Oct 2018 14:48:02 -0400 Subject: [PATCH] Move some of the logic in `get_prefixed_version` to a ModuleBuild static method The logic used to parse the stream version will need to be reused elsewhere to support the filtering and ordering of a base module stream version. --- module_build_service/models.py | 36 ++++++++++++++++++++++++++++ module_build_service/utils/submit.py | 26 ++------------------ tests/test_models/test_models.py | 5 ++++ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/module_build_service/models.py b/module_build_service/models.py index 281738c5..ae40b4ea 100644 --- a/module_build_service/models.py +++ b/module_build_service/models.py @@ -620,6 +620,42 @@ class ModuleBuild(MBSBase): return ModuleBuildTrace.query.filter_by( module_id=module_id).order_by(ModuleBuildTrace.state_time).all() + @staticmethod + def get_stream_version(stream): + """ + Parse the supplied stream to find its version. + + This will parse a stream such as "f27" and return 270000. Another example would be a stream + of "f27.0.1" and return 270001. + :param str stream: the module stream + :return: a stream version represented as an integer + :rtype: int or None if the stream doesn't have a valid version + """ + # The platform version (e.g. prefix1.2.0 => 010200) + version = '' + for char in stream: + # See if the current character is an integer, signifying the version has started + if char.isdigit(): + version += char + # If version isn't set, then a digit hasn't been encountered + elif version: + # If the character is a period and the version is set, then + # the loop is still processing the version part of the stream + if char == '.': + version += '.' + # If the version is set and the character is not a period or + # digit, then the remainder of the stream is a suffix like "-beta" + else: + break + + # Remove the periods and pad the numbers if necessary + version = ''.join([section.zfill(2) for section in version.split('.')]) + + if version: + # Since the version must be stored as a number, we convert the string back to + # an integer which consequently drops the leading zero if there is one + return int(version) + def __repr__(self): return (("") diff --git a/module_build_service/utils/submit.py b/module_build_service/utils/submit.py index 28bfce1a..729758d6 100644 --- a/module_build_service/utils/submit.py +++ b/module_build_service/utils/submit.py @@ -239,36 +239,14 @@ def get_prefixed_version(mmd): return version # The platform version (e.g. prefix1.2.0 => 010200) - version_prefix = '' - for char in base_module_stream: - try: - # See if the current character is an integer, signifying the version - # has started - int(char) - version_prefix += char - except ValueError: - # If version_prefix isn't set, then a digit hasn't been encountered - if version_prefix: - # If the character is a period and the version_prefix is set, then - # the loop is still processing the version part of the stream - if char == '.': - version_prefix += '.' - # If the version_prefix is set and the character is not a period or - # digit, then the remainder of the stream is a suffix like "-beta" - else: - break - - # Remove the periods and pad the numbers if necessary - version_prefix = ''.join([section.zfill(2) for section in version_prefix.split('.')]) + version_prefix = models.ModuleBuild.get_stream_version(base_module_stream) if not version_prefix: log.warning('The "{0}" stream "{1}" couldn\'t be used to prefix the module\'s ' 'version'.format(base_module, base_module_stream)) return version - # Since the version must be stored as a number, we convert the string back to - # an integer which consequently drops the leading zero if there is one - new_version = int(version_prefix + str(version)) + new_version = int(str(version_prefix) + str(version)) if new_version > GLib.MAXUINT64: log.warning('The "{0}" stream "{1}" caused the module\'s version prefix to be ' 'too long'.format(base_module, base_module_stream)) diff --git a/tests/test_models/test_models.py b/tests/test_models/test_models.py index eb3dd17d..f6f764c8 100644 --- a/tests/test_models/test_models.py +++ b/tests/test_models/test_models.py @@ -93,6 +93,11 @@ class TestModels: build_one = ModuleBuild.query.get(2) assert build_one.siblings == [3, 4] + def test_get_stream_version(self): + """Test the ModuleBuild.get_stream_version method when right_pad is True.""" + assert ModuleBuild.get_stream_version('f27') == 270000 + assert ModuleBuild.get_stream_version('f27.02.30') == 270230 + class TestModelsGetStreamsContexts: