Skip to content

Commit

Permalink
Prepare for distutils.version being removed in Python 3.12 (#1165)
Browse files Browse the repository at this point in the history
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 [email protected]
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 <[email protected]>
Reviewed-by: Felix Fontein <[email protected]>
Reviewed-by: Mario Lenz <[email protected]>
Reviewed-by: None <None>
  • Loading branch information
Akasurde authored Dec 29, 2021
1 parent 801361b commit ca7901f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/disutils.version.yml
Original file line number Diff line number Diff line change
@@ -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."
19 changes: 19 additions & 0 deletions plugins/module_utils/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

# Copyright: (c) 2021, Felix Fontein <[email protected]>
# 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)
3 changes: 1 addition & 2 deletions plugins/modules/vmware_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/vmware_vc_infraprofile_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca7901f

Please sign in to comment.