From ccaa425ca3286f55c5e225e8206f4a4a95e823a9 Mon Sep 17 00:00:00 2001 From: Emil Svensson Date: Mon, 23 Sep 2024 19:59:43 +0200 Subject: [PATCH] handle dashes in version parsing (#571) * fix parsing of versions with dashes * ensure we have 4 version components * pylint fixes * fix more version cases --- lib/inputstreamhelper/utils.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/inputstreamhelper/utils.py b/lib/inputstreamhelper/utils.py index 55752cfb..31c4a753 100644 --- a/lib/inputstreamhelper/utils.py +++ b/lib/inputstreamhelper/utils.py @@ -4,6 +4,7 @@ from __future__ import absolute_import, division, unicode_literals import os +import re from time import time from socket import timeout from ssl import SSLError @@ -11,7 +12,6 @@ from typing import NamedTuple from functools import total_ordering - try: # Python 3 from urllib.error import HTTPError, URLError from urllib.request import Request, urlopen @@ -364,17 +364,20 @@ def remove_tree(path): def parse_version(vstring): - """Parse a version string and return a comparable version object""" - vstring = vstring.strip('v') - vstrings = vstring.split('.') - try: - vnums = tuple(int(v) for v in vstrings) - except ValueError: - log(3, f"Version string {vstring} can't be interpreted! Contains non-numerics.") - return Version(0, 0, 0, 0) - - if len(vnums) > 4: - log(3, f"Version string {vstring} can't be interpreted! Too long.") - return Version(0, 0, 0, 0) + """Parse a version string and return a comparable version object, properly handling non-numeric prefixes.""" + vstring = vstring.strip('v').lower() + parts = re.split(r'\.', vstring) # split on periods first + + vnums = [] + for part in parts: + # extract numeric part, ignoring non-numeric prefixes + numeric_part = re.search(r'\d+', part) + if numeric_part: + vnums.append(int(numeric_part.group())) + else: + vnums.append(0) # default to 0 if no numeric part found + + # ensure the version tuple always has 4 components + vnums = (vnums + [0] * 4)[:4] return Version(*vnums)