3 # Create DisplaySpecifiers LDIF (as a string) from the documents provided by
4 # Microsoft under the WSPP.
6 # Copyright (C) Andrew Kroeger <andrew@id10ts.net> 2009
8 # Based on ms_schema.py
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 def __read_folded_line(f
, buffer):
26 """Read a line from an LDIF file, unfolding it"""
35 # cannot fold an empty line
36 assert(line
!= "" and line
!= "\n")
46 # eof, definitely won't be folded
49 # marks end of a folded line
50 # line contains the now unfolded line
51 # buffer contains the start of the next possibly folded line
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-]*):")
61 def __read_raw_entries(f
):
62 """Read an LDIF entry, only unfolding lines"""
70 (l
, buffer) = __read_folded_line(f
, buffer)
75 if l
== "\n" or l
== "":
78 m
= attr_type_re
.match(l
)
86 print >>sys
.stderr
, "Invalid line: %s" % l
,
96 """Fix a string DN to use ${CONFIGDN}"""
98 if dn
.find("<Configuration NC Distinguished Name>") != -1:
99 dn
= dn
.replace("\n ", "")
100 return dn
.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
104 def __write_ldif_one(entry
):
105 """Write out entry as LDIF"""
110 out
.append("%s: %s" % (l
[0], l
[1]))
112 # This is a base64-encoded value
113 out
.append("%s:: %s" % (l
[0], l
[1]))
115 return "\n".join(out
)
117 def __transform_entry(entry
):
118 """Perform required transformations to the Microsoft-provided LDIF"""
125 if l
.find("::") != -1:
126 # This is a base64-encoded value
127 t
= l
.split(":: ", 1)
135 if key
== "changetype":
138 if key
== "distinguishedname":
141 if key
== "instancetype":
150 if key
== "objectcategory":
153 if key
== "showinadvancedviewonly":
154 value
= t
[1].upper().lstrip().rstrip()
156 # Remove showInAdvancedViewOnly attribute if it is set to the
157 # default value of TRUE
168 def read_ms_ldif(filename
):
169 """Read and transform Microsoft-provided LDIF file."""
173 f
= open(filename
, "rU")
174 for entry
in __read_raw_entries(f
):
175 out
.append(__write_ldif_one(__transform_entry(entry
)))
177 return "\n\n".join(out
) + "\n\n"
179 if __name__
== '__main__':
183 display_specifiers_file
= sys
.argv
[1]
185 print >>sys
.stderr
, "Usage: %s display-specifiers-ldif-file.txt" % (sys
.argv
[0])
188 print read_ms_ldif(display_specifiers_file
)