diff options
author | Daniil Baturin <daniil@baturin.org> | 2021-05-03 21:03:16 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2021-05-03 21:03:16 +0700 |
commit | de04f3dd2410c34b0f862c7391ed411d2acff0ce (patch) | |
tree | d6a0aff3d154f34b85f5a4768bee56f959e0c83f | |
parent | c617e5e84bbab2a4ed947c61a5f1defb985499a0 (diff) | |
download | community.vyos.net-de04f3dd2410c34b0f862c7391ed411d2acff0ce.tar.gz community.vyos.net-de04f3dd2410c34b0f862c7391ed411d2acff0ce.zip |
Use the natural sort function for snapshot sorting (courtesy of Christian Poessinger)
-rwxr-xr-x | scripts/list-snapshots.py | 30 |
1 files changed, 15 insertions, 15 deletions
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 %} <a href="#{{s.name}}">#</a><h3 id="{{s.name}}">{{s.name}}</h3> <ul> {% for f in s.files %} |