File: depfile.py

package info (click to toggle)
qemu 1%3A10.0.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 413,648 kB
  • sloc: ansic: 4,733,433; pascal: 114,769; python: 105,506; asm: 68,406; sh: 52,878; makefile: 27,469; perl: 18,778; cpp: 11,435; xml: 3,404; objc: 2,877; yacc: 2,505; php: 1,299; tcl: 1,296; lex: 1,110; sql: 71; awk: 43; sed: 35; javascript: 7
file content (69 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (6)
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
# coding=utf-8
#
# QEMU depfile generation extension
#
# Copyright (c) 2020 Red Hat, Inc.
#
# This work is licensed under the terms of the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

"""depfile is a Sphinx extension that writes a dependency file for
   an external build system"""

import os
import sphinx
import sys
from pathlib import Path

__version__ = '1.0'

def get_infiles(env):
    for x in env.found_docs:
        yield str(env.doc2path(x))
        yield from ((os.path.join(env.srcdir, dep)
                    for dep in env.dependencies[x]))
    for mod in sys.modules.values():
        if hasattr(mod, '__file__'):
            if mod.__file__:
                yield mod.__file__
    # this is perhaps going to include unused files:
    for static_path in env.config.html_static_path + env.config.templates_path:
        for path in Path(static_path).rglob('*'):
            yield str(path)

    # also include kdoc script
    yield str(env.config.kerneldoc_bin[1])


def write_depfile(app, exception):
    if exception:
        return

    env = app.env
    if not env.config.depfile:
        return

    # Using a directory as the output file does not work great because
    # its timestamp does not necessarily change when the contents change.
    # So create a timestamp file.
    if env.config.depfile_stamp:
        with open(env.config.depfile_stamp, 'w') as f:
            pass

    with open(env.config.depfile, 'w') as f:
        print((env.config.depfile_stamp or app.outdir) + ": \\", file=f)
        print(*get_infiles(env), file=f)
        for x in get_infiles(env):
            print(x + ":", file=f)


def setup(app):
    app.add_config_value('depfile', None, 'env')
    app.add_config_value('depfile_stamp', None, 'env')
    app.connect('build-finished', write_depfile)

    return dict(
        version = __version__,
        parallel_read_safe = True,
        parallel_write_safe = True
    )