summaryrefslogtreecommitdiff
path: root/repo/www/make-deps.py
blob: d6720e552d6a9b27c12345bcff85fd49e4e09b03 (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
#!/usr/bin/env python3

from os import path
from subprocess import run
from sys import argv, exit


def parse_arguments(args):
    if len(args) != 3:
        exit(f'Usage: {argv[0]} TOP-DIR OUTPUT-DIR')

    return argv[1], argv[2]


def find_sources(top_dir):
    p = run(
        ('find', top_dir, '-name', '*.md', '-o', '-name', '*.org'),
        capture_output=True, check=True, text=True
    )

    return p.stdout.splitlines()


def html_path(source_path, top_dir, out_dir):
    _, ext = path.splitext(source_path)
    return source_path.replace(top_dir, out_dir).replace(ext, '.html')


def write_dependencies(output, sources, top_dir, out_dir):
    pages = []
    directories = set()

    for src in sources:
        html = html_path(src, top_dir, out_dir)
        html_dir = path.dirname(html)

        print(f'{html}: {src} | {html_dir}', file=output)

        pages.append(html)
        directories.add(html_dir)

    print(file=output)
    print(f'pages = {" ".join(pages)}', file=output)
    print(f'folders = {" ".join(directories)}', file=output)


def main(argv):
    top_dir, out_dir = parse_arguments(argv)
    source_files = find_sources(top_dir)

    with open('deps.mk', 'w') as deps:
        write_dependencies(deps, source_files, top_dir, out_dir)


if __name__ == '__main__':
    main(argv)