1
0
Fork 0
forked from infra/ansible

updates+uptime: Fix nat sorting of proxy40. vs. proxy101.

Signed-off-by: James Antill <james@and.org>
This commit is contained in:
James Antill 2026-03-26 14:51:28 -04:00
commit 4d31cfdd39

View file

@ -16,6 +16,7 @@ import fnmatch
import shutil
import subprocess
import time
from functools import total_ordering
# Use utf8 prefixes in diff, these need to be a "normal" width 1 character
conf_utf8 = True
@ -193,9 +194,9 @@ def natcmp(x, y):
https://en.wikipedia.org/wiki/Natural_sort_order
Aka. vercmp() """
def _cmp_xy_mix(): # One is a digit, the other isn't.
def _cmp_xy_mix(val): # One is a digit, the other isn't.
if inum is not None: # 0/1 vs. x/.
return 1
return val
if x[i] > y[i]:
return 1
else:
@ -205,9 +206,9 @@ def natcmp(x, y):
check_zeros = False
for i in range(min(len(x), len(y))):
if x[i] in "0123456789" and y[i] not in "0123456789":
return _cmp_xy_mix()
return _cmp_xy_mix(1)
if x[i] not in "0123456789" and y[i] in "0123456789":
return _cmp_xy_mix()
return _cmp_xy_mix(-1)
if x[i] in "0123456789": # Both are digits...
if inum is None:
@ -263,6 +264,7 @@ def natcmp(x, y):
return inum
@total_ordering
class NatCmp():
__slots__ = ['s',]
def __init__(self, s):
@ -467,6 +469,7 @@ def _wild_eq(s1, s2):
_max_len_osnm = 0 # osname_small
_max_len_osvr = 0 # osvers ... upto the first '.'
@total_ordering
class Host():
""" Class for holding the Host data from a line in the files. """
@ -688,7 +691,7 @@ def line2data(line):
return Host(locals())
def lines2datas(lines):
return (line2data(line) for line in lines)
return list(sorted(line2data(line) for line in lines))
# Filter datas using name as a filename wildcard match.
def filter_name_datas(datas, names):
@ -799,7 +802,7 @@ def _maybe_dynamic_uptime(data):
mtime = os.path.getmtime(fname)
since = int(time.time()) - int(mtime)
data = list(sorted(data))
data = data.copy()
for d1 in data:
d1.uptime += since
return data
@ -944,12 +947,12 @@ def _diffstats(data1, data2):
d1 = data1[0]
d2 = data2[0]
if d1.name < d2.name:
if natcmp(d1.name, d2.name) < 0:
udel -= d1.rpms
data1.pop(0)
continue
if d1.name > d2.name:
if natcmp(d1.name, d2.name) > 0:
uadd += d2.rpms
data2.pop(0)
boot += 1
@ -1204,7 +1207,7 @@ def _hist_lengths(hosts=None):
# Whatever, it's less memory than holding all history at once if you want
# to enable it..
for backup in reversed(backups):
data = list(sorted(lines2datas(bfname2lines(backup))))
data = lines2datas(bfname2lines(backup)))
data = list(filter_name_datas(data, hosts))
updates = _ui_int(sum(d.rpms for d in data))
hl = max(hl, len(_ui_int(len(data))))
@ -1233,7 +1236,7 @@ def _cmd_history(args):
_ui_t_title("Boots", rl))
for backup in reversed(backups):
data = list(sorted(lines2datas(bfname2lines(backup))))
data = lines2datas(bfname2lines(backup)))
data = list(filter_name_datas(data, args.hosts))
data = list(filter_osname_datas(data, args.osnames))
updates = sum(d.rpms for d in last_data)
@ -1628,7 +1631,6 @@ def _cmd_created(args):
dur += 60*60*24 # Broken, but mostly works. Do backups daily, or mtime.
data = lines2datas(bfname2lines(backup))
data = list(sorted(data))
# If a machine is missing from an older backup, it's marked as being
# (re)installed ... even though it might appear in an even older
@ -1673,12 +1675,12 @@ def _diff_hosts(data1, data2, show_both=False, show_utf8=True, skip_eq=False):
d1 = data1[0]
d2 = data2[0]
if d1.name < d2.name:
if natcmp(d1.name, d2.name) < 0:
_print_line('-', d1, prev=pdata)
pdata = data1.pop(0)
continue
if d1.name > d2.name:
if natcmp(d1.name, d2.name) > 0:
_print_line('+', d2, prev=pdata)
pdata = data2.pop(0)
continue
@ -1784,7 +1786,6 @@ def _cmd_host(args):
# Would be a lot faster if we could exit early here, but it's difficult
for backup in hbackups:
data = filter_name_datas(lines2datas(bfname2lines(backup)), hosts)
data = list(sorted(data))
data = list(filter_osname_datas(data, args.osnames))
_max_update(data)
_max_update_correct(' ')
@ -1798,7 +1799,6 @@ def _cmd_host(args):
thostnum = 0
for backup in hbackups:
data = filter_name_datas(lines2datas(bfname2lines(backup)), hosts)
data = list(sorted(data))
data = list(filter_osname_datas(data, args.osnames))
if done and skipped_num < 1: