forked from kelbrown20/openshift-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_update_rest_api.py
executable file
·70 lines (51 loc) · 1.94 KB
/
_update_rest_api.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
#!/usr/bin/env python2
import collections
import os
import shutil
import yaml
import yaml.dumper
# maintain order of yaml dictionaries
def dict_representer(dumper, data):
return dumper.represent_dict(data.iteritems())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
yaml.add_representer(collections.OrderedDict, dict_representer)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
dict_constructor)
def main():
origin = os.environ.get("ORIGIN_REPO", "../origin")
# clear out all subdirectories under rest_api
for f in os.listdir("rest_api"):
f = os.path.join("rest_api", f)
if os.path.isdir(f):
shutil.rmtree(f)
# copy in subdirectories under rest_api from origin
for f in os.listdir(os.path.join(origin, "api/docs")):
srcp = os.path.join(origin, "api/docs", f)
dstp = os.path.join("rest_api", f)
if os.path.isdir(srcp):
shutil.copytree(srcp, dstp)
# read rest_api topics snippet from origin
with open(os.path.join(origin, "api/docs/_topic_map.yml")) as f:
topics = yaml.load(f)
# read in existing _topic_map.yml
preamble = ""
with open("_topic_map.yml") as f:
while True:
line = f.readline()
preamble += line
if line == "" or line.strip() == "---":
break
docs = list(yaml.load_all(f))
for doc in docs:
if doc["Dir"] == "rest_api":
# remove existing topics referencing subdirectories of rest_api
doc["Topics"] = [t for t in doc["Topics"] if "Dir" not in t]
# add rest_api topics snippet from origin
doc["Topics"].extend(topics)
# write out modified _topic_map.yml
with open("_topic_map.yml", "w") as f:
f.write(preamble)
yaml.dump_all(docs, f, default_flow_style=False)
if __name__ == "__main__":
main()