Debian Codegraph @

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

raw
1 """Analyze C# import statements."""
2
3 from __future__ import annotations
4
5 import os
6 import re
7
8 from ..io import (
9 open_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_csharp_module_utils_imports(powershell_targets: list[TestTarget], csharp_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_csharp_module_utils_imports(target.path, module_utils, False)
37
38 for target in csharp_targets:
39 imports_by_target_path[target.path] = extract_csharp_module_utils_imports(target.path, module_utils, True)
40
41 imports: dict[str, set[str]] = {module_util: set() for module_util in module_utils}
42
43 for target_path, modules in imports_by_target_path.items():
44 for module_util in modules:
45 imports[module_util].add(target_path)
46
47 for module_util in sorted(imports):
48 if not imports[module_util]:
49 display.warning('No imports found which use the "%s" module_util.' % module_util)
50
51 return imports
52
53
54 def get_csharp_module_utils_name(path: str) -> str:
55 """Return a namespace and name from the given module_utils path."""
56 base_path = data_context().content.module_utils_csharp_path
57
58 if data_context().content.collection:
59 prefix = 'ansible_collections.' + data_context().content.collection.prefix + 'plugins.module_utils.'
60 else:
61 prefix = ''
62
63 name = prefix + os.path.splitext(os.path.relpath(path, base_path))[0].replace(os.path.sep, '.')
64
65 return name
66
67
68 def enumerate_module_utils() -> set[str]:
69 """Return a set of available module_utils imports."""
70 return set(get_csharp_module_utils_name(p)
71 for p in data_context().content.walk_files(data_context().content.module_utils_csharp_path)
72 if os.path.splitext(p)[1] == '.cs')
73
74
75 def extract_csharp_module_utils_imports(path: str, module_utils: set[str], is_pure_csharp: bool) -> set[str]:
76 """Return a set of module_utils imports found in the specified source file."""
77 imports = set()
78 if is_pure_csharp:
79 pattern = re.compile(r'(?i)^using\s((?:Ansible|AnsibleCollections)\..+);$')
80 else:
81 pattern = re.compile(r'(?i)^#\s*ansiblerequires\s+-csharputil\s+((?:Ansible|ansible.collections|\.)\..+)')
82
83 with open_text_file(path) as module_file:
84 for line_number, line in enumerate(module_file, 1):
85 match = re.search(pattern, line)
86
87 if not match:
88 continue
89
90 import_name = resolve_csharp_ps_util(match.group(1), path)
91
92 if import_name in module_utils:
93 imports.add(import_name)
94 elif data_context().content.is_ansible or \
95 import_name.startswith('ansible_collections.%s' % data_context().content.prefix):
96 display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name))
97
98 return imports