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/>.
23 def __read_folded_line(f, buffer):
24 """Read a line from an LDIF file, unfolding it"""
33 # cannot fold an empty line
34 assert(line != "" and line != "\n")
44 # eof, definitely won't be folded
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
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"""
68 (l, buffer) = __read_folded_line(f, buffer)
73 if l == "\n" or l == "":
76 m = attr_type_re.match(l)
84 print >>sys.stderr, "Invalid line: %s" % l,
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}")
102 def __write_ldif_one(entry):
103 """Write out entry as LDIF"""
108 out.append("%s: %s" % (l[0], l[1]))
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"""
123 if l.find("::") != -1:
124 # This is a base64-encoded value
125 t = l.split(":: ", 1)
133 if key == "changetype":
136 if key == "distinguishedname":
139 if key == "instancetype":
148 if key == "objectcategory":
151 if key == "showinadvancedviewonly":
152 value = t[1].upper().lstrip().rstrip()
154 # Remove showInAdvancedViewOnly attribute if it is set to the
155 # default value of TRUE
166 def read_ms_ldif(filename):
167 """Read and transform Microsoft-provided LDIF file."""
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__':
181 display_specifiers_file = sys.argv[1]
183 print >>sys.stderr, "Usage: %s display-specifiers-ldif-file.txt" % (sys.argv[0])
186 print read_ms_ldif(display_specifiers_file)