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.
This commit is contained in:
mprahl
2018-10-11 14:48:02 -04:00
parent 8ef445c0b2
commit 4f9fffff3b
3 changed files with 43 additions and 24 deletions

View File

@@ -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 (("<ModuleBuild %s, id=%d, stream=%s, version=%s, state %r,"
" batch %r, state_reason %r>")

View File

@@ -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))

View File

@@ -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: