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

Block module ansible imports outside module_utils.

This commit is contained in:
Matt Clay 2018-10-11 16:23:23 -07:00
parent 761f99a4e3
commit 0c29463785

View file

@ -40,11 +40,19 @@ class BlacklistEntry(object):
return False return False
if self.modules_only: if self.modules_only:
return '/lib/ansible/modules/' in path or '/lib/ansible/module_utils/' in path return is_module_path(path)
return True return True
def is_module_path(path):
"""
:type path: str
:rtype: bool
"""
return '/lib/ansible/modules/' in path or '/lib/ansible/module_utils/' in path
class AnsibleBlacklistChecker(BaseChecker): class AnsibleBlacklistChecker(BaseChecker):
"""Checker for blacklisted imports and functions.""" """Checker for blacklisted imports and functions."""
__implements__ = (IAstroidChecker,) __implements__ = (IAstroidChecker,)
@ -54,6 +62,7 @@ class AnsibleBlacklistChecker(BaseChecker):
BAD_IMPORT = 'ansible-bad-import' BAD_IMPORT = 'ansible-bad-import'
BAD_IMPORT_FROM = 'ansible-bad-import-from' BAD_IMPORT_FROM = 'ansible-bad-import-from'
BAD_FUNCTION = 'ansible-bad-function' BAD_FUNCTION = 'ansible-bad-function'
BAD_MODULE_IMPORT = 'ansible-bad-module-import'
msgs = dict( msgs = dict(
E5101=('Import %s instead of %s', E5101=('Import %s instead of %s',
@ -65,6 +74,9 @@ class AnsibleBlacklistChecker(BaseChecker):
E5103=('Call %s instead of %s', E5103=('Call %s instead of %s',
BAD_FUNCTION, BAD_FUNCTION,
'Identifies functions which should not be used.'), 'Identifies functions which should not be used.'),
E5104=('Import external package or ansible.module_utils not %s',
BAD_MODULE_IMPORT,
'Identifies imports which should not be used.'),
) )
blacklist_imports = dict( blacklist_imports = dict(
@ -171,6 +183,8 @@ class AnsibleBlacklistChecker(BaseChecker):
:type node: astroid.node_classes.Import :type node: astroid.node_classes.Import
:type modname: str :type modname: str
""" """
self._check_module_import(node, modname)
entry = self.blacklist_imports.get(modname) entry = self.blacklist_imports.get(modname)
if not entry: if not entry:
@ -185,6 +199,8 @@ class AnsibleBlacklistChecker(BaseChecker):
:type modname: str :type modname: str
:type names: list[str[ :type names: list[str[
""" """
self._check_module_import(node, modname)
entry = self.blacklist_imports.get(modname) entry = self.blacklist_imports.get(modname)
if not entry: if not entry:
@ -194,6 +210,20 @@ class AnsibleBlacklistChecker(BaseChecker):
if entry.applies_to(self.linter.current_file, name[0]): if entry.applies_to(self.linter.current_file, name[0]):
self.add_message(self.BAD_IMPORT_FROM, args=(name[0], entry.alternative, modname), node=node) self.add_message(self.BAD_IMPORT_FROM, args=(name[0], entry.alternative, modname), node=node)
def _check_module_import(self, node, modname):
"""
:type node: astroid.node_classes.Import | astroid.node_classes.ImportFrom
:type modname: str
"""
if not is_module_path(self.linter.current_file):
return
if modname == 'ansible.module_utils' or modname.startswith('ansible.module_utils.'):
return
if modname == 'ansible' or modname.startswith('ansible.'):
self.add_message(self.BAD_MODULE_IMPORT, args=(modname,), node=node)
def register(linter): def register(linter):
"""required method to auto register this checker """ """required method to auto register this checker """