mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-22 05:51:43 -07:00
mark requests and boto as blacklisted imports for new modules. Fixes #21
This commit is contained in:
parent
9ce546d03e
commit
7cc11e4ad5
1 changed files with 21 additions and 11 deletions
|
@ -44,8 +44,15 @@ import yaml
|
||||||
BLACKLIST_DIRS = frozenset(('.git', 'test', '.github'))
|
BLACKLIST_DIRS = frozenset(('.git', 'test', '.github'))
|
||||||
INDENT_REGEX = re.compile(r'([\t]*)')
|
INDENT_REGEX = re.compile(r'([\t]*)')
|
||||||
BLACKLIST_IMPORTS = {
|
BLACKLIST_IMPORTS = {
|
||||||
'requests': ('requests import found, should use '
|
'requests': {
|
||||||
'ansible.module_utils.urls instead'),
|
'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):
|
def _find_blacklist_imports(self):
|
||||||
for child in self.ast.body:
|
for child in self.ast.body:
|
||||||
|
names = []
|
||||||
if isinstance(child, ast.Import):
|
if isinstance(child, ast.Import):
|
||||||
for name in child.names:
|
names.extend(child.names)
|
||||||
if name.name in BLACKLIST_IMPORTS:
|
|
||||||
self.errors.append(BLACKLIST_IMPORTS[name.name])
|
|
||||||
elif isinstance(child, ast.TryExcept):
|
elif isinstance(child, ast.TryExcept):
|
||||||
bodies = child.body
|
bodies = child.body
|
||||||
for handler in child.handlers:
|
for handler in child.handlers:
|
||||||
bodies.extend(handler.body)
|
bodies.extend(handler.body)
|
||||||
for grandchild in bodies:
|
for grandchild in bodies:
|
||||||
if isinstance(grandchild, ast.Import):
|
if isinstance(grandchild, ast.Import):
|
||||||
for name in grandchild.names:
|
names.extend(grandchild.names)
|
||||||
|
for name in names:
|
||||||
if name.name in BLACKLIST_IMPORTS:
|
if name.name in BLACKLIST_IMPORTS:
|
||||||
self.errors.append(
|
msg = BLACKLIST_IMPORTS[name.name]['msg']
|
||||||
BLACKLIST_IMPORTS[name.name]
|
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):
|
def _find_module_utils(self, main):
|
||||||
linenos = []
|
linenos = []
|
||||||
|
@ -228,7 +239,6 @@ class ModuleValidator(Validator):
|
||||||
linenos.append(child.lineno)
|
linenos.append(child.lineno)
|
||||||
|
|
||||||
for name in child.names:
|
for name in child.names:
|
||||||
print(name.name)
|
|
||||||
if (isinstance(name, ast.alias) and
|
if (isinstance(name, ast.alias) and
|
||||||
name.name == 'basic'):
|
name.name == 'basic'):
|
||||||
found_basic = True
|
found_basic = True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue