Use synchronous methods and cleaner results
authorDan Scott <dscott@laurentian.ca>
Wed, 11 Jul 2012 01:20:41 +0000 (21:20 -0400)
committerDan Scott <dscott@laurentian.ca>
Tue, 7 May 2013 18:57:51 +0000 (14:57 -0400)
Rather than calling ldap.result(), which results in awkward lists of
lists which are hard to handle, just iterate over the results from
ldap.search_s() (which, btw, is now a synchronous call).

Signed-off-by: Dan Scott <dscott@laurentian.ca>
tools/patron-load/ldap_osrf_sync

index dadd143..ba8e250 100755 (executable)
@@ -70,15 +70,15 @@ class User:
     Less formal than an actual 'au' object
     """
 
-    def __init__(self, raw_ldap):
+    def __init__(self, raw_cn, raw_atts):
         """
         Map core LDAP schema attributes to Evergreen user bits
 
         Easier to replace hard-coded mappings with calls to functions
         """
 
-        self.cname = raw_ldap[0]
-        self.ldap_atts = raw_ldap[1]
+        self.cname = raw_cn
+        self.ldap_atts = raw_atts
 
         self.cn = self._simple_map('cn')
         self.lang_pref = self._simple_map('preferredLanguage')
@@ -317,15 +317,14 @@ def find_ldap_users(con, ldap_filter, attributes, auth):
     results = []
 
     try:
-        result_id = con.search(base_dn, search_scope, ldap_filter, attributes)
-        while 1:
-            result_type, result_data = con.result(result_id, 0)
-            if result_data == []:
-                break
+        raw_results = con.search_ext_s(base_dn, search_scope, ldap_filter, attributes)
+        for raw_user, raw_atts in raw_results:
+            if raw_atts == []:
+                continue
 
-            user = User(result_data[0])
+            user = User(raw_user, raw_atts)
             if ARGS.dump_ldap:
-                dump_data(result_data)
+                dump_data(raw_atts)
             if ARGS.create_users:
                 res = create_evergreen_user(auth, user)
                 if res:
@@ -416,7 +415,7 @@ def find_evergreen_user(auth, user):
 
     return None
 
-def retrieve_evergreen_user(auth, user, uid):
+def retrieve_evergreen_user(auth, uid):
     """
     Update account for an existing user in Evergreen
 
@@ -454,7 +453,7 @@ def create_evergreen_user(auth, user):
 
     if found:
         print("Found: %s" % user.usrname)
-        egau = retrieve_evergreen_user(auth, user, found)
+        egau = retrieve_evergreen_user(auth, found)
         egau.ischanged(True)
         egau.active(True)
     else:
@@ -551,9 +550,8 @@ def dump_data(result_data):
     """
 
     print()
-    print(result_data[0][0])
-    for key in result_data[0][1]:
-        print(key, result_data[0][1][key])
+    for key in result_data:
+        print(key, result_data[key])
 
 def ldap_query(con, auth):
     """
@@ -693,7 +691,7 @@ def main(args=None):
             print >> sys.stderr, exc
         sys.exit()
     finally:
-        con.unbind()
+        con.unbind_ext_s()
 
     return results