mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-06-09 18:15:54 +00:00
Fix all deprecated module_utils imports before ansible-core 2.24 removal
SUMMARY
Fixes all deprecated ansible.module_utils imports across the entire collection that will be removed in ansible-core 2.24.
This PR comprehensively addresses deprecation warnings reported in #686 by updating import statements in 20 files to use the new recommended import paths, and removes 8 unused test utility files that contained deprecated imports.
Deprecated imports replaced:
Deprecated import
Replacement
ansible.module_utils._text
ansible.module_utils.common.text.converters
ansible.module_utils.common._collections_compat
collections.abc
ansible.module_utils.six.moves.shlex_quote
shlex.quote
ansible.module_utils.six.moves.reduce
functools.reduce
ansible.module_utils.six.moves.urllib.parse.urlparse
urllib.parse.urlparse
ansible.module_utils.six.string_types
basestring/str (Python 2/3 compatible)
ansible.module_utils.six.text_type
str
ansible.module_utils.six.PY3
Removed (simplified Python 2/3 conditionals)
ansible.module_utils.six.with_metaclass
Native metaclass= syntax
ansible.module_utils.six.iteritems
dict.items()
Files fixed (20 files, 1 commit per file for easier review):
plugins/action/patch.py
plugins/action/synchronize.py
plugins/callback/cgroup_perf_recap.py
plugins/callback/json.py
plugins/callback/jsonl.py
plugins/callback/profile_roles.py
plugins/callback/profile_tasks.py
plugins/modules/acl.py
plugins/modules/authorized_key.py
plugins/modules/firewalld_info.py
plugins/modules/mount.py
plugins/modules/patch.py
plugins/modules/rhel_rpm_ostree.py
plugins/modules/rpm_ostree_upgrade.py
plugins/modules/seboolean.py
plugins/modules/synchronize.py
plugins/modules/sysctl.py
plugins/shell/csh.py
plugins/shell/fish.py
tests/unit/modules/system/test_mount.py
Files deleted (8 unused test utility files):
These files are dead code - none of them are imported or used anywhere in the test suite or the collection. Removing them also addresses Python 2.7 compatibility concerns raised in code review, as several contained deprecated imports that would be incorrect to fix for Python 2.
tests/unit/compat/builtins.py
tests/unit/mock/loader.py
tests/unit/mock/path.py
tests/unit/mock/procenv.py
tests/unit/mock/vault_helper.py
tests/unit/mock/yaml_helper.py
tests/unit/modules/conftest.py
tests/unit/modules/utils.py
Completeness verified with:
git grep -n -P '_compat|utils._text|utils.six' -- '*.py' | grep -v yml
This command returns no results, confirming all deprecated imports have been replaced.
Notes on Python 2.7 compatibility:
For modules that may run on Python 2.7 managed hosts (e.g., authorized_key.py, synchronize.py, sysctl.py), Python 2/3 compatible fallbacks were used instead of direct Python 3 replacements:
authorized_key.py: try/except ImportError for urllib.parse.urlparse (falls back to urlparse on Python 2)
synchronize.py: try/except ImportError for shlex.quote (falls back to pipes.quote on Python 2)
sysctl.py: uses sys.version_info to set string_types to str on Python 3 (basestring on Python 2)
Also removes corresponding pylint:ansible-bad-import-from entries from tests/sanity/ignore-2.21.txt and tests/sanity/ignore-2.22.txt where applicable.
Fixes #686
ISSUE TYPE
Bugfix Pull Request
ADDITIONAL INFORMATION
Approach:
Each file is fixed in a separate commit for easier code review. The changelog fragment is added in a final commit. Corresponding pylint:ansible-bad-import-from ignore entries in tests/sanity/ignore-2.21.txt and tests/sanity/ignore-2.22.txt are removed in the same commit as the file fix (or the file removal commit).
CI results:
All 59 checks passing (Azure Pipelines sanity, units, lint, Docker, Remote across ansible-core 2.17 through devel, and Zuul ansible/check).
Reviewed-by: Felix Fontein <felix@fontein.de>
Reviewed-by: Pavel Bar
Reviewed-by: Abhijeet Kasurde
(cherry picked from commit 2022c1bd86)
Co-authored-by: centosinfra-prod-github-app[bot] <161850885+centosinfra-prod-github-app[bot]@users.noreply.github.com>
145 lines
4.2 KiB
Python
145 lines
4.2 KiB
Python
# (c) 2017, Tennis Smith, https://github.com/gamename
|
|
# (c) 2017 Ansible Project
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# Make coding more python3-ish
|
|
from __future__ import (absolute_import, division, print_function)
|
|
__metaclass__ = type
|
|
|
|
DOCUMENTATION = '''
|
|
name: profile_roles
|
|
type: aggregate
|
|
short_description: adds timing information to roles
|
|
description:
|
|
- This callback module provides profiling for ansible roles.
|
|
requirements:
|
|
- whitelisting in configuration
|
|
options:
|
|
summary_only:
|
|
description:
|
|
- Only show summary, not individual task profiles.
|
|
Especially usefull in combination with C(DISPLAY_SKIPPED_HOSTS=false) and/or C(ANSIBLE_DISPLAY_OK_HOSTS=false).
|
|
type: bool
|
|
default: False
|
|
env:
|
|
- name: PROFILE_ROLES_SUMMARY_ONLY
|
|
ini:
|
|
- section: callback_profile_roles
|
|
key: summary_only
|
|
version_added: 1.5.0
|
|
'''
|
|
|
|
import collections
|
|
import time
|
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
from functools import reduce
|
|
|
|
# define start time
|
|
t0 = tn = time.time()
|
|
|
|
|
|
def secondsToStr(t):
|
|
# http://bytes.com/topic/python/answers/635958-handy-short-cut-formatting-elapsed-time-floating-point-seconds
|
|
def rediv(ll, b):
|
|
return list(divmod(ll[0], b)) + ll[1:]
|
|
|
|
return "%d:%02d:%02d.%03d" % tuple(
|
|
reduce(rediv, [[t * 1000, ], 1000, 60, 60]))
|
|
|
|
|
|
def filled(msg, fchar="*"):
|
|
if len(msg) == 0:
|
|
width = 79
|
|
else:
|
|
msg = "%s " % msg
|
|
width = 79 - len(msg)
|
|
if width < 3:
|
|
width = 3
|
|
filler = fchar * width
|
|
return "%s%s " % (msg, filler)
|
|
|
|
|
|
def timestamp(self):
|
|
if self.current is not None:
|
|
self.stats[self.current] = time.time() - self.stats[self.current]
|
|
self.totals[self.current] += self.stats[self.current]
|
|
|
|
|
|
def tasktime():
|
|
global tn
|
|
time_current = time.strftime('%A %d %B %Y %H:%M:%S %z')
|
|
time_elapsed = secondsToStr(time.time() - tn)
|
|
time_total_elapsed = secondsToStr(time.time() - t0)
|
|
tn = time.time()
|
|
return filled('%s (%s)%s%s' %
|
|
(time_current, time_elapsed, ' ' * 7, time_total_elapsed))
|
|
|
|
|
|
class CallbackModule(CallbackBase):
|
|
"""
|
|
This callback module provides profiling for ansible roles.
|
|
"""
|
|
CALLBACK_VERSION = 2.0
|
|
CALLBACK_TYPE = 'aggregate'
|
|
CALLBACK_NAME = 'ansible.posix.profile_roles'
|
|
CALLBACK_NEEDS_WHITELIST = True
|
|
|
|
def __init__(self):
|
|
self.stats = collections.Counter()
|
|
self.totals = collections.Counter()
|
|
self.current = None
|
|
|
|
self.summary_only = None
|
|
|
|
super(CallbackModule, self).__init__()
|
|
|
|
def set_options(self, task_keys=None, var_options=None, direct=None):
|
|
|
|
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
|
|
|
|
self.summary_only = self.get_option('summary_only')
|
|
|
|
def _display_tasktime(self):
|
|
if not self.summary_only:
|
|
self._display.display(tasktime())
|
|
|
|
def _record_task(self, task):
|
|
"""
|
|
Logs the start of each task
|
|
"""
|
|
self._display_tasktime()
|
|
timestamp(self)
|
|
|
|
if task._role:
|
|
self.current = task._role._role_name
|
|
else:
|
|
self.current = task.action
|
|
|
|
self.stats[self.current] = time.time()
|
|
|
|
def v2_playbook_on_task_start(self, task, is_conditional):
|
|
self._record_task(task)
|
|
|
|
def v2_playbook_on_handler_task_start(self, task):
|
|
self._record_task(task)
|
|
|
|
def v2_playbook_on_stats(self, stats):
|
|
# Align summary report header with other callback plugin summary
|
|
self._display.banner("ROLES RECAP")
|
|
|
|
self._display.display(tasktime())
|
|
self._display.display(filled("", fchar="="))
|
|
|
|
timestamp(self)
|
|
total_time = sum(self.totals.values())
|
|
|
|
# Print the timings starting with the largest one
|
|
for result in self.totals.most_common():
|
|
msg = u"{0:-<70}{1:->9}".format(result[0] + u' ', u' {0:.02f}s'.format(result[1]))
|
|
self._display.display(msg)
|
|
|
|
msg_total = u"{0:-<70}{1:->9}".format(u'total ', u' {0:.02f}s'.format(total_time))
|
|
self._display.display(filled("", fchar="~"))
|
|
self._display.display(msg_total)
|