From de04f3dd2410c34b0f862c7391ed411d2acff0ce Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Mon, 3 May 2021 21:03:16 +0700 Subject: Use the natural sort function for snapshot sorting (courtesy of Christian Poessinger) --- scripts/list-snapshots.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'scripts') diff --git a/scripts/list-snapshots.py b/scripts/list-snapshots.py index 4079f2c..96339e7 100755 --- a/scripts/list-snapshots.py +++ b/scripts/list-snapshots.py @@ -21,19 +21,19 @@ def make_link(s, f): f = re.sub(r'\s+', '+', f) return "https://s3.amazonaws.com/{0}/{1}".format(bucket, f) -def compare(l, r): - try: - regex = r'\-(\d+)$' - l_date = int(re.search(regex, l).group(1)) - r_date = int(re.search(regex, r).group(1)) - if l_date == r_date: - return 0 - elif l_date > r_date: - return 1 - else: - return -1 - except: - return(-1) +def natural_sort(iterable, reverse=False): + import re + from jinja2.runtime import Undefined + + if isinstance(iterable, Undefined) or iterable is None: + return list() + + def convert(text): + return int(text) if text.isdigit() else text.lower() + def alphanum_key(key): + return [convert(c) for c in re.split('([0-9]+)', str(key))] + + return sorted(iterable, key=alphanum_key, reverse=reverse) s3 = boto3.client('s3') @@ -64,10 +64,10 @@ for name in snapshot_names: snapshots.append(snapshot) -snapshots.sort(reverse=True, key=cmp_to_key(lambda l, r: compare(l["name"], r["name"]))) +snapshots = natural_sort(snapshots, reverse=True) tmpl = jinja2.Template(""" -{% for s in snapshots | natural_sort %} +{% for s in snapshots %} #

{{s.name}}