1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

add dependency manager (#5535) (#5574)

* add dependency manager

* add plugins/module_utils/deps.py to BOTMETA

* ditch usng OrderedDict to keep compatibility with Python 2.6

* Update plugins/module_utils/deps.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 0624951e17)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2022-11-23 07:37:45 +01:00 committed by GitHub
parent 38b4e316ae
commit ffee01cd9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 69 deletions

2
.github/BOTMETA.yml vendored
View file

@ -265,6 +265,8 @@ files:
maintainers: delineaKrehl tylerezimmerman maintainers: delineaKrehl tylerezimmerman
$module_utils/: $module_utils/:
labels: module_utils labels: module_utils
$module_utils/deps.py:
maintainers: russoz
$module_utils/gconftool2.py: $module_utils/gconftool2.py:
labels: gconftool2 labels: gconftool2
maintainers: russoz maintainers: russoz

View file

@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
# (c) 2022, Alexei Znamensky <russoz@gmail.com>
# Copyright (c) 2022, Ansible Project
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
# SPDX-License-Identifier: BSD-2-Clause
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import traceback
from contextlib import contextmanager
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.basic import missing_required_lib
_deps = dict()
class _Dependency(object):
_states = ["pending", "failure", "success"]
def __init__(self, name, reason=None, url=None, msg=None):
self.name = name
self.reason = reason
self.url = url
self.msg = msg
self.state = 0
self.trace = None
self.exc = None
def succeed(self):
self.state = 2
def fail(self, exc, trace):
self.state = 1
self.exc = exc
self.trace = trace
@property
def message(self):
if self.msg:
return to_native(self.msg)
else:
return missing_required_lib(self.name, reason=self.reason, url=self.url)
@property
def failed(self):
return self.state == 1
def verify(self, module):
if self.failed:
module.fail_json(msg=self.message, exception=self.trace)
def __str__(self):
return "<dependency: {0} [{1}]>".format(self.name, self._states[self.state])
@contextmanager
def declare(name, *args, **kwargs):
dep = _Dependency(name, *args, **kwargs)
try:
yield dep
except Exception as e:
dep.fail(e, traceback.format_exc())
else:
dep.succeed()
finally:
_deps[name] = dep
def validate(module, spec=None):
dep_names = sorted(_deps)
if spec is not None:
if spec.startswith("-"):
spec_split = spec[1:].split(":")
for d in spec_split:
dep_names.remove(d)
else:
spec_split = spec[1:].split(":")
dep_names = []
for d in spec_split:
_deps[d] # ensure it exists
dep_names.append(d)
for dep in dep_names:
_deps[dep].verify(module)

View file

@ -230,18 +230,11 @@ dnsimple_record_info:
type: str type: str
''' '''
import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import missing_required_lib from ansible_collections.community.general.plugins.module_utils import deps
try: with deps.declare("requests"):
from requests import Request, Session from requests import Request, Session
except ImportError:
HAS_REQUESTS = False
REQUESTS_IMPORT_ERROR = traceback.format_exc()
else:
HAS_REQUESTS = True
REQUESTS_IMPORT_ERROR = None
def build_url(account, key, is_sandbox): def build_url(account, key, is_sandbox):
@ -310,10 +303,7 @@ def main():
params['api_key'], params['api_key'],
params['sandbox']) params['sandbox'])
if not HAS_REQUESTS: deps.validate(module)
module.exit_json(
msg=missing_required_lib('requests'),
exception=REQUESTS_IMPORT_ERROR)
# At minimum we need account and key # At minimum we need account and key
if params['account_id'] and params['api_key']: if params['account_id'] and params['api_key']:

View file

@ -97,19 +97,14 @@ dest_iso:
''' '''
import os import os
import traceback
PYCDLIB_IMP_ERR = None from ansible_collections.community.general.plugins.module_utils import deps
try: from ansible.module_utils.basic import AnsibleModule
import pycdlib
HAS_PYCDLIB = True
except ImportError:
PYCDLIB_IMP_ERR = traceback.format_exc()
HAS_PYCDLIB = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
with deps.declare("pycdlib"):
import pycdlib
# The upper dir exist, we only add subdirectoy # The upper dir exist, we only add subdirectoy
def iso_add_dir(module, opened_iso, iso_type, dir_path): def iso_add_dir(module, opened_iso, iso_type, dir_path):
@ -306,9 +301,7 @@ def main():
required_one_of=[('delete_files', 'add_files'), ], required_one_of=[('delete_files', 'add_files'), ],
supports_check_mode=True, supports_check_mode=True,
) )
if not HAS_PYCDLIB: deps.validate(module)
module.fail_json(
missing_required_lib('pycdlib'), exception=PYCDLIB_IMP_ERR)
src_iso = module.params['src_iso'] src_iso = module.params['src_iso']
if not os.path.exists(src_iso): if not os.path.exists(src_iso):

View file

@ -80,25 +80,12 @@ EXAMPLES = r'''
RETURN = r''' # ''' RETURN = r''' # '''
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
import traceback
from os import path from os import path
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils import deps
try: with deps.declare("pdpyras", url="https://github.com/PagerDuty/pdpyras"):
from pdpyras import APISession from pdpyras import APISession, PDClientError
HAS_PD_PY = True
PD_IMPORT_ERR = None
except ImportError:
HAS_PD_PY = False
PD_IMPORT_ERR = traceback.format_exc()
try:
from pdpyras import PDClientError
HAS_PD_CLIENT_ERR = True
PD_CLIENT_ERR_IMPORT_ERR = None
except ImportError:
HAS_PD_CLIENT_ERR = False
PD_CLIENT_ERR_IMPORT_ERR = traceback.format_exc()
class PagerDutyUser(object): class PagerDutyUser(object):
@ -202,11 +189,7 @@ def main():
supports_check_mode=True, supports_check_mode=True,
) )
if not HAS_PD_PY: deps.validate(module)
module.fail_json(msg=missing_required_lib('pdpyras', url='https://github.com/PagerDuty/pdpyras'), exception=PD_IMPORT_ERR)
if not HAS_PD_CLIENT_ERR:
module.fail_json(msg=missing_required_lib('PDClientError', url='https://github.com/PagerDuty/pdpyras'), exception=PD_CLIENT_ERR_IMPORT_ERR)
access_token = module.params['access_token'] access_token = module.params['access_token']
pd_user = module.params['pd_user'] pd_user = module.params['pd_user']

View file

@ -3,8 +3,8 @@
# Copyright (c) 2019, Saranya Sridharan # Copyright (c) 2019, Saranya Sridharan
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
DOCUMENTATION = ''' DOCUMENTATION = '''
@ -60,18 +60,15 @@ import re
from os.path import basename from os.path import basename
from ansible.module_utils import six from ansible.module_utils import six
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils import deps
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
try: with deps.declare("psutil"):
import psutil import psutil
HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False
class PSAdapterError(Exception): class PSAdapterError(Exception):
pass pass
@ -177,8 +174,8 @@ def compare_lower(a, b):
class Pids(object): class Pids(object):
def __init__(self, module): def __init__(self, module):
if not HAS_PSUTIL:
module.fail_json(msg=missing_required_lib('psutil')) deps.validate(module)
self._ps = PSAdapter.from_package(psutil) self._ps = PSAdapter.from_package(psutil)

View file

@ -183,20 +183,14 @@ ansible_interfaces:
''' '''
import binascii import binascii
import traceback
from collections import defaultdict from collections import defaultdict
from ansible_collections.community.general.plugins.module_utils import deps
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_text
PYSNMP_IMP_ERR = None with deps.declare("pysnmp"):
try:
from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto.rfc1905 import EndOfMibView from pysnmp.proto.rfc1905 import EndOfMibView
HAS_PYSNMP = True
except Exception:
PYSNMP_IMP_ERR = traceback.format_exc()
HAS_PYSNMP = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_text
class DefineOid(object): class DefineOid(object):
@ -299,8 +293,7 @@ def main():
m_args = module.params m_args = module.params
if not HAS_PYSNMP: deps.validate(module)
module.fail_json(msg=missing_required_lib('pysnmp'), exception=PYSNMP_IMP_ERR)
cmdGen = cmdgen.CommandGenerator() cmdGen = cmdgen.CommandGenerator()
transport_opts = dict((k, m_args[k]) for k in ('timeout', 'retries') if m_args[k] is not None) transport_opts = dict((k, m_args[k]) for k in ('timeout', 'retries') if m_args[k] is not None)