ctdb-failover: omit "restrict" optimization keyword
[Samba.git] / python / samba / ms_display_specifiers.py
blobae48dce4ffbea510b1ed526739c1fa2e2090b0fc
1 # Create DisplaySpecifiers LDIF (as a string) from the documents provided by
2 # Microsoft under the WSPP.
4 # Copyright (C) Andrew Kroeger <andrew@id10ts.net> 2009
6 # Based on ms_schema.py
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 import re
24 def __read_folded_line(f, buffer):
25 """Read a line from an LDIF file, unfolding it"""
26 line = buffer
28 while True:
29 l = f.readline()
31 if l[:1] == " ":
32 # continued line
34 # cannot fold an empty line
35 assert(line != "" and line != "\n")
37 # preserves '\n '
38 line = line + l
39 else:
40 # non-continued line
41 if line == "":
42 line = l
44 if l == "":
45 # eof, definitely won't be folded
46 break
47 else:
48 # marks end of a folded line
49 # line contains the now unfolded line
50 # buffer contains the start of the next possibly folded line
51 buffer = l
52 break
54 return (line, buffer)
57 # Only compile regexp once.
58 # Will not match options after the attribute type.
59 attr_type_re = re.compile("^([A-Za-z][A-Za-z0-9-]*):")
62 def __read_raw_entries(f):
63 """Read an LDIF entry, only unfolding lines"""
65 buffer = ""
67 while True:
68 entry = []
70 while True:
71 (l, buffer) = __read_folded_line(f, buffer)
73 if l[:1] == "#":
74 continue
76 if l == "\n" or l == "":
77 break
79 m = attr_type_re.match(l)
81 if m:
82 if l[-1:] == "\n":
83 l = l[:-1]
85 entry.append(l)
86 else:
87 print("Invalid line: %s" % l, end=' ', file=sys.stderr)
88 sys.exit(1)
90 if len(entry):
91 yield entry
93 if l == "":
94 break
97 def fix_dn(dn):
98 """Fix a string DN to use ${CONFIGDN}"""
100 if dn.find("<Configuration NC Distinguished Name>") != -1:
101 dn = dn.replace("\n ", "")
102 return dn.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
103 else:
104 return dn
107 def __write_ldif_one(entry):
108 """Write out entry as LDIF"""
109 out = []
111 for l in entry:
112 if l[2] == 0:
113 out.append("%s: %s" % (l[0], l[1]))
114 else:
115 # This is a base64-encoded value
116 out.append("%s:: %s" % (l[0], l[1]))
118 return "\n".join(out)
121 def __transform_entry(entry):
122 """Perform required transformations to the Microsoft-provided LDIF"""
124 temp_entry = []
126 for l in entry:
127 t = []
129 if l.find("::") != -1:
130 # This is a base64-encoded value
131 t = l.split(":: ", 1)
132 t.append(1)
133 else:
134 t = l.split(": ", 1)
135 t.append(0)
137 key = t[0].lower()
139 if key == "changetype":
140 continue
142 if key == "distinguishedname":
143 continue
145 if key == "instancetype":
146 continue
148 if key == "name":
149 continue
151 if key == "cn":
152 continue
154 if key == "objectcategory":
155 continue
157 if key == "showinadvancedviewonly":
158 value = t[1].upper().lstrip().rstrip()
159 if value == "TRUE":
160 # Remove showInAdvancedViewOnly attribute if it is set to the
161 # default value of TRUE
162 continue
164 t[1] = fix_dn(t[1])
166 temp_entry.append(t)
168 entry = temp_entry
170 return entry
173 def read_ms_ldif(filename):
174 """Read and transform Microsoft-provided LDIF file."""
176 out = []
178 from io import open
179 with open(filename, "r", encoding='latin-1') as f:
180 for entry in __read_raw_entries(f):
181 out.append(__write_ldif_one(__transform_entry(entry)))
183 return "\n\n".join(out) + "\n\n"
186 if __name__ == '__main__':
187 import sys
189 try:
190 display_specifiers_file = sys.argv[1]
191 except IndexError:
192 print("Usage: %s display-specifiers-ldif-file.txt" % (sys.argv[0]), file=sys.stderr)
193 sys.exit(1)
195 print(read_ms_ldif(display_specifiers_file))