Fix packaging/os modules for wildcard imports and get_exception

pull/35891/head
Toshio Kuratomi 7 years ago
parent 5c50720325
commit a5b80464df

@ -841,7 +841,7 @@ def get_cache(module):
try: try:
cache = apt.Cache() cache = apt.Cache()
except SystemError as e: except SystemError as e:
if '/var/lib/apt/lists/' in str(e).lower(): if '/var/lib/apt/lists/' in to_native(e).lower():
# update cache until files are fixed or retries exceeded # update cache until files are fixed or retries exceeded
retries = 0 retries = 0
while retries < 2: while retries < 2:
@ -850,7 +850,7 @@ def get_cache(module):
if rc == 0: if rc == 0:
break break
if rc != 0: if rc != 0:
module.fail_json(msg='Updating the cache to correct corrupt package lists failed:\n%s\n%s' % (str(e), str(so) + str(se)), rc=rc) module.fail_json(msg='Updating the cache to correct corrupt package lists failed:\n%s\n%s' % (to_native(e), so + se), rc=rc)
# try again # try again
cache = apt.Cache() cache = apt.Cache()
else: else:

@ -103,6 +103,7 @@ EXAMPLES = '''
''' '''
import glob import glob
import json
import os import os
import re import re
import sys import sys
@ -122,12 +123,17 @@ except ImportError:
distro = None distro = None
HAVE_PYTHON_APT = False HAVE_PYTHON_APT = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.urls import fetch_url
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
PYTHON_APT = 'python-apt' PYTHON_APT = 'python-apt'
else: else:
PYTHON_APT = 'python3-apt' PYTHON_APT = 'python3-apt'
DEFAULT_SOURCES_PERM = int('0644', 8) DEFAULT_SOURCES_PERM = 0o0644
VALID_SOURCE_TYPES = ('deb', 'deb-src') VALID_SOURCE_TYPES = ('deb', 'deb-src')

@ -41,6 +41,8 @@ EXAMPLES = '''
selection: hold selection: hold
''' '''
from ansible.module_utils.basic import AnsibleModule
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -72,7 +74,5 @@ def main():
module.exit_json(changed=changed, before=current, after=selection) module.exit_json(changed=changed, before=current, after=selection)
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -70,6 +70,8 @@ EXAMPLES = '''
import re import re
from ansible.module_utils.basic import AnsibleModule
def a_valid_tap(tap): def a_valid_tap(tap):
'''Returns True if the tap is valid.''' '''Returns True if the tap is valid.'''
@ -248,8 +250,6 @@ def main():
else: else:
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
# this is magic, see lib/ansible/module_common.py
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -83,6 +83,7 @@ EXAMPLES = '''
''' '''
import shutil import shutil
from os import path from os import path
try: try:
@ -92,6 +93,10 @@ try:
except ImportError: except ImportError:
HAS_LAYMAN_API = False HAS_LAYMAN_API = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
USERAGENT = 'ansible-httpget' USERAGENT = 'ansible-httpget'
@ -262,8 +267,5 @@ def main():
module.exit_json(changed=changed, name=name) module.exit_json(changed=changed, name=name)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -66,7 +66,8 @@ EXAMPLES = '''
state: inactive state: inactive
''' '''
import pipes from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import shlex_quote
def update_package_db(module, port_path): def update_package_db(module, port_path):
@ -83,7 +84,7 @@ def query_package(module, port_path, name, state="present"):
if state == "present": if state == "present":
rc, out, err = module.run_command("%s installed | grep -q ^.*%s" % (pipes.quote(port_path), pipes.quote(name)), use_unsafe_shell=True) rc, out, err = module.run_command("%s installed | grep -q ^.*%s" % (shlex_quote(port_path), shlex_quote(name)), use_unsafe_shell=True)
if rc == 0: if rc == 0:
return True return True
@ -91,7 +92,7 @@ def query_package(module, port_path, name, state="present"):
elif state == "active": elif state == "active":
rc, out, err = module.run_command("%s installed %s | grep -q active" % (pipes.quote(port_path), pipes.quote(name)), use_unsafe_shell=True) rc, out, err = module.run_command("%s installed %s | grep -q active" % (shlex_quote(port_path), shlex_quote(name)), use_unsafe_shell=True)
if rc == 0: if rc == 0:
return True return True
@ -225,8 +226,6 @@ def main():
elif p["state"] == "inactive": elif p["state"] == "inactive":
deactivate_packages(module, port_path, pkgs) deactivate_packages(module, port_path, pkgs)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -87,7 +87,8 @@ EXAMPLES = '''
force: overwrite force: overwrite
''' '''
import pipes from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import shlex_quote
def update_package_db(module, opkg_path): def update_package_db(module, opkg_path):
@ -104,7 +105,7 @@ def query_package(module, opkg_path, name, state="present"):
if state == "present": if state == "present":
rc, out, err = module.run_command("%s list-installed | grep -q \"^%s \"" % (pipes.quote(opkg_path), pipes.quote(name)), use_unsafe_shell=True) rc, out, err = module.run_command("%s list-installed | grep -q \"^%s \"" % (shlex_quote(opkg_path), shlex_quote(name)), use_unsafe_shell=True)
if rc == 0: if rc == 0:
return True return True
@ -193,8 +194,6 @@ def main():
elif p["state"] in ["absent", "removed"]: elif p["state"] in ["absent", "removed"]:
remove_packages(module, opkg_path, pkgs) remove_packages(module, opkg_path, pkgs)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -75,6 +75,8 @@ EXAMPLES = '''
origin: 'https://pkg.example.com/site/' origin: 'https://pkg.example.com/site/'
''' '''
from ansible.module_utils.basic import AnsibleModule
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -205,7 +207,5 @@ def unstringify(val):
return val return val
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -132,6 +132,8 @@ EXAMPLES = '''
import re import re
from ansible.module_utils.basic import AnsibleModule
def query_package(module, name): def query_package(module, name):
"""Search for the package by name. """Search for the package by name.
@ -382,8 +384,6 @@ def main():
elif p["state"] == "absent": elif p["state"] == "absent":
remove_packages(module, pkgs) remove_packages(module, pkgs)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -66,8 +66,7 @@ EXAMPLES = '''
state: latest state: latest
''' '''
import os from ansible.module_utils.basic import AnsibleModule
import pipes
def package_installed(module, name): def package_installed(module, name):
@ -225,8 +224,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -220,11 +220,11 @@ EXAMPLES = '''
depclean: yes depclean: yes
''' '''
import os import os
import pipes
import re import re
from ansible.module_utils.basic import AnsibleModule
def query_package(module, package, action): def query_package(module, package, action):
if package.startswith('@'): if package.startswith('@'):
@ -508,8 +508,6 @@ def main():
elif p['state'] in portage_absent_states: elif p['state'] in portage_absent_states:
unmerge_packages(module, packages) unmerge_packages(module, packages)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -68,6 +68,8 @@ EXAMPLES = '''
state: latest state: latest
''' '''
from ansible.module_utils.basic import AnsibleModule
def query_package(module, slackpkg_path, name): def query_package(module, slackpkg_path, name):
@ -195,8 +197,6 @@ def main():
elif p["state"] in ['removed', 'absent']: elif p["state"] in ['removed', 'absent']:
remove_packages(module, slackpkg_path, pkgs) remove_packages(module, slackpkg_path, pkgs)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -105,6 +105,8 @@ EXAMPLES = '''
import os import os
import tempfile import tempfile
from ansible.module_utils.basic import AnsibleModule
def package_installed(module, name, category): def package_installed(module, name, category):
cmd = [module.get_bin_path('pkginfo', True)] cmd = [module.get_bin_path('pkginfo', True)]
@ -257,8 +259,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -69,7 +69,9 @@ EXAMPLES = '''
''' '''
import re import re
import pipes
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import shlex_quote
def compare_package(version1, version2): def compare_package(version1, version2):
@ -97,10 +99,10 @@ def query_package(module, name, depot=None):
cmd_list = '/usr/sbin/swlist -a revision -l product' cmd_list = '/usr/sbin/swlist -a revision -l product'
if depot: if depot:
rc, stdout, stderr = module.run_command("%s -s %s %s | grep %s" % (cmd_list, pipes.quote(depot), pipes.quote(name), pipes.quote(name)), rc, stdout, stderr = module.run_command("%s -s %s %s | grep %s" % (cmd_list, shlex_quote(depot), shlex_quote(name), shlex_quote(name)),
use_unsafe_shell=True) use_unsafe_shell=True)
else: else:
rc, stdout, stderr = module.run_command("%s %s | grep %s" % (cmd_list, pipes.quote(name), pipes.quote(name)), use_unsafe_shell=True) rc, stdout, stderr = module.run_command("%s %s | grep %s" % (cmd_list, shlex_quote(name), shlex_quote(name)), use_unsafe_shell=True)
if rc == 0: if rc == 0:
version = re.sub(r"\s\s+|\t", " ", stdout).strip().split()[1] version = re.sub(r"\s\s+|\t", " ", stdout).strip().split()[1]
else: else:
@ -210,8 +212,6 @@ def main():
module.exit_json(changed=changed, name=name, state=state, msg=msg) module.exit_json(changed=changed, name=name, state=state, msg=msg)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -447,9 +447,10 @@ state:
''' '''
import os import os
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.six.moves import configparser from ansible.module_utils.six.moves import configparser
from ansible.module_utils._text import to_native
class YumRepo(object): class YumRepo(object):

@ -143,10 +143,13 @@ EXAMPLES = '''
runrefresh: yes runrefresh: yes
''' '''
REPO_OPTS = ['alias', 'name', 'priority', 'enabled', 'autorefresh', 'gpgcheck']
from distutils.version import LooseVersion from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule
REPO_OPTS = ['alias', 'name', 'priority', 'enabled', 'autorefresh', 'gpgcheck']
def _get_cmd(*args): def _get_cmd(*args):
"""Combines the non-interactive zypper command with arguments/subcommands""" """Combines the non-interactive zypper command with arguments/subcommands"""
@ -403,8 +406,6 @@ def main():
else: else:
module.fail_json(msg="Zypper failed with rc %s" % rc, rc=rc, stdout=stdout, stderr=stderr, repodata=repodata, state=state, warnings=warnings) module.fail_json(msg="Zypper failed with rc %s" % rc, rc=rc, stdout=stdout, stderr=stderr, repodata=repodata, state=state, warnings=warnings)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -17,7 +17,6 @@ wildcard_imports=$(find . -path ./test/runner/.tox -prune \
-o -path ./test/units/plugins/action/test_action.py \ -o -path ./test/units/plugins/action/test_action.py \
-o -path ./lib/ansible/compat/tests/mock.py -prune \ -o -path ./lib/ansible/compat/tests/mock.py -prune \
-o -path ./lib/ansible/compat/tests/unittest.py \ -o -path ./lib/ansible/compat/tests/unittest.py \
-o -path ./lib/ansible/modules/packaging/os -prune \
-o -name '*.py' -type f -exec grep -H 'import \*' '{}' '+') -o -name '*.py' -type f -exec grep -H 'import \*' '{}' '+')

Loading…
Cancel
Save