From ca7901f63be3c7bb273c257aef531e65455505df Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 29 Dec 2021 23:23:28 +0530 Subject: [PATCH] Prepare for distutils.version being removed in Python 3.12 (#1165) Prepare for distutils.version being removed in Python 3.12 SUMMARY distutils has been deprecafed and will be removed from Python's stdlib in Python 3.12 (see https://python.org/dev/peps/pep-0632). This PR replaces the use of distutils.version.LooseVersion and distutils.version.StrictVersion with LooseVersion from the vendored copy of distutils.version included with ansible-core 2.12 (ansible/ansible#74644) if available, and falls back to distutils.version for ansible-core 2.11 and before. Since ansible-core 2.11 and earlier do not support Python 3.12 (since they use LooseVersion itself in various places), this incomplete fix should be OK for now. Also, the way this PR works (by adding a new module_utils version that abstracts away where LooseVersion comes from), it is easy to also fix this for ansible-core 2.11 and earlier later on. Signed-off-by: Abhijeet Kasurde akasurde@redhat.com ISSUE TYPE Bugfix Pull Request COMPONENT NAME changelogs/fragments/disutils.version.yml plugins/module_utils/version.py plugins/modules/vmware_category.py plugins/modules/vmware_vc_infraprofile_info.py Reviewed-by: Andrew Klychkov Reviewed-by: Felix Fontein Reviewed-by: Mario Lenz Reviewed-by: None --- changelogs/fragments/disutils.version.yml | 2 ++ plugins/module_utils/version.py | 19 +++++++++++++++++++ plugins/modules/vmware_category.py | 3 +-- .../modules/vmware_vc_infraprofile_info.py | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/disutils.version.yml create mode 100644 plugins/module_utils/version.py diff --git a/changelogs/fragments/disutils.version.yml b/changelogs/fragments/disutils.version.yml new file mode 100644 index 000000000..48f69e4bd --- /dev/null +++ b/changelogs/fragments/disutils.version.yml @@ -0,0 +1,2 @@ +bugfixes: + - "Various modules and plugins - use vendored version of ``distutils.version`` included in ansible-core 2.12 if available. This avoids breakage when ``distutils`` is removed from the standard library of Python 3.12. Note that ansible-core 2.11, ansible-base 2.10 and Ansible 2.9 are right now not compatible with Python 3.12, hence this fix does not target these ansible-core/-base/2.9 versions." diff --git a/plugins/module_utils/version.py b/plugins/module_utils/version.py new file mode 100644 index 000000000..73934eb0c --- /dev/null +++ b/plugins/module_utils/version.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +# Copyright: (c) 2021, Felix Fontein +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +"""Provide version object to compare version numbers.""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.six import raise_from + +try: + from ansible.module_utils.compat.version import LooseVersion # noqa: F401 +except ImportError: + try: + from distutils.version import LooseVersion # noqa: F401 + except ImportError as exc: + raise_from(ImportError('To use this plugin or module with ansible-core < 2.11, you need to use Python < 3.12 with distutils.version present'), exc) diff --git a/plugins/modules/vmware_category.py b/plugins/modules/vmware_category.py index 78e6ee18b..f50b9e691 100644 --- a/plugins/modules/vmware_category.py +++ b/plugins/modules/vmware_category.py @@ -151,10 +151,9 @@ } ''' -from distutils.version import LooseVersion - from ansible.module_utils._text import to_native from ansible.module_utils.basic import AnsibleModule +from ansible_collections.community.vmware.plugins.module_utils.version import LooseVersion from ansible_collections.community.vmware.plugins.module_utils.vmware import connect_to_api from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient diff --git a/plugins/modules/vmware_vc_infraprofile_info.py b/plugins/modules/vmware_vc_infraprofile_info.py index 58f515fbd..9e27d17fb 100644 --- a/plugins/modules/vmware_vc_infraprofile_info.py +++ b/plugins/modules/vmware_vc_infraprofile_info.py @@ -149,8 +149,8 @@ } ''' -from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule +from ansible_collections.community.vmware.plugins.module_utils.version import LooseVersion from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi import json