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
|
#!/usr/bin/env python3
"""Write dependencies for all website pages in makefile syntax.
We want to compute:
- a list of leaf pages,
- a list of auto-generated indices,
- dependencies for leaf pages:
OUTPUT/foo/bar.html: foo/bar.txt | OUTPUT/foo
- special case for READMEs:
OUTPUT/foo/index.html: foo/README.txt foo | OUTPUT/foo
"""
from os import path
from sys import argv, exit
from git import Repo
from helpers import compute_directories
def parse_arguments(args):
if len(args) != 3:
exit(f'Usage: {argv[0]} EXTENSIONS OUTPUT-DIR')
return argv[1].split(), argv[2]
def pjoin(directory, item):
return (
path.join(directory, item)
if item # Avoid trailing slash for top-level files.
else directory
)
def write_dependencies(deps_file, directories, top_dir, out_dir):
pages = list()
indices = list()
autoindices = list()
for dpath, d in directories.items():
autoindex = True
src_dir = pjoin(top_dir, dpath)
for f in d.files:
src_path = path.join(src_dir, f)
name, _ = path.splitext(f)
deps = [src_path]
target = pages
if name == 'README':
name = 'index'
deps.append(src_dir)
target = indices
autoindex = False
html_dir = pjoin(out_dir, dpath)
html_path = path.join(html_dir, name+'.html')
print(f'{html_path}: {" ".join(deps)} | {html_dir}', file=deps_file)
target.append(html_path)
if autoindex:
autoindices.append(
path.join(out_dir, dpath, 'index.html')
)
print(file=deps_file)
print(f'pages = {" ".join(pages)}', file=deps_file)
print(f'indices = {" ".join(indices)}', file=deps_file)
print(f'autoindices = {" ".join(autoindices)}', file=deps_file)
def main(arguments):
extensions, out_dir = parse_arguments(arguments)
repository = Repo(search_parent_directories=True)
top_dir = path.relpath(repository.working_dir, path.curdir)
directories = compute_directories(extensions, repository)
with open('deps.mk', 'w') as deps:
write_dependencies(deps, directories, top_dir, out_dir)
if __name__ == '__main__':
main(argv)
|