2017-10-17 20:26:32 +02:00
|
|
|
"""Analyze powershell import statements."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-10-17 20:26:32 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2021-05-06 02:37:59 +02:00
|
|
|
from ..io import (
|
2020-02-04 20:21:53 +01:00
|
|
|
read_text_file,
|
|
|
|
)
|
|
|
|
|
2021-05-06 02:37:59 +02:00
|
|
|
from ..util import (
|
2017-10-17 20:26:32 +02:00
|
|
|
display,
|
|
|
|
)
|
|
|
|
|
2021-05-06 02:37:59 +02:00
|
|
|
from ..util_common import (
|
2020-04-01 01:01:18 +02:00
|
|
|
resolve_csharp_ps_util,
|
|
|
|
)
|
|
|
|
|
2021-05-06 02:37:59 +02:00
|
|
|
from ..data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2017-10-17 20:26:32 +02:00
|
|
|
|
|
|
|
def get_powershell_module_utils_imports(powershell_targets):
|
|
|
|
"""Return a dictionary of module_utils names mapped to sets of powershell file paths.
|
|
|
|
:type powershell_targets: list[TestTarget]
|
|
|
|
:rtype: dict[str, set[str]]
|
|
|
|
"""
|
|
|
|
|
|
|
|
module_utils = enumerate_module_utils()
|
|
|
|
|
|
|
|
imports_by_target_path = {}
|
|
|
|
|
|
|
|
for target in powershell_targets:
|
|
|
|
imports_by_target_path[target.path] = extract_powershell_module_utils_imports(target.path, module_utils)
|
|
|
|
|
|
|
|
imports = dict([(module_util, set()) for module_util in module_utils])
|
|
|
|
|
|
|
|
for target_path in imports_by_target_path:
|
|
|
|
for module_util in imports_by_target_path[target_path]:
|
|
|
|
imports[module_util].add(target_path)
|
|
|
|
|
|
|
|
for module_util in sorted(imports):
|
|
|
|
if not imports[module_util]:
|
|
|
|
display.warning('No imports found which use the "%s" module_util.' % module_util)
|
|
|
|
|
|
|
|
return imports
|
|
|
|
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
def get_powershell_module_utils_name(path): # type: (str) -> str
|
|
|
|
"""Return a namespace and name from the given module_utils path."""
|
|
|
|
base_path = data_context().content.module_utils_powershell_path
|
|
|
|
|
|
|
|
if data_context().content.collection:
|
2020-03-25 07:44:50 +01:00
|
|
|
prefix = 'ansible_collections.' + data_context().content.collection.prefix + 'plugins.module_utils.'
|
2019-07-23 04:24:48 +02:00
|
|
|
else:
|
|
|
|
prefix = ''
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.')
|
2019-07-23 04:24:48 +02:00
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
2017-10-17 20:26:32 +02:00
|
|
|
def enumerate_module_utils():
|
|
|
|
"""Return a list of available module_utils imports.
|
|
|
|
:rtype: set[str]
|
|
|
|
"""
|
2019-07-23 04:24:48 +02:00
|
|
|
return set(get_powershell_module_utils_name(p)
|
|
|
|
for p in data_context().content.walk_files(data_context().content.module_utils_powershell_path)
|
|
|
|
if os.path.splitext(p)[1] == '.psm1')
|
2017-10-17 20:26:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def extract_powershell_module_utils_imports(path, module_utils):
|
|
|
|
"""Return a list of module_utils imports found in the specified source file.
|
|
|
|
:type path: str
|
|
|
|
:type module_utils: set[str]
|
|
|
|
:rtype: set[str]
|
|
|
|
"""
|
|
|
|
imports = set()
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
code = read_text_file(path)
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-03-25 07:44:50 +01:00
|
|
|
if data_context().content.is_ansible and '# POWERSHELL_COMMON' in code:
|
2020-02-04 20:21:53 +01:00
|
|
|
imports.add('Ansible.ModuleUtils.Legacy')
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
lines = code.splitlines()
|
|
|
|
line_number = 0
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
for line in lines:
|
|
|
|
line_number += 1
|
2020-04-01 01:01:18 +02:00
|
|
|
match = re.search(r'(?i)^#\s*(?:requires\s+-module(?:s?)|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections|\.)\..+)', line)
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
if not match:
|
|
|
|
continue
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-04-01 01:01:18 +02:00
|
|
|
import_name = resolve_csharp_ps_util(match.group(1), path)
|
2017-10-17 20:26:32 +02:00
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
if import_name in module_utils:
|
|
|
|
imports.add(import_name)
|
2020-03-25 07:44:50 +01:00
|
|
|
elif data_context().content.is_ansible or \
|
|
|
|
import_name.startswith('ansible_collections.%s' % data_context().content.prefix):
|
2020-02-04 20:21:53 +01:00
|
|
|
display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name))
|
2017-10-17 20:26:32 +02:00
|
|
|
|
|
|
|
return imports
|