summaryrefslogtreecommitdiff
path: root/test/sanity/code-smell/docs-build.py
blob: f0071de85914a43d5cd018d44c4fd97045b61025 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from __future__ import annotations

import os
import re
import shutil
import subprocess
import sys
import tempfile


def main():
    base_dir = os.getcwd()

    keep_dirs = [
        'bin',
        'docs',
        'examples',
        'hacking',
        'lib',
        'packaging',
        'test/lib',
        'test/sanity',
    ]

    keep_files = [
        'MANIFEST.in',
        'pyproject.toml',
        'requirements.txt',
        'setup.cfg',
        'setup.py',
    ]

    # The tests write to the source tree, which isn't permitted for sanity tests.
    # To work around this a temporary copy is used.

    current_dir = os.getcwd()

    with tempfile.TemporaryDirectory(prefix='docs-build-', suffix='-sanity') as temp_dir:
        for keep_dir in keep_dirs:
            shutil.copytree(os.path.join(base_dir, keep_dir), os.path.join(temp_dir, keep_dir), symlinks=True)

        for keep_file in keep_files:
            shutil.copy2(os.path.join(base_dir, keep_file), os.path.join(temp_dir, keep_file))

        paths = os.environ['PATH'].split(os.pathsep)
        paths = [f'{temp_dir}/bin' if path == f'{current_dir}/bin' else path for path in paths]

        # Fix up the environment so everything runs from the temporary copy.
        os.environ['PATH'] = os.pathsep.join(paths)
        os.environ['PYTHONPATH'] = f'{temp_dir}/lib'
        os.chdir(temp_dir)

        run_test()


def run_test():
    base_dir = os.getcwd() + os.path.sep
    docs_dir = os.path.abspath('docs/docsite')

    cmd = ['make', 'core_singlehtmldocs']
    sphinx = subprocess.run(cmd, stdin=subprocess.DEVNULL, capture_output=True, cwd=docs_dir, check=False, text=True)

    stdout = sphinx.stdout
    stderr = sphinx.stderr

    if sphinx.returncode != 0:
        sys.stderr.write("Command '%s' failed with status code: %d\n" % (' '.join(cmd), sphinx.returncode))

        if stdout.strip():
            stdout = simplify_stdout(stdout)

            sys.stderr.write("--> Standard Output\n")
            sys.stderr.write("%s\n" % stdout.strip())

        if stderr.strip():
            sys.stderr.write("--> Standard Error\n")
            sys.stderr.write("%s\n" % stderr.strip())

        sys.exit(1)

    with open('docs/docsite/rst_warnings', 'r') as warnings_fd:
        output = warnings_fd.read().strip()
        lines = output.splitlines()

    known_warnings = {
        'block-quote-missing-blank-line': r'^Block quote ends without a blank line; unexpected unindent.$',
        'literal-block-lex-error': r'^Could not lex literal_block as "[^"]*". Highlighting skipped.$',
        'duplicate-label': r'^duplicate label ',
        'undefined-label': r'undefined label: ',
        'unknown-document': r'unknown document: ',
        'toc-tree-missing-document': r'toctree contains reference to nonexisting document ',
        'reference-target-not-found': r'[^ ]* reference target not found: ',
        'not-in-toc-tree': r"document isn't included in any toctree$",
        'unexpected-indentation': r'^Unexpected indentation.$',
        'definition-list-missing-blank-line': r'^Definition list ends without a blank line; unexpected unindent.$',
        'explicit-markup-missing-blank-line': r'Explicit markup ends without a blank line; unexpected unindent.$',
        'toc-tree-glob-pattern-no-match': r"^toctree glob pattern '[^']*' didn't match any documents$",
        'unknown-interpreted-text-role': '^Unknown interpreted text role "[^"]*".$',
    }

    for line in lines:
        match = re.search('^(?P<path>[^:]+):((?P<line>[0-9]+):)?((?P<column>[0-9]+):)? (?P<level>WARNING|ERROR): (?P<message>.*)$', line)

        if not match:
            path = 'docs/docsite/rst/index.rst'
            lineno = 0
            column = 0
            code = 'unknown'
            message = line

            # surface unknown lines while filtering out known lines to avoid excessive output
            print('%s:%d:%d: %s: %s' % (path, lineno, column, code, message))
            continue

        path = match.group('path')
        lineno = int(match.group('line') or 0)
        column = int(match.group('column') or 0)
        level = match.group('level').lower()
        message = match.group('message')

        path = os.path.abspath(path)

        if path.startswith(base_dir):
            path = path[len(base_dir):]

        if path.startswith('rst/'):
            path = 'docs/docsite/' + path  # fix up paths reported relative to `docs/docsite/`

        if level == 'warning':
            code = 'warning'

            for label, pattern in known_warnings.items():
                if re.search(pattern, message):
                    code = label
                    break
        else:
            code = 'error'

        print('%s:%d:%d: %s: %s' % (path, lineno, column, code, message))


def simplify_stdout(value):
    """Simplify output by omitting earlier 'rendering: ...' messages."""
    lines = value.strip().splitlines()

    rendering = []
    keep = []

    def truncate_rendering():
        """Keep last rendering line (if any) with a message about omitted lines as needed."""
        if not rendering:
            return

        notice = rendering[-1]

        if len(rendering) > 1:
            notice += ' (%d previous rendering line(s) omitted)' % (len(rendering) - 1)

        keep.append(notice)
        # Could change to rendering.clear() if we do not support python2
        rendering[:] = []

    for line in lines:
        if line.startswith('rendering: '):
            rendering.append(line)
            continue

        truncate_rendering()
        keep.append(line)

    truncate_rendering()

    result = '\n'.join(keep)

    return result


if __name__ == '__main__':
    main()