provision: Use the official MS 2008R2 schema by default
[Samba.git] / python / samba / ms_markdown.py
blobc695f8bd7f104053d03d1c9ed899594df96578bf
1 # Create schema.ldif from Github markdown
3 # Each LDF section in the markdown file then gets written to a corresponding
4 # .LDF output file.
6 # Copyright (C) Andrew Bartlett 2017
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 """Generate LDIF from Github documentation."""
23 import re
24 import os
25 import markdown
26 import xml.etree.ElementTree as ET
28 def innertext(tag):
29 return (tag.text or '') + \
30 ''.join(innertext(e) for e in tag) + \
31 (tag.tail or '')
33 def read_ms_markdown(in_file, out_folder):
34 """Read Github documentation-derived schema files."""
36 with open(in_file) as update_file:
37 # Remove any comments from the raw LDF files
38 html = markdown.markdown(re.sub(r'(?m)^# .*\n?', '', update_file.read()),
39 output_format='xhtml')
41 tree = ET.fromstring('<root>' + html + '</root>')
43 ldf = None
44 try:
45 for node in tree:
46 if node.tag == 'h3':
47 if ldf is not None:
48 ldf.close()
50 out_path = os.path.join(out_folder, innertext(node).strip())
51 ldf = open(out_path, 'w')
52 elif node.tag == 'p' and ldf is not None:
53 ldf.write(innertext(node).replace('```', '') + '\n')
54 finally:
55 if ldf is not None:
56 ldf.close()
58 if __name__ == '__main__':
59 import sys
61 out_folder = ''
63 if len(sys.argv) == 0:
64 print >>sys.stderr, "Usage: %s <Schema-Update.md> [<output folder>]" % (sys.argv[0])
65 sys.exit(1)
67 in_file = sys.argv[1]
68 if len(sys.argv) > 2:
69 out_folder = sys.argv[2]
71 read_ms_markdown(in_file, out_folder)