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
|
#!/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 indices,
- dependencies for leaf pages (READMEs excluded):
OUTPUT/foo/bar.html: foo/bar.txt | OUTPUT/foo
- dependencies for READMEs:
OUTPUT/foo/index.html: foo/README.txt foo | OUTPUT/foo
- dependencies for autogenerated indices:
OUTPUT/foo/index.html: 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 = []
readmes = {}
for dpath, d in directories.items():
src_dir = pjoin(top_dir, dpath)
html_dir = pjoin(out_dir, dpath)
for f in d.files:
src_path = path.join(src_dir, f)
name, _ = path.splitext(f)
if name == 'README':
readmes[dpath] = f
continue
html_path = path.join(html_dir, name+'.html')
print(f'{html_path}: {src_path} | {html_dir}', file=deps_file)
pages.append(html_path)
print(file=deps_file)
for dpath in directories:
src_dir = pjoin(top_dir, dpath)
html_dir = pjoin(out_dir, dpath)
html_path = path.join(html_dir, 'index.html')
if dpath in readmes:
src_path = path.join(src_dir, readmes[dpath])
print(f'{html_path}: {src_path} {src_dir} | {html_dir}', file=deps_file)
continue
print(f'{html_path}: {src_dir} | {html_dir}', file=deps_file)
print(file=deps_file)
print(f'pages = {" ".join(pages)}', file=deps_file)
indices = (path.join(out_dir, dpath, 'index.html') for dpath in directories)
print(f'indices = {" ".join(indices)}', 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)
|