scan only *.gid files in /etc/sfa/trusted_roots
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 13 Jul 2011 10:23:58 +0000 (12:23 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 13 Jul 2011 10:23:58 +0000 (12:23 +0200)
previous behaviour was to scan all files, which was error-prone

sfa/trust/trustedroot.py

index ec8d2f0..2cd4f3b 100644 (file)
@@ -1,44 +1,36 @@
-### $Id$
-### $URL$
+import os.path
+import glob
 
-import os
-
-from sfa.trust.gid import *
+from sfa.trust.gid import GID
 
 class TrustedRootList:
     def __init__(self, dir):
         self.basedir = dir
-        
-        # create the directory to hold the files
-        try:
+        # create the directory to hold the files, if not existing
+        if not os.path.isdir (self.basedir):
             os.makedirs(self.basedir)
-        # if the path already exists then pass
-        except OSError, (errno, strerr):
-            if errno == 17:
-                pass
 
     def add_gid(self, gid):
         fn = os.path.join(self.basedir, gid.get_hrn() + ".gid")
-
         gid.save_to_file(fn)
 
     def get_list(self):
         gid_list = []
-        file_list = os.listdir(self.basedir)
-        for gid_file in file_list:
-            fn = os.path.join(self.basedir, gid_file)
-            if os.path.isfile(fn):
-                gid = GID(filename = fn)
+        pattern=os.path.join(self.basedir,"*.gid")
+        gid_files = glob.glob(pattern)
+        for gid_file in gid_files:
+            # ignore non-files
+            if os.path.isfile(gid_file):
+                gid = GID(filename = gid_file)
                 gid_list.append(gid)
         return gid_list
 
     def get_file_list(self):
         gid_file_list = []
-        
-        file_list = os.listdir(self.basedir)
-        for gid_file in file_list:
-            fn = os.path.join(self.basedir, gid_file)
-            if os.path.isfile(fn):
-                gid_file_list.append(fn)
-
+        pattern=os.path.join(self.basedir,"*.gid")
+        gid_files = glob.glob(pattern)
+        for gid_file in gid_files:
+            # ignore non-files
+            if os.path.isfile(gid_file):
+                gid_file_list.append(gid_file)        
         return gid_file_list