@ -14,8 +14,8 @@ ANSIBLE_TEST_MODULES_PATH = os.environ['ANSIBLE_TEST_MODULES_PATH']
ANSIBLE_TEST_MODULE_UTILS_PATH = os . environ [ ' ANSIBLE_TEST_MODULE_UTILS_PATH ' ]
class Blacklist Entry:
""" Defines a import blacklist entry ."""
class Unwanted Entry:
""" Defines a n unwanted import."""
def __init__ ( self , alternative , modules_only = False , names = None , ignore_paths = None ) :
"""
: type alternative : str
@ -58,11 +58,11 @@ def is_module_path(path):
return path . startswith ( ANSIBLE_TEST_MODULES_PATH ) or path . startswith ( ANSIBLE_TEST_MODULE_UTILS_PATH )
class Ansible Blacklist Checker( BaseChecker ) :
""" Checker for blacklis ted imports and functions."""
class Ansible Unwanted Checker( BaseChecker ) :
""" Checker for unwan ted imports and functions."""
__implements__ = ( IAstroidChecker , )
name = ' blacklist '
name = ' unwanted '
BAD_IMPORT = ' ansible-bad-import '
BAD_IMPORT_FROM = ' ansible-bad-import-from '
@ -84,20 +84,20 @@ class AnsibleBlacklistChecker(BaseChecker):
' Identifies imports which should not be used. ' ) ,
)
blacklist _imports = dict (
unwanted _imports = dict (
# Additional imports that we may want to start checking:
# boto= Blacklist Entry('boto3', modules_only=True),
# requests= Blacklist Entry('ansible.module_utils.urls', modules_only=True),
# urllib= Blacklist Entry('ansible.module_utils.urls', modules_only=True),
# boto= Unwanted Entry('boto3', modules_only=True),
# requests= Unwanted Entry('ansible.module_utils.urls', modules_only=True),
# urllib= Unwanted Entry('ansible.module_utils.urls', modules_only=True),
# see https://docs.python.org/2/library/urllib2.html
urllib2 = Blacklist Entry( ' ansible.module_utils.urls ' ,
urllib2 = Unwanted Entry( ' ansible.module_utils.urls ' ,
ignore_paths = (
' /lib/ansible/module_utils/urls.py ' ,
) ) ,
# see https://docs.python.org/3.7/library/collections.abc.html
collections = Blacklist Entry( ' ansible.module_utils.common._collections_compat ' ,
collections = Unwanted Entry( ' ansible.module_utils.common._collections_compat ' ,
ignore_paths = (
' /lib/ansible/module_utils/common/_collections_compat.py ' ,
) ,
@ -118,11 +118,11 @@ class AnsibleBlacklistChecker(BaseChecker):
) ) ,
)
blacklist _functions = {
unwanted _functions = {
# see https://docs.python.org/2/library/tempfile.html#tempfile.mktemp
' tempfile.mktemp ' : Blacklist Entry( ' tempfile.mkstemp ' ) ,
' tempfile.mktemp ' : Unwanted Entry( ' tempfile.mkstemp ' ) ,
' sys.exit ' : Blacklist Entry( ' exit_json or fail_json ' ,
' sys.exit ' : Unwanted Entry( ' exit_json or fail_json ' ,
ignore_paths = (
' /lib/ansible/module_utils/basic.py ' ,
' /lib/ansible/modules/async_wrapper.py ' ,
@ -130,7 +130,7 @@ class AnsibleBlacklistChecker(BaseChecker):
) ,
modules_only = True ) ,
' builtins.print ' : Blacklist Entry( ' module.log or module.debug ' ,
' builtins.print ' : Unwanted Entry( ' module.log or module.debug ' ,
ignore_paths = (
' /lib/ansible/module_utils/basic.py ' ,
' /lib/ansible/module_utils/common/removed.py ' ,
@ -163,7 +163,7 @@ class AnsibleBlacklistChecker(BaseChecker):
module = last_child . name
entry = self . blacklist _imports. get ( module )
entry = self . unwanted _imports. get ( module )
if entry and entry . names :
if entry . applies_to ( self . linter . current_file , node . attrname ) :
@ -183,7 +183,7 @@ class AnsibleBlacklistChecker(BaseChecker):
if not func :
continue
entry = self . blacklist _functions. get ( func )
entry = self . unwanted _functions. get ( func )
if entry and entry . applies_to ( self . linter . current_file ) :
self . add_message ( self . BAD_FUNCTION , args = ( entry . alternative , func ) , node = node )
@ -197,7 +197,7 @@ class AnsibleBlacklistChecker(BaseChecker):
"""
self . _check_module_import ( node , modname )
entry = self . blacklist _imports. get ( modname )
entry = self . unwanted _imports. get ( modname )
if not entry :
return
@ -213,7 +213,7 @@ class AnsibleBlacklistChecker(BaseChecker):
"""
self . _check_module_import ( node , modname )
entry = self . blacklist _imports. get ( modname )
entry = self . unwanted _imports. get ( modname )
if not entry :
return
@ -239,4 +239,4 @@ class AnsibleBlacklistChecker(BaseChecker):
def register ( linter ) :
""" required method to auto register this checker """
linter . register_checker ( Ansible Blacklist Checker( linter ) )
linter . register_checker ( Ansible Unwanted Checker( linter ) )