Debian Codegraph @

ansible_collections/community/okd/ci/doc_fragment_modules.py

raw
1 #!/usr/bin/env python
2
3 from __future__ import (absolute_import, division, print_function)
4 __metaclass__ = type
5
6 import ast
7 from pathlib import PosixPath
8 import yaml
9 import argparse
10 import os
11
12
13 def read_docstring(filename):
14
15 """
16 Search for assignment of the DOCUMENTATION and EXAMPLES variables in the given file.
17 Parse DOCUMENTATION from YAML and return the YAML doc or None together with EXAMPLES, as plain text.
18 """
19
20 data = {
21 'doc': None,
22 'plainexamples': None,
23 'returndocs': None,
24 'metadata': None, # NOTE: not used anymore, kept for compat
25 'seealso': None,
26 }
27
28 string_to_vars = {
29 'DOCUMENTATION': 'doc',
30 'EXAMPLES': 'plainexamples',
31 'RETURN': 'returndocs',
32 'ANSIBLE_METADATA': 'metadata', # NOTE: now unused, but kept for backwards compat
33 }
34
35 try:
36 with open(filename, 'rb') as b_module_data:
37 M = ast.parse(b_module_data.read())
38
39 for child in M.body:
40 if isinstance(child, ast.Assign):
41 for t in child.targets:
42 try:
43 theid = t.id
44 except AttributeError:
45 # skip errors can happen when trying to use the normal code
46 # sys.stderr.write("Failed to assign id for %s on %s, skipping\n" % (t, filename))
47 continue
48
49 if theid in string_to_vars:
50 varkey = string_to_vars[theid]
51 if isinstance(child.value, ast.Dict):
52 data[varkey] = ast.literal_eval(child.value)
53 else:
54 if theid != 'EXAMPLES':
55 # string should be yaml if already not a dict
56 data[varkey] = child.value.s
57
58 # sys.stderr.write('assigned: %s\n' % varkey)
59
60 except Exception:
61 # sys.stderr.write("unable to parse %s" % filename)
62 return
63
64 return yaml.safe_load(data["doc"]) if data["doc"] is not None else None
65
66
67 def is_extending_collection(result, col_fqcn):
68 if result:
69 for x in result.get("extends_documentation_fragment", []):
70 if x.startswith(col_fqcn):
71 return True
72 return False
73
74
75 def main():
76
77 parser = argparse.ArgumentParser(
78 description="list modules with inherited doc fragments from kubernetes.core that need rendering to deal with Galaxy/AH lack of functionality."
79 )
80 parser.add_argument(
81 "-c", "--collection-path", type=str, default=os.getcwd(), help="path to the collection"
82 )
83
84 args = parser.parse_args()
85
86 path = PosixPath(args.collection_path) / PosixPath("plugins/modules")
87 output = []
88 for d in path.iterdir():
89 if d.is_file():
90 result = read_docstring(str(d))
91 if is_extending_collection(result, "kubernetes.core."):
92 output.append(d.stem.replace(".py", ""))
93 print("\n".join(output))
94
95
96 if __name__ == '__main__':
97 main()