forked from 2600hz/kazoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-doc-schemas.sh
executable file
·73 lines (58 loc) · 1.92 KB
/
generate-doc-schemas.sh
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
#!/usr/bin/env python2
from __future__ import print_function
import os
import re
import sys
if len(sys.argv) < 2:
print('Usage: ' + sys.argv[0] + ' applications/crossbar/doc/ref/*.md', file=sys.stderr)
sys.exit(1)
def find_schema(txt):
found = re.findall('^#{2,} Schema\n\n[^>]*?(?=\n{3}\#{1,4}|\Z)', txt, re.MULTILINE | re.DOTALL)
return found[0]
def public_doc(ref_path):
ref_name = os.path.basename(ref_path)
if ref_name == "skel.md":
return "skip"
if os.path.basename(os.path.dirname(ref_path)) == "ref":
doc_root = os.path.dirname(os.path.dirname(ref_path))
else:
return "skip"
ref_name = os.path.basename(ref_path)
ref_to_doc = {
'api_auth.md': 'api_authentication.md',
'conferences.md': 'conference.md',
'user_auth.md': 'user_authentication.md',
'vmboxes.md': 'voicemail.md',
'whitelabel.md': 'whitelabeling.md'
}
return os.path.join(doc_root, ref_to_doc.get(ref_name, ref_name))
errors = 0
for refname in sys.argv[1::]:
docname = public_doc(refname)
if docname == "skip":
continue
if not os.path.isfile(docname):
print('Doc does not exist, please create', docname, '(from', refname, ')', file=sys.stderr)
errors += 1
continue
try:
with open(refname, 'r') as f:
schemas = find_schema(f.read())
except IndexError:
# print('No schemas found, ignoring', refname)
continue
with open(docname, 'r') as f:
whole_doc = f.read()
try:
outdated = find_schema(whole_doc)
except IndexError:
continue
updated = whole_doc.replace(outdated, schemas)
try:
with open(docname, 'w') as f:
f.write(updated)
except IndexError:
print('These missing schemas need to be manually added to', docname, file=sys.stderr)
print(schemas, file=sys.stderr)
errors += 1
sys.exit(errors)