summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/sphinx.py
blob: 4d7afb621c69899a11eca56e82a6c3304f4a40c5 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import importlib
from pathlib import Path

from docutils import nodes
from docutils.parsers.rst import Directive
from mots.config import FileConfig
from mots.directory import Directory
from mots.export import export_to_format
from sphinx.util.docstrings import prepare_docstring
from sphinx.util.docutils import ReferenceRole


def function_reference(f, attr, args, doc):
    lines = []

    lines.extend(
        [
            f,
            "-" * len(f),
            "",
        ]
    )

    docstring = prepare_docstring(doc)

    lines.extend(
        [
            docstring[0],
            "",
        ]
    )

    arg_types = []

    for t in args:
        if isinstance(t, list):
            inner_types = [t2.__name__ for t2 in t]
            arg_types.append(" | ".join(inner_types))
            continue

        arg_types.append(t.__name__)

    arg_s = "(%s)" % ", ".join(arg_types)

    lines.extend(
        [
            ":Arguments: %s" % arg_s,
            "",
        ]
    )

    lines.extend(docstring[1:])
    lines.append("")

    return lines


def variable_reference(v, st_type, in_type, doc):
    lines = [
        v,
        "-" * len(v),
        "",
    ]

    docstring = prepare_docstring(doc)

    lines.extend(
        [
            docstring[0],
            "",
        ]
    )

    lines.extend(
        [
            ":Storage Type: ``%s``" % st_type.__name__,
            ":Input Type: ``%s``" % in_type.__name__,
            "",
        ]
    )

    lines.extend(docstring[1:])
    lines.append("")

    return lines


def special_reference(v, func, typ, doc):
    lines = [
        v,
        "-" * len(v),
        "",
    ]

    docstring = prepare_docstring(doc)

    lines.extend(
        [
            docstring[0],
            "",
            ":Type: ``%s``" % typ.__name__,
            "",
        ]
    )

    lines.extend(docstring[1:])
    lines.append("")

    return lines


def format_module(m):
    lines = []

    lines.extend(
        [
            ".. note::",
            "   moz.build files' implementation includes a ``Path`` class.",
        ]
    )
    path_docstring_minus_summary = prepare_docstring(m.Path.__doc__)[2:]
    lines.extend(["   " + line for line in path_docstring_minus_summary])

    for subcontext, cls in sorted(m.SUBCONTEXTS.items()):
        lines.extend(
            [
                ".. _mozbuild_subcontext_%s:" % subcontext,
                "",
                "Sub-Context: %s" % subcontext,
                "=============" + "=" * len(subcontext),
                "",
            ]
        )
        lines.extend(prepare_docstring(cls.__doc__))
        if lines[-1]:
            lines.append("")

        for k, v in sorted(cls.VARIABLES.items()):
            lines.extend(variable_reference(k, *v))

    lines.extend(
        [
            "Variables",
            "=========",
            "",
        ]
    )

    for v in sorted(m.VARIABLES):
        lines.extend(variable_reference(v, *m.VARIABLES[v]))

    lines.extend(
        [
            "Functions",
            "=========",
            "",
        ]
    )

    for func in sorted(m.FUNCTIONS):
        lines.extend(function_reference(func, *m.FUNCTIONS[func]))

    lines.extend(
        [
            "Special Variables",
            "=================",
            "",
        ]
    )

    for v in sorted(m.SPECIAL_VARIABLES):
        lines.extend(special_reference(v, *m.SPECIAL_VARIABLES[v]))

    return lines


def find_mots_config_path(app):
    """Find and return mots config path if it exists."""
    base_path = Path(app.srcdir).parent
    config_path = base_path / "mots.yaml"
    if config_path.exists():
        return config_path


def export_mots(config_path):
    """Load mots configuration and export it to file."""
    # Load from disk and initialize configuration and directory.
    config = FileConfig(config_path)
    config.load()
    directory = Directory(config)
    directory.load()

    # Fetch file format (i.e., "rst") and export path.
    frmt = config.config["export"]["format"]
    path = config_path.parent / config.config["export"]["path"]

    # Generate output.
    output = export_to_format(directory, frmt)

    # Create export directory if it does not exist.
    path.parent.mkdir(parents=True, exist_ok=True)

    # Write changes to disk.
    with path.open("w", encoding="utf-8") as f:
        f.write(output)


class MozbuildSymbols(Directive):
    """Directive to insert mozbuild sandbox symbol information."""

    required_arguments = 1

    def run(self):
        module = importlib.import_module(self.arguments[0])
        fname = module.__file__
        if fname.endswith(".pyc"):
            fname = fname[0:-1]

        self.state.document.settings.record_dependencies.add(fname)

        # We simply format out the documentation as rst then feed it back
        # into the parser for conversion. We don't even emit ourselves, so
        # there's no record of us.
        self.state_machine.insert_input(format_module(module), fname)

        return []


class Searchfox(ReferenceRole):
    """Role which links a relative path from the source to it's searchfox URL.

    Can be used like:

        See :searchfox:`browser/base/content/browser-places.js` for more details.

    Will generate a link to
    ``https://searchfox.org/mozilla-central/source/browser/base/content/browser-places.js``

    The example above will use the path as the text, to use custom text:

        See :searchfox:`this file <browser/base/content/browser-places.js>` for
        more details.

    To specify a different source tree:

        See :searchfox:`mozilla-beta:browser/base/content/browser-places.js`
        for more details.
    """

    def run(self):
        base = "https://searchfox.org/{source}/source/{path}"

        if ":" in self.target:
            source, path = self.target.split(":", 1)
        else:
            source = "mozilla-central"
            path = self.target

        url = base.format(source=source, path=path)

        if self.has_explicit_title:
            title = self.title
        else:
            title = path

        node = nodes.reference(self.rawtext, title, refuri=url, **self.options)
        return [node], []


def setup(app):
    from moztreedocs import manager

    app.add_directive("mozbuildsymbols", MozbuildSymbols)
    app.add_role("searchfox", Searchfox())

    # Unlike typical Sphinx installs, our documentation is assembled from
    # many sources and staged in a common location. This arguably isn't a best
    # practice, but it was the easiest to implement at the time.
    #
    # Here, we invoke our custom code for staging/generating all our
    # documentation.

    # Export and write "governance" documentation to disk.
    config_path = find_mots_config_path(app)
    if config_path:
        export_mots(config_path)

    manager.generate_docs(app)
    app.srcdir = manager.staging_dir