From efb89bfeb14e7e9148eddb2770793b43e8605421 Mon Sep 17 00:00:00 2001 From: James Antill Date: Thu, 6 Nov 2025 16:46:28 -0500 Subject: [PATCH] updates+uptimes: Use hacky verscmp(). Signed-off-by: James Antill --- files/scripts/updates-uptime-cmd.py | 44 +++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/files/scripts/updates-uptime-cmd.py b/files/scripts/updates-uptime-cmd.py index 7398e70331..9584397731 100755 --- a/files/scripts/updates-uptime-cmd.py +++ b/files/scripts/updates-uptime-cmd.py @@ -14,6 +14,7 @@ import sys import argparse import fnmatch import locale +import re import shutil import time @@ -49,6 +50,9 @@ conf_term_keyword = 'underline' conf_term_time = 'underline' conf_term_title = 'italic' +# Use version cmp() for hostnames. +conf_verscmp = True + # Use _ instead of , for number seperator. conf_num_sep_ = False @@ -185,6 +189,41 @@ def _user_conf_line(line): print(" Error: Configuration ", key,'bad op', file=sys.stderr) return +# This isn't fast but it's SMALL: +# Sort as: ABC1, ABC2, ABC10b, ... +def verscmp(x, y): + if not conf_verscmp: + if x == y: + return 0 + if x > y: + return 1 + return -1 + + xc = re.split(r'(\d+|\W+)', x) + yc = re.split(r'(\d+|\W+)', y) + while xc and yc: + if xc[0] == yc[0]: + xc.pop(0) + yc.pop(0) + continue + + if xc[0].isnumeric(): + if not yc[0].isnumeric(): + return 1 + nx = int(xc[0]) + ny = int(yc[0]) + if nx != ny: # don't make 0 == 00 ... we aren't rpm. + return nx - ny + elif yc[0].isnumeric(): + return -1 + + # Neither numeric, but also 0 == 00 BS + if xc[0] > yc[0]: + return 1 + return -1 + return len(xc) - len(yc) + + # Have nice "plain" numbers... def _ui_int(num): if conf_num_sep_: @@ -422,9 +461,10 @@ class Host(): return True def __gt__(self, other): - if self.name > other.name: + ret = verscmp(self.name, other.name) + if ret > 0: return True - if self.name != other.name: + if ret < 0: return False if self.rpms > other.rpms: