From 0ce5a84cdbdd6f4f6d7d55da283be932d063b45f Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 15 Jan 2026 11:30:00 +0000 Subject: [PATCH] WIP --- ansible_mitogen/compat/six.py | 89 +++++++++++++++++++ ansible_mitogen/mixins.py | 7 +- .../plugins/action/mitogen_fetch.py | 4 +- ansible_mitogen/runner.py | 6 +- ansible_mitogen/services.py | 3 +- ansible_mitogen/transport_config.py | 4 +- mitogen/ssh.py | 5 +- 7 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 ansible_mitogen/compat/six.py diff --git a/ansible_mitogen/compat/six.py b/ansible_mitogen/compat/six.py new file mode 100644 index 000000000..b0c2f479a --- /dev/null +++ b/ansible_mitogen/compat/six.py @@ -0,0 +1,89 @@ +# SPDX-FileCopyrightText: 2010-2024 Benjamin Peterson +# SPDX-FileCopyrightText: 2026 Mitogen authors +# SPDX-License-Identifier: MIT +# Source: https://github.com/benjaminp/six/blob/1.17.0/six.py +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import absolute_import + +import sys +import types + +if sys.version_info >= (3, 0): + exec_ = getattr(__import__('builtins'), 'exec') + + string_types = (str,) + + def reraise(tp, value, tb=None): + try: + if value is None: + value = tp() + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + finally: + value = None + tb = None + +else: + string_types = (basestring,) + + def exec_(_code_, _globs_=None, _locs_=None): + """Execute code in a namespace.""" + if _globs_ is None: + frame = sys._getframe(1) + _globs_ = frame.f_globals + if _locs_ is None: + _locs_ = frame.f_locals + del frame + elif _locs_ is None: + _locs_ = _globs_ + exec("""exec _code_ in _globs_, _locs_""") + + exec_("""def reraise(tp, value, tb=None): + try: + raise tp, value, tb + finally: + tb = None +""") + + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + # This requires a bit of explanation: the basic idea is to make a dummy + # metaclass for one level of class instantiation that replaces itself with + # the actual metaclass. + class metaclass(type): + + def __new__(cls, name, this_bases, d): + if sys.version_info[:2] >= (3, 7): + # This version introduced PEP 560 that requires a bit + # of extra care (we mimic what is done by __build_class__). + resolved_bases = types.resolve_bases(bases) + if resolved_bases is not bases: + d['__orig_bases__'] = bases + else: + resolved_bases = bases + return meta(name, resolved_bases, d) + + @classmethod + def __prepare__(cls, name, this_bases): + return meta.__prepare__(name, bases) + return type.__new__(metaclass, 'temporary_class', (), {}) diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index 7065c69a7..ecd1ef22b 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -34,15 +34,20 @@ import os import pwd import random +import sys import traceback +if sys.version_info >= (3, 3): + from shlex import quote as shlex_quote +else: + from pipes import quote as shlex_quote + import ansible import ansible.plugins.action import ansible.utils.unsafe_proxy import ansible.vars.clean from ansible.module_utils.common.text.converters import to_bytes, to_text -from ansible.module_utils.six.moves import shlex_quote import mitogen.core import mitogen.select diff --git a/ansible_mitogen/plugins/action/mitogen_fetch.py b/ansible_mitogen/plugins/action/mitogen_fetch.py index c1ef19020..237de496a 100644 --- a/ansible_mitogen/plugins/action/mitogen_fetch.py +++ b/ansible_mitogen/plugins/action/mitogen_fetch.py @@ -19,15 +19,17 @@ import os import base64 + from ansible.errors import AnsibleError, AnsibleActionFail, AnsibleActionSkip from ansible.module_utils.common.text.converters import to_bytes, to_text -from ansible.module_utils.six import string_types from ansible.module_utils.parsing.convert_bool import boolean from ansible.plugins.action import ActionBase from ansible.utils.display import Display from ansible.utils.hashing import checksum, checksum_s, md5, secure_hash from ansible.utils.path import makedirs_safe, is_subpath +from ansible_mitogen.compat.six import string_types + display = Display() diff --git a/ansible_mitogen/runner.py b/ansible_mitogen/runner.py index f9c51bd62..0fc0d5d97 100644 --- a/ansible_mitogen/runner.py +++ b/ansible_mitogen/runner.py @@ -43,6 +43,7 @@ import ctypes import json import logging +import io import os import re import shlex @@ -57,7 +58,10 @@ else: import imp -from ansible.module_utils.six.moves import shlex_quote +if sys.version_info >= (3, 3): + from shlex import quote as shlex_quote +else: + from pipes import quote as shlex_quote import mitogen.core import ansible_mitogen.target # TODO: circular import diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index a48ab7571..df5cb7d03 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -50,8 +50,6 @@ import ansible.constants -from ansible.module_utils.six import reraise - import mitogen.core import mitogen.service import ansible_mitogen.loaders @@ -59,6 +57,7 @@ import ansible_mitogen.target import ansible_mitogen.utils import ansible_mitogen.utils.unsafe +from ansible_mitogen.compat.six import reraise LOG = logging.getLogger(__name__) diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 203ce7900..f9550fbc4 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -69,11 +69,11 @@ import ansible.constants as C import ansible.executor.interpreter_discovery import ansible.utils.unsafe_proxy - -from ansible.module_utils.six import with_metaclass from ansible.module_utils.parsing.convert_bool import boolean import ansible_mitogen.utils +from ansible_mitogen.compat.six import with_metaclass + import mitogen.core diff --git a/mitogen/ssh.py b/mitogen/ssh.py index c43d00da8..ccd188e7f 100644 --- a/mitogen/ssh.py +++ b/mitogen/ssh.py @@ -34,10 +34,11 @@ import logging import re +import sys -try: +if sys.version_info >= (3, 3): from shlex import quote as shlex_quote -except ImportError: +else: from pipes import quote as shlex_quote import mitogen.core