# # This is just a code snippet showing how to migrate an OpenLDAP # schema to the LDIF format used by Fedora Directory Server. # # # Copyright (C) 2005 Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # def _convert_schema_to_ldif (self, schema_path, ldif_path): f = file (schema_path) attribute_types = [] object_classes = [] attr_str = "" obj_str = "" while True: line = f.readline () if not line: break def handle_line (list, s, line): line = line.strip () if line.startswith ("#"): return s s += " " + line if s.count ("(") == s.count (")"): list.append (s) s = "" return s if attr_str: attr_str = handle_line (attribute_types, attr_str, line) elif obj_str: obj_str = handle_line (object_classes, obj_str, line) elif line.startswith ("attributetype"): attr_str = line[len("attributetype"):].strip () elif line.startswith ("objectclass"): obj_str = line[len("objectclass"):].strip () f.close () temp = ldif_path + ".new" try: f = file (temp, "w") except: temp = None f = file (ldif_path, "w") try: f.write ("dn: cn=schema\n") for attr in attribute_types: f.write ("attributeTypes: %s\n" % attr) for obj in object_classes: f.write ("objectClasses: %s\n" % obj) f.close () except: if temp != None: os.remove (temp) raise os.rename (temp, ldif_path) def install_schema_file (self, schema_path): basename = os.path.basename (schema_path) if basename.endswith (".schema"): basename = basename[:-len (".schema")] schema_dir = os.path.join (FEDORA_DS_SERVER_ROOT, "slapd-" + FEDORA_DS_INSTANCE_ID, FEDORA_DS_SCHEMA_DIR); ldif_path = "" results = glob.glob (os.path.join (schema_dir, "[0-9][0-9]" + basename + ".ldif")) if len (results) > 0: ldif_path = results[0] else: num = 60 end = 99 while num <= end: results = glob.glob (os.path.join (schema_dir, "%d*.ldif" % num)) if len (results) == 0: break num += 1 if num > end: raise SchemaInstallError (_("Could not find an unused schema index in %(schema_dir)") % schema_dir) ldif_path = os.path.join (schema_dir, str (num) + basename + ".ldif") self._convert_schema_to_ldif (schema_path, ldif_path)