def sync(self, data): # make a query from a "to match template" query = SyncBC.get_match_query(self.sync_section, data) if query is None: return Info("\nIdentity query: %s" % query) # try get a entry for the match query # retrieve only attributes to be changed from "to update template" # (this is mandatory but current attributes will be removed) entry = self.conn.getSingleResult(query, self.update_template.keys()) if not entry: Info("No entry found for query: %s" % query) return # set variables dn = entry.keys()[0] old_attributes = entry.values()[0] # process "to update template" new_attributes = self.update_template.copy() for key, value in new_attributes.items(): # attributes to be removed from LDAP if value is None: del new_attributes[key] continue # process binary attributes if key.split(";")[0] in self.binary_attrs: value = data[self.__get_template_id(value, len(data))] if value: new_attributes[key] = [value] else: del new_attributes[key] continue # process string attributes new_attributes[key] = Strings.replace_from_array(new_attributes.get(key), data, SyncBC.get_acquire_connection_encoding(self.sync_section)) if new_attributes[key].find("%") != -1: # keep old value or don't add to LDAP if old_attributes.has_key(key): new_attributes[key] = old_attributes[key] else: del new_attributes[key] else: new_attributes[key] = [new_attributes[key]] Info("DN for update: %s" % dn) Debug("Current attributes: %s" % old_attributes) Debug("Rules for update: %s" % new_attributes) self.conn.modify(dn, old_attributes, new_attributes)
def __init__(self, db_section, sync_section): self.c = ConfigDAO() self.db_section = db_section self.sync_section = sync_section self.conn = LdapDAO(self.c.config.get(db_section, "uri"), self.c.config.get(db_section, "username"), self.c.config.get(db_section, "password"), self.c.config.get(self.db_section, "basedn")) self.update_template = SyncBC.get_update_template(sync_section) self.binary_attrs = SyncBC.get_connection_binary_attrs(db_section) # rename binary attributes from "to update template" for key, value in self.update_template.items(): if key in self.binary_attrs: self.update_template["%s;binary" % key] = self.update_template[key] del self.update_template[key]
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. """ dbsync - A synchronization framework """ __author__ = "Reinaldo Gil Lima de Carvalho" __version__ = "0.1" from util.Message import Info from controller.SyncBC import SyncBC """ Defines de scope of the module dbsync.py. In this case, top-level scope, a similar "main program". It's not used as an imported module, otherwise a program that reads the standard input of the O.S. """ if __name__ == "__main__": for sync_section in SyncBC.get_sync_sections(): Info("Synchronizing '%s'..." % sync_section) a_backend = SyncBC.get_acquire_backend(sync_section) p_backend = SyncBC.get_persist_backend(sync_section) for data in a_backend.load(): data = SyncBC.convert(sync_section, data) p_backend.sync(data) p_backend.flush()