#!/usr/bin/env python def main(fn_in, fn_out): ''' Import an LDIF file and re-export it to an LDIF file. Use the python ldap library to import and immediately re-export an ldif file. This was written as a test to understand that library better, but turns out to be useful to write not-so-compliant ldif files into RFC compliant ldif files, for example, if utf-8 characters have not been encoded properly. It takes two arguments, the existing and the new file. ''' import os import ldif filein = file(fn_in, 'r') # We make sure we don't overwrite an existing file. # We let exception rise on their own. fd = os.open(fn_out, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0666) fileout = os.fdopen(fd, 'w') ldif_content = ldif.LDIFRecordList(filein) ldif_content.parse() filein.close() ldif_out = ldif.LDIFWriter(fileout) for dn, record in ldif_content.all_records: ldif_out.unparse(dn, record) fileout.close() import sys if len(sys.argv) != 3: raise ValueError else: sys.exit(main(sys.argv[1], sys.argv[2]))