mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Require some module_utils imports to be at the bottom
This commit is contained in:
parent
8ff644680d
commit
90c469d8ec
1 changed files with 26 additions and 5 deletions
|
@ -72,6 +72,17 @@ class ModuleValidator(Validator):
|
||||||
'__init__.py'))
|
'__init__.py'))
|
||||||
BLACKLIST = BLACKLIST_FILES.union(BLACKLIST_MODULES)
|
BLACKLIST = BLACKLIST_FILES.union(BLACKLIST_MODULES)
|
||||||
|
|
||||||
|
BOTTOM_IMPORTS = frozenset((
|
||||||
|
'ansible.module_utils.basic',
|
||||||
|
'ansible.module_utils.urls',
|
||||||
|
'ansible.module_utils.facts',
|
||||||
|
'ansible.module_utils.splitter',
|
||||||
|
'ansible.module_utils.known_hosts',
|
||||||
|
))
|
||||||
|
BOTTOM_IMPORTS_BLACKLIST = frozenset((
|
||||||
|
'command.py',
|
||||||
|
))
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
super(ModuleValidator, self).__init__()
|
super(ModuleValidator, self).__init__()
|
||||||
|
|
||||||
|
@ -107,6 +118,9 @@ class ModuleValidator(Validator):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _is_bottom_import_blacklisted(self):
|
||||||
|
return self.object_name in self.BOTTOM_IMPORTS_BLACKLIST
|
||||||
|
|
||||||
def _check_interpreter(self):
|
def _check_interpreter(self):
|
||||||
if not self.text.startswith('#!/usr/bin/python'):
|
if not self.text.startswith('#!/usr/bin/python'):
|
||||||
self.errors.append('Interpreter line is not "#!/usr/bin/python"')
|
self.errors.append('Interpreter line is not "#!/usr/bin/python"')
|
||||||
|
@ -129,13 +143,23 @@ class ModuleValidator(Validator):
|
||||||
'already provided by '
|
'already provided by '
|
||||||
'ansible.module_utils.basic')
|
'ansible.module_utils.basic')
|
||||||
|
|
||||||
def _find_module_utils(self):
|
def _find_module_utils(self, main):
|
||||||
linenos = []
|
linenos = []
|
||||||
for child in self.ast.body:
|
for child in self.ast.body:
|
||||||
found_module_utils_import = False
|
found_module_utils_import = False
|
||||||
if isinstance(child, ast.ImportFrom):
|
if isinstance(child, ast.ImportFrom):
|
||||||
if child.module.startswith('ansible.module_utils.'):
|
if child.module.startswith('ansible.module_utils.'):
|
||||||
found_module_utils_import = True
|
found_module_utils_import = True
|
||||||
|
|
||||||
|
if child.module in self.BOTTOM_IMPORTS:
|
||||||
|
if (child.lineno < main - 10 and
|
||||||
|
not self._is_bottom_import_blacklisted()):
|
||||||
|
self.errors.append('%s import not near main()' %
|
||||||
|
child.module)
|
||||||
|
else:
|
||||||
|
self.warnings.append('%s import not near main()' %
|
||||||
|
child.module)
|
||||||
|
|
||||||
linenos.append(child.lineno)
|
linenos.append(child.lineno)
|
||||||
|
|
||||||
if not child.names:
|
if not child.names:
|
||||||
|
@ -242,11 +266,8 @@ class ModuleValidator(Validator):
|
||||||
self._check_for_sys_exit()
|
self._check_for_sys_exit()
|
||||||
self._check_for_gpl3_header()
|
self._check_for_gpl3_header()
|
||||||
self._find_json_import()
|
self._find_json_import()
|
||||||
module_utils = self._find_module_utils()
|
|
||||||
main = self._find_main_call()
|
main = self._find_main_call()
|
||||||
for mu in module_utils:
|
module_utils = self._find_module_utils(main)
|
||||||
if mu < main - 10:
|
|
||||||
self.warnings.append('module_utils import not near main()')
|
|
||||||
|
|
||||||
self._find_has_import()
|
self._find_has_import()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue