Wireshark/Conformance.pm: Fix typo.
[Samba.git] / python / samba / ms_display_specifiers.py
blob44dfba07b39bbe448d0a18eb45fafe3a96a2df25
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
23 def __read_folded_line(f, buffer):
24 """Read a line from an LDIF file, unfolding it"""
25 line = buffer
27 while True:
28 l = f.readline()
30 if l[:1] == " ":
31 # continued line
33 # cannot fold an empty line
34 assert(line != "" and line != "\n")
36 # preserves '\n '
37 line = line + l
38 else:
39 # non-continued line
40 if line == "":
41 line = l
43 if l == "":
44 # eof, definitely won't be folded
45 break
46 else:
47 # marks end of a folded line
48 # line contains the now unfolded line
49 # buffer contains the start of the next possibly folded line
50 buffer = l
51 break
53 return (line, buffer)
55 # Only compile regexp once.
56 # Will not match options after the attribute type.
57 attr_type_re = re.compile("^([A-Za-z][A-Za-z0-9-]*):")
59 def __read_raw_entries(f):
60 """Read an LDIF entry, only unfolding lines"""
62 buffer = ""
64 while True:
65 entry = []
67 while True:
68 (l, buffer) = __read_folded_line(f, buffer)
70 if l[:1] == "#":
71 continue
73 if l == "\n" or l == "":
74 break
76 m = attr_type_re.match(l)
78 if m:
79 if l[-1:] == "\n":
80 l = l[:-1]
82 entry.append(l)
83 else:
84 print >>sys.stderr, "Invalid line: %s" % l,
85 sys.exit(1)
87 if len(entry):
88 yield entry
90 if l == "":
91 break
93 def fix_dn(dn):
94 """Fix a string DN to use ${CONFIGDN}"""
96 if dn.find("<Configuration NC Distinguished Name>") != -1:
97 dn = dn.replace("\n ", "")
98 return dn.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
99 else:
100 return dn
102 def __write_ldif_one(entry):
103 """Write out entry as LDIF"""
104 out = []
106 for l in entry:
107 if l[2] == 0:
108 out.append("%s: %s" % (l[0], l[1]))
109 else:
110 # This is a base64-encoded value
111 out.append("%s:: %s" % (l[0], l[1]))
113 return "\n".join(out)
115 def __transform_entry(entry):
116 """Perform required transformations to the Microsoft-provided LDIF"""
118 temp_entry = []
120 for l in entry:
121 t = []
123 if l.find("::") != -1:
124 # This is a base64-encoded value
125 t = l.split(":: ", 1)
126 t.append(1)
127 else:
128 t = l.split(": ", 1)
129 t.append(0)
131 key = t[0].lower()
133 if key == "changetype":
134 continue
136 if key == "distinguishedname":
137 continue
139 if key == "instancetype":
140 continue
142 if key == "name":
143 continue
145 if key == "cn":
146 continue
148 if key == "objectcategory":
149 continue
151 if key == "showinadvancedviewonly":
152 value = t[1].upper().lstrip().rstrip()
153 if value == "TRUE":
154 # Remove showInAdvancedViewOnly attribute if it is set to the
155 # default value of TRUE
156 continue
158 t[1] = fix_dn(t[1])
160 temp_entry.append(t)
162 entry = temp_entry
164 return entry
166 def read_ms_ldif(filename):
167 """Read and transform Microsoft-provided LDIF file."""
169 out = []
171 f = open(filename, "rU")
172 for entry in __read_raw_entries(f):
173 out.append(__write_ldif_one(__transform_entry(entry)))
175 return "\n\n".join(out) + "\n\n"
177 if __name__ == '__main__':
178 import sys
180 try:
181 display_specifiers_file = sys.argv[1]
182 except IndexError:
183 print >>sys.stderr, "Usage: %s display-specifiers-ldif-file.txt" % (sys.argv[0])
184 sys.exit(1)
186 print read_ms_ldif(display_specifiers_file)