1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add version_sort filter to properly sort list of versions (#1916)

* Add version_sort filter to properly sort list of versions

* Fix all comments from Felix

- add changelog fragment
- fix test by removing runme.sh/yml and renaming to filter_version_sort
- use fully qualified name of filter in test case

* Remove wrong plugin.test changelog fragment

Ups...

* Properly name the file version_sort.py

* Update changelogs/fragments/1916-add-version-sort-filter.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Eric L <ewl+git@lavar.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Eric L 2021-02-27 17:42:45 +01:00 committed by GitHub
parent 20bd065e77
commit 4a8d6cf7cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,3 @@
add plugin.filter:
- name: version_sort
description: Sort a list according to version order instead of pure alphabetical one

View file

@ -0,0 +1,21 @@
# Copyright (C) 2021 Eric Lavarde <elavarde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from distutils.version import LooseVersion
def version_sort(value, reverse=False):
'''Sort a list according to loose versions so that e.g. 2.9 is smaller than 2.10'''
return sorted(value, key=LooseVersion, reverse=reverse)
class FilterModule(object):
''' Version sort filter '''
def filters(self):
return {
'version_sort': version_sort
}

View file

@ -0,0 +1,2 @@
shippable/posix/group2
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller

View file

@ -0,0 +1,10 @@
---
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################
- name: validate that versions are properly sorted in a stable way
assert:
that:
- "['a-1.9.rpm', 'a-1.10-1.rpm', 'a-1.09.rpm', 'b-1.01.rpm', 'a-2.1-0.rpm', 'a-1.10-0.rpm'] | community.general.version_sort == ['a-1.9.rpm', 'a-1.09.rpm', 'a-1.10-0.rpm', 'a-1.10-1.rpm', 'a-2.1-0.rpm', 'b-1.01.rpm']"