Skip to content

Commit efa19ae

Browse files
Merge pull request #9 from guidograzioli/version_sort
Version sort
2 parents a99c7ec + 2339713 commit efa19ae

File tree

5 files changed

+439
-8
lines changed

5 files changed

+439
-8
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Ansible Collection - Common
2-
2+
<!--start build_status -->
33
[![Build Status](https://github.com/ansible-middleware/common/workflows/CI/badge.svg?branch=main)](https://github.com/ansible-middleware/common/actions/workflows/ci.yml)
4-
4+
<!--end build_status -->
55
## About
66

77
Collection containing common utilities to support Ansible Middleware automation
@@ -20,17 +20,23 @@ This collection has been tested against following Pyhton versions: **>=3.6**.
2020

2121
## Included content
2222

23-
### Modules:
23+
### Modules
2424

2525
* `product_download`: downloads products from the JBoss Network API
2626
* `product_search`: searches products from the JBoss Network API
2727

28+
### Filters
29+
30+
* `version_sort`: sort a list of strings according to version ordering
31+
32+
2833
## Installation
2934

35+
<!--start galaxy_download -->
3036
### Download from galaxy
3137

3238
ansible-galaxy collection install middleware_automation.common
33-
39+
<!--end galaxy_download -->
3440

3541
### Build and install locally
3642

@@ -50,9 +56,8 @@ To install all the dependencies via galaxy:
5056

5157
pip install -r requirements.txt
5258

53-
## Support
54-
55-
The common collection is for [Technical Preview](https://access.redhat.com/support/offerings/techpreview). If you have any issues or questions related to collection, please don't hesitate to contact us on <Ansible-middleware-core@redhat.com> or open an issue on <https://github.com/ansible-middleware/common/issues>
59+
<!--start support -->
60+
<!--end support -->
5661

5762
## License
5863

galaxy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
---
22
namespace: middleware_automation
33
name: common
4-
version: "1.0.2"
4+
version: "1.1.0"
55
readme: README.md
66
authors:
77
- Andrew Block <ablock@redhat.com>
8+
- Guido Grazioli <ggraziol@redhat.com>
89
description: Common utilities to support Ansible Middleware automation.
910
license_file: "LICENSE"
1011
tags:

plugins/filter/version_sort.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2021 Eric Lavarde <elavarde@redhat.com>
3+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
6+
from __future__ import (absolute_import, division, print_function)
7+
__metaclass__ = type
8+
9+
DOCUMENTATION = '''
10+
name: version_sort
11+
short_description: Sort a list according to version order instead of pure alphabetical one
12+
version_added: 2.2.0
13+
author: Eric L. (@ericzolf)
14+
description:
15+
- Sort a list according to version order instead of pure alphabetical one.
16+
options:
17+
_input:
18+
description: A list of strings to sort.
19+
type: list
20+
elements: string
21+
required: true
22+
'''
23+
24+
EXAMPLES = '''
25+
- name: Convert list of tuples into dictionary
26+
ansible.builtin.set_fact:
27+
dictionary: "{{ ['2.1', '2.10', '2.9'] | middleware_automation.common.version_sort }}"
28+
# Result is ['2.1', '2.9', '2.10']
29+
'''
30+
31+
RETURN = '''
32+
_value:
33+
description: The list of strings sorted by version.
34+
type: list
35+
elements: string
36+
'''
37+
38+
from ansible_collections.middleware_automation.common.plugins.module_utils.version import LooseVersion
39+
40+
41+
def version_sort(value, reverse=False):
42+
'''Sort a list according to loose versions so that e.g. 2.9 is smaller than 2.10'''
43+
return sorted(value, key=LooseVersion, reverse=reverse)
44+
45+
46+
class FilterModule(object):
47+
''' Version sort filter '''
48+
49+
def filters(self):
50+
return {
51+
'version_sort': version_sort
52+
}

plugins/module_utils/version.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2021, Felix Fontein <felix@fontein.de>
4+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
7+
"""Provide version object to compare version numbers."""
8+
9+
from __future__ import absolute_import, division, print_function
10+
__metaclass__ = type
11+
12+
13+
from ansible.module_utils.six import raise_from
14+
15+
try:
16+
from ansible.module_utils.compat.version import LooseVersion # noqa: F401, pylint: disable=unused-import
17+
except ImportError:
18+
try:
19+
from distutils.version import LooseVersion # noqa: F401, pylint: disable=unused-import
20+
except ImportError as exc:
21+
msg = 'To use this plugin or module with ansible-core 2.11, you need to use Python < 3.12 with distutils.version present'
22+
raise_from(ImportError(msg), exc)

0 commit comments

Comments
 (0)