Skip to content

Commit ccaa425

Browse files
handle dashes in version parsing (#571)
* fix parsing of versions with dashes * ensure we have 4 version components * pylint fixes * fix more version cases
1 parent e5bc154 commit ccaa425

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/inputstreamhelper/utils.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
from __future__ import absolute_import, division, unicode_literals
66
import os
7+
import re
78
from time import time
89
from socket import timeout
910
from ssl import SSLError
1011
import struct
1112
from typing import NamedTuple
1213
from functools import total_ordering
1314

14-
1515
try: # Python 3
1616
from urllib.error import HTTPError, URLError
1717
from urllib.request import Request, urlopen
@@ -364,17 +364,20 @@ def remove_tree(path):
364364

365365

366366
def parse_version(vstring):
367-
"""Parse a version string and return a comparable version object"""
368-
vstring = vstring.strip('v')
369-
vstrings = vstring.split('.')
370-
try:
371-
vnums = tuple(int(v) for v in vstrings)
372-
except ValueError:
373-
log(3, f"Version string {vstring} can't be interpreted! Contains non-numerics.")
374-
return Version(0, 0, 0, 0)
375-
376-
if len(vnums) > 4:
377-
log(3, f"Version string {vstring} can't be interpreted! Too long.")
378-
return Version(0, 0, 0, 0)
367+
"""Parse a version string and return a comparable version object, properly handling non-numeric prefixes."""
368+
vstring = vstring.strip('v').lower()
369+
parts = re.split(r'\.', vstring) # split on periods first
370+
371+
vnums = []
372+
for part in parts:
373+
# extract numeric part, ignoring non-numeric prefixes
374+
numeric_part = re.search(r'\d+', part)
375+
if numeric_part:
376+
vnums.append(int(numeric_part.group()))
377+
else:
378+
vnums.append(0) # default to 0 if no numeric part found
379+
380+
# ensure the version tuple always has 4 components
381+
vnums = (vnums + [0] * 4)[:4]
379382

380383
return Version(*vnums)

0 commit comments

Comments
 (0)