4
4
import os
5
5
import sys
6
6
import json
7
+ import time
7
8
import getopt
8
9
import subprocess
9
10
import urllib .error
16
17
MINTIMEOUT = 5
17
18
MAXTIMEOUT = 625
18
19
TIMEOUTMULTIPLE = 5
20
+ CACHE_FILENAME = "COmanage_Projects_cache.txt"
21
+ CACHE_LIFETIME_HOURS = 0.5
19
22
20
23
LDAP_AUTH_COMMAND = [
21
24
"awk" , "/ldap_default_authtok/ {print $3}" , "/etc/sssd/conf.d/0060_domain_CILOGON.ORG.conf" ,
@@ -275,14 +278,16 @@ def get_ldap_active_users(filter_group_name):
275
278
stdout = subprocess .PIPE
276
279
).stdout .decode ('utf-8' ).strip ()
277
280
278
- filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None else f"(&(isMemberOf={ filter_group_name } )(isMemberOf=CO:members:active))" )
281
+ filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None
282
+ else f"(&(isMemberOf={ filter_group_name } )(isMemberOf=CO:members:active))" )
279
283
280
284
ldap_active_users_command = LDAP_ACTIVE_USERS_COMMAND
281
285
ldap_active_users_command [LDAP_ACTIVE_USERS_COMMAND .index ("{auth}" )] = auth_str
282
286
ldap_active_users_command [LDAP_ACTIVE_USERS_COMMAND .index ("{filter}" )] = filter_str
283
287
284
288
active_users = subprocess .run (ldap_active_users_command , stdout = subprocess .PIPE ).stdout .decode ('utf-8' ).split ('\n ' )
285
- users = set (line .replace ("voPersonApplicationUID: " , "" ) if re .compile ("dn: voPerson*" ) else "" for line in active_users )
289
+ users = set (line .replace ("voPersonApplicationUID: " , "" ) if re .compile ("dn: voPerson*" )
290
+ else "" for line in active_users )
286
291
return users
287
292
288
293
@@ -299,9 +304,7 @@ def create_user_to_projects_map(project_to_user_map, active_users, osggids_to_na
299
304
return users_to_projects_map
300
305
301
306
302
- def get_co_api_data ():
303
- #TODO add cacheing for COManage API data
304
-
307
+ def get_groups_data_from_api ():
305
308
groups = get_osg_co_groups__map ()
306
309
project_osggids_to_name = dict ()
307
310
for id ,name in groups .items ():
@@ -310,6 +313,32 @@ def get_co_api_data():
310
313
return project_osggids_to_name
311
314
312
315
316
+ def get_co_api_data ():
317
+ try :
318
+ r = open (CACHE_FILENAME , "r" )
319
+ lines = r .readlines ()
320
+ if float (lines [0 ]) >= (time .time () - (60 * 60 * CACHE_LIFETIME_HOURS )):
321
+ entries = lines [1 :len (lines )]
322
+ project_osggids_to_name = dict ()
323
+ for entry in entries :
324
+ osggid_name_pair = entry .split (":" )
325
+ if len (osggid_name_pair ) == 2 :
326
+ project_osggids_to_name [osggid_name_pair [0 ]] = osggid_name_pair [1 ]
327
+ else :
328
+ raise OSError
329
+ except OSError :
330
+ with open (CACHE_FILENAME , "w" ) as w :
331
+ project_osggids_to_name = get_groups_data_from_api ()
332
+ print (time .time (), file = w )
333
+ for osggid , name in project_osggids_to_name .items ():
334
+ print (f"{ osggid } :{ name } " , file = w )
335
+ finally :
336
+ if r :
337
+ r .close ()
338
+
339
+ return project_osggids_to_name
340
+
341
+
313
342
def get_osguser_groups (filter_group_name = None ):
314
343
project_osggids_to_name = get_co_api_data ()
315
344
ldap_groups_members = get_ldap_group_members_data ()
0 commit comments