From 7cc11e4ad516a5aba0ede50a0ed3c16338e2b017 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 5 May 2016 12:17:18 -0500 Subject: [PATCH] mark requests and boto as blacklisted imports for new modules. Fixes #21 --- ansible_testing/modules.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/ansible_testing/modules.py b/ansible_testing/modules.py index d2a1f1f57d..bc2ef60d57 100644 --- a/ansible_testing/modules.py +++ b/ansible_testing/modules.py @@ -44,8 +44,15 @@ import yaml BLACKLIST_DIRS = frozenset(('.git', 'test', '.github')) INDENT_REGEX = re.compile(r'([\t]*)') BLACKLIST_IMPORTS = { - 'requests': ('requests import found, should use ' - 'ansible.module_utils.urls instead'), + 'requests': { + 'new_only': True, + 'msg': ('requests import found, should use ' + 'ansible.module_utils.urls instead') + }, + 'boto': { + 'new_only': True, + 'msg': ('boto import found, new modules should use boto3') + }, } @@ -194,21 +201,25 @@ class ModuleValidator(Validator): def _find_blacklist_imports(self): for child in self.ast.body: + names = [] if isinstance(child, ast.Import): - for name in child.names: - if name.name in BLACKLIST_IMPORTS: - self.errors.append(BLACKLIST_IMPORTS[name.name]) + names.extend(child.names) elif isinstance(child, ast.TryExcept): bodies = child.body for handler in child.handlers: bodies.extend(handler.body) for grandchild in bodies: if isinstance(grandchild, ast.Import): - for name in grandchild.names: - if name.name in BLACKLIST_IMPORTS: - self.errors.append( - BLACKLIST_IMPORTS[name.name] - ) + names.extend(grandchild.names) + for name in names: + if name.name in BLACKLIST_IMPORTS: + msg = BLACKLIST_IMPORTS[name.name]['msg'] + new_only = BLACKLIST_IMPORTS[name.name]['new_only'] + if self._is_new_module() and new_only: + self.errors.append(msg) + elif not new_only: + self.errors.append(msg) + def _find_module_utils(self, main): linenos = [] @@ -228,7 +239,6 @@ class ModuleValidator(Validator): linenos.append(child.lineno) for name in child.names: - print(name.name) if (isinstance(name, ast.alias) and name.name == 'basic'): found_basic = True