Skip to content

Commit

Permalink
Prepare for distutils.version being removed in Python 3.12
Browse files Browse the repository at this point in the history
distutils has been deprecafed and will be removed from
Python's stdlib in Python 3.12 (see 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.

Co-authored-by: Felix Fontein <felix@fontein.de>
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde and felixfontein committed Dec 27, 2021
1 parent 39b6c43 commit 6d59c0b
Show file tree
Hide file tree
Showing 5 changed files with 34 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."
2 changes: 1 addition & 1 deletion molecule/default/roles/helm/library/helm_test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
'''

import re
from distutils.version import LooseVersion
from ansible_collections.kubernetes.core.plugins.module_utils.version import LooseVersion

from ansible.module_utils.basic import AnsibleModule

Expand Down
4 changes: 3 additions & 1 deletion plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import sys
import hashlib
from datetime import datetime
from distutils.version import LooseVersion

from ansible_collections.kubernetes.core.plugins.module_utils.version import (
LooseVersion,
)
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
AUTH_ARG_MAP,
AUTH_ARG_SPEC,
Expand Down
25 changes: 25 additions & 0 deletions plugins/module_utils/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

# Copyright: (c) 2021, Felix Fontein <felix@fontein.de>
# 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
except ImportError:
try:
from distutils.version import LooseVersion
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,
)
4 changes: 3 additions & 1 deletion plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@

import tempfile
import traceback
from distutils.version import LooseVersion
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
LooseVersion,
)

try:
import yaml
Expand Down

0 comments on commit 6d59c0b

Please sign in to comment.