summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_util/controller/sanity/code-smell/action-plugin-docs.py
blob: a319d1a12e4ca237043efef991f0ecfd4b3b8f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Test to verify action plugins have an associated module to provide documentation."""
from __future__ import annotations

import os
import sys


def main():
    """Main entry point."""
    paths = sys.argv[1:] or sys.stdin.read().splitlines()

    module_names = set()

    module_prefixes = {
        'lib/ansible/modules/': True,
        'plugins/modules/': False,
    }

    action_prefixes = {
        'lib/ansible/plugins/action/': True,
        'plugins/action/': False,
    }

    for path in paths:
        full_name = get_full_name(path, module_prefixes)

        if full_name:
            module_names.add(full_name)

    for path in paths:
        full_name = get_full_name(path, action_prefixes)

        if full_name and full_name not in module_names:
            print('%s: action plugin has no matching module to provide documentation' % path)


def get_full_name(path, prefixes):
    """Return the full name of the plugin at the given path by matching against the given path prefixes, or None if no match is found."""
    for prefix, flat in prefixes.items():
        if path.startswith(prefix):
            relative_path = os.path.relpath(path, prefix)

            if flat:
                full_name = os.path.basename(relative_path)
            else:
                full_name = relative_path

            full_name = os.path.splitext(full_name)[0]

            name = os.path.basename(full_name)

            if name == '__init__':
                return None

            if name.startswith('_'):
                name = name[1:]

            full_name = os.path.join(os.path.dirname(full_name), name).replace(os.path.sep, '.')

            return full_name

    return None


if __name__ == '__main__':
    main()