From f4f2e3a951ed86893b2dd1194bcb9a22582f3efe Mon Sep 17 00:00:00 2001 From: Jakub Frejlach Date: Thu, 8 Feb 2024 16:14:14 +0100 Subject: [PATCH] Fix picking latest NVR To pick the latest NVR we need to sort the list of NVRs naturally otherwise it might produce incorrect assumption since two almost identical NVRs which only has the difference that one part of the version has two digits is considered to be lower than the one with only one digit on the same place --- griffon/helpers.py | 8 ++++++++ griffon/output.py | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/griffon/helpers.py b/griffon/helpers.py index c006976..04ac75a 100644 --- a/griffon/helpers.py +++ b/griffon/helpers.py @@ -1,7 +1,9 @@ """ Helpers for direct usage or debbuging """ + import json +import re from enum import Enum from typing import Callable, Optional, Type, Union @@ -106,3 +108,9 @@ class Style(Enum): def __str__(self): return str(self.value) + + +def natural_sort_key(string): + """Key for builtin sorted function to perform natural sort""" + split_by_digit = re.split("([0-9]+)", string) + return [int(part) if part.isdigit() else part.lower() for part in split_by_digit] diff --git a/griffon/output.py b/griffon/output.py index f389cf4..683f8c6 100644 --- a/griffon/output.py +++ b/griffon/output.py @@ -17,6 +17,8 @@ from rich.text import Text from rich.tree import Tree +from .helpers import natural_sort_key + console = Console(color_system="auto") logger = logging.getLogger("griffon") @@ -511,7 +513,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -579,7 +583,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -703,7 +709,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -856,7 +864,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -971,7 +981,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"],