diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
commit | 8a754e0858d922e955e71b253c139e071ecec432 (patch) | |
tree | 527d16e74bfd1840c85efd675fdecad056c54107 /test/sanity/code-smell/rstcheck.py | |
parent | Initial commit. (diff) | |
download | ansible-core-8a754e0858d922e955e71b253c139e071ecec432.tar.xz ansible-core-8a754e0858d922e955e71b253c139e071ecec432.zip |
Adding upstream version 2.14.3.upstream/2.14.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/sanity/code-smell/rstcheck.py')
-rw-r--r-- | test/sanity/code-smell/rstcheck.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/sanity/code-smell/rstcheck.py b/test/sanity/code-smell/rstcheck.py new file mode 100644 index 0000000..99917ca --- /dev/null +++ b/test/sanity/code-smell/rstcheck.py @@ -0,0 +1,62 @@ +"""Sanity test using rstcheck and sphinx.""" +from __future__ import annotations + +import re +import subprocess +import sys + + +def main(): + paths = sys.argv[1:] or sys.stdin.read().splitlines() + + encoding = 'utf-8' + + ignore_substitutions = ( + 'br', + ) + + cmd = [ + sys.executable, + '-m', 'rstcheck', + '--report', 'warning', + '--ignore-substitutions', ','.join(ignore_substitutions), + ] + paths + + process = subprocess.run(cmd, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=False, + ) + + if process.stdout: + raise Exception(process.stdout) + + pattern = re.compile(r'^(?P<path>[^:]*):(?P<line>[0-9]+): \((?P<level>INFO|WARNING|ERROR|SEVERE)/[0-4]\) (?P<message>.*)$') + + results = parse_to_list_of_dict(pattern, process.stderr.decode(encoding)) + + for result in results: + print('%s:%s:%s: %s' % (result['path'], result['line'], 0, result['message'])) + + +def parse_to_list_of_dict(pattern, value): + matched = [] + unmatched = [] + + for line in value.splitlines(): + match = re.search(pattern, line) + + if match: + matched.append(match.groupdict()) + else: + unmatched.append(line) + + if unmatched: + raise Exception('Pattern "%s" did not match values:\n%s' % (pattern, '\n'.join(unmatched))) + + return matched + + +if __name__ == '__main__': + main() |