-
Notifications
You must be signed in to change notification settings - Fork 46
/
generate_config.py
222 lines (181 loc) · 6.33 KB
/
generate_config.py
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
"""
- Fetches external repositories containing docs (listed in OUTSIDE_DOCS)
- Generate mkdocs.yml from mkdocs.yml.tpl
"""
import yaml
import json
import os
import sys
import re
import os.path as osp
from collections import OrderedDict, namedtuple
import fnmatch
import sh
def simple_glob(directory, glob_pattern):
matches = []
for root, dirnames, filenames in os.walk(directory, followlinks=True):
for filename in fnmatch.filter(filenames, glob_pattern):
matches.append(osp.join(root, filename))
return matches
def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
return yaml.load(stream, OrderedLoader)
def ordered_dump(data, stream=None, Dumper=yaml.Dumper, **kwds):
class OrderedDumper(Dumper):
pass
def _dict_representer(dumper, data):
return dumper.represent_mapping(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
data.items())
OrderedDumper.add_representer(OrderedDict, _dict_representer)
return yaml.dump(data, stream, OrderedDumper, **kwds)
leadingHash = re.compile(r'#+\s+')
def read_toc(directory):
def make_paths_absolute(tree):
for t in tree:
for k in t.keys():
if isinstance(t[k], str):
t[k] = re.sub('^.', directory, t[k])
else:
make_paths_absolute(t[k])
toc_path = osp.join('src', directory, 'toc.yml')
README_path = osp.join('src', directory, 'README.md')
if osp.exists(toc_path):
with open(toc_path) as f:
toc = ordered_load(f)
elif osp.exists(README_path):
toc = ordered_load('- README: ./README.md')
else:
return None
make_paths_absolute(toc)
return toc
def find_entry(tree, name):
try:
return [p for p in tree if p.get(name)][0][name]
except IndexError:
return None
def walk_dict(d, path=None):
if path is None:
path = []
it = None
if isinstance(d, list):
it = enumerate(iter(d))
elif isinstance(d, dict):
it = d.items()
if it:
for key, item in it:
path.append(key)
for key, leaf in walk_dict(item, path):
yield key, leaf
path.pop()
else:
yield path, d
def get_name(filename):
return '.'.join(osp.basename(filename).split('.')[:-1])
ExternalDoc = namedtuple('ExternalDoc', ['name', 'repository', 'doc_directory'])
def parse_external_doc_line(l):
return ExternalDoc(*(l.strip().split(' ')))
has_pulled = {}
def fetch_external_doc(repository, destination):
sh.rm('-rf', destination)
sh.mkdir('-p', destination)
with sh.pushd(destination):
if osp.exists('.git') and not has_pulled.get(repository):
sh.git('pull')
has_pulled[repository] = True
else:
sh.git('clone', repository, '--depth', '1', '.')
def fetch_all_external_docs_from_file(filename):
with open(filename) as f:
external_docs = [parse_external_doc_line(l) for l in f]
for name, repository, doc_directory in external_docs:
tmpdir = osp.join('/tmp', name)
print('Fetching %s...' % name)
fetch_external_doc(repository, tmpdir)
src_dir = osp.join('src', name)
sh.rm('-f', src_dir)
print('Linking %s...' % name)
sh.ln('-s', osp.join(tmpdir, doc_directory), src_dir)
def read_tocs(outside_doc_names):
tocs = {}
for dir in outside_doc_names:
abs = osp.join('./src', dir)
toc = read_toc(dir)
if toc:
tocs[dir] = toc
return tocs
def set_entry_content(obj, path, items):
cur = obj
for i, p in enumerate(path):
next = find_entry(cur, p)
if not next:
# Adding a new entry
if i == len(path) - 1:
print('Adding', len('items'), ' items to path ', path)
cur.append({ p: items })
return
else:
raise ValueError('Cannot find %s in %s (last crumb: %s)' % (path, obj, p))
break
else:
cur = next
if cur:
# Replacing all the content
del cur[:]
for item in items:
cur.append(item)
def flatten_entry_if_single(entries):
if len(entries) == 1:
return next(iter(entries[0].values()))
else:
return entries
def find_node(tree, finder):
for path_leaf in walk_dict(tree):
if finder(path_leaf):
return path_leaf
return None
def replace_toc_placeholders(nav, named_tocs):
for name, toc in named_tocs.items():
path_leaf = find_node(nav, lambda x: ('<%s>' % name) == x[1])
if path_leaf:
path, content = path_leaf
cur = nav
for tok in path[:-1]:
cur = cur[tok]
single_key = next(iter(cur.keys()))
cur[single_key] = flatten_entry_if_single(toc)
def main(argv):
OUTSIDE_DOCS = 'OUTSIDE_DOCS'
if '--fetch' in argv:
fetch_all_external_docs_from_file(OUTSIDE_DOCS)
with open('./mkdocs.yml.tpl') as f:
data = ordered_load(f, yaml.SafeLoader)
with open(OUTSIDE_DOCS) as f:
outside_docs_conf = [l.strip().split(' ') for l in f.readlines()]
outside_docs_conf = [
{'name': c[0], 'repo': c[1], 'subdir': c[2]}
for c in outside_docs_conf
]
outside_doc_names = [c['name'] for c in outside_docs_conf]
name_to_tocs = read_tocs(outside_doc_names)
nav = data['nav']
replace_toc_placeholders(data['nav'], name_to_tocs)
outside_docs_entry = {"outside_docs": outside_docs_conf}
if 'extra' not in data:
data['extra'] = {}
data['extra'] = dict(data['extra'], **outside_docs_entry)
with open('mkdocs.yml', 'w+') as f:
ordered_dump(data, f, indent=2, default_flow_style=False, Dumper=yaml.SafeDumper)
f.seek(0, 0)
content = f.read()
f.seek(0, 0)
warning = '#\n# THIS FILE IS AUTOMATICALLY GENERATED, PLEASE EDIT `mkdocs.yml.tpl` AND LAUNCH `python generate_config.py`\n#\n'
f.write(warning + content)
main(sys.argv)