summaryrefslogtreecommitdiffstats
path: root/test/sanity/code-smell/rstcheck.py
blob: 99917ca80ef502d8512866e7a814a2830cf33f6c (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
"""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()