Debian Codegraph @

test/lib/ansible_test/_internal/classification/powershell.py

raw
1 """Analyze powershell import statements."""
2
3 from __future__ import annotations
4
5 import os
6 import re
7
8 from ..io import (
9 read_text_file,
10 )
11
12 from ..util import (
13 display,
14 )
15
16 from .common import (
17 resolve_csharp_ps_util,
18 )
19
20 from ..data import (
21 data_context,
22 )
23
24 from ..target import (
25 TestTarget,
26 )
27
28
29 def get_powershell_module_utils_imports(powershell_targets: list[TestTarget]) -> dict[str, set[str]]:
30 """Return a dictionary of module_utils names mapped to sets of powershell file paths."""
31 module_utils = enumerate_module_utils()
32
33 imports_by_target_path = {}
34
35 for target in powershell_targets:
36 imports_by_target_path[target.path] = extract_powershell_module_utils_imports(target.path, module_utils)
37
38 imports: dict[str, set[str]] = {module_util: set() for module_util in module_utils}
39
40 for target_path, modules in imports_by_target_path.items():
41 for module_util in modules:
42 imports[module_util].add(target_path)
43
44 for module_util in sorted(imports):
45 if not imports[module_util]:
46 display.warning('No imports found which use the "%s" module_util.' % module_util)
47
48 return imports
49
50
51 def get_powershell_module_utils_name(path: str) -> str:
52 """Return a namespace and name from the given module_utils path."""
53 base_path = data_context().content.module_utils_powershell_path
54
55 if data_context().content.collection:
56 prefix = 'ansible_collections.' + data_context().content.collection.prefix + 'plugins.module_utils.'
57 else:
58 prefix = ''
59
60 name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.')
61
62 return name
63
64
65 def enumerate_module_utils() -> set[str]:
66 """Return a set of available module_utils imports."""
67 return set(get_powershell_module_utils_name(p)
68 for p in data_context().content.walk_files(data_context().content.module_utils_powershell_path)
69 if os.path.splitext(p)[1] == '.psm1')
70
71
72 def extract_powershell_module_utils_imports(path: str, module_utils: set[str]) -> set[str]:
73 """Return a set of module_utils imports found in the specified source file."""
74 imports = set()
75
76 code = read_text_file(path)
77
78 if data_context().content.is_ansible and '# POWERSHELL_COMMON' in code:
79 imports.add('Ansible.ModuleUtils.Legacy')
80
81 lines = code.splitlines()
82 line_number = 0
83
84 for line in lines:
85 line_number += 1
86 match = re.search(r'(?i)^#\s*(?:requires\s+-modules?|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections|\.)\..+)', line)
87
88 if not match:
89 continue
90
91 import_name = resolve_csharp_ps_util(match.group(1), path)
92
93 if import_name in module_utils:
94 imports.add(import_name)
95 elif data_context().content.is_ansible or \
96 import_name.startswith('ansible_collections.%s' % data_context().content.prefix):
97 display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name))
98
99 return imports