hopefully fewer leaks in files : always use a context manager to open files
[nodemanager.git] / plugins / vsys_privs.py
index 62b84a0..bf3b1d1 100755 (executable)
@@ -1,6 +1,3 @@
-# $Id$
-# $URL$
-
 """
 vsys sub-configurator.  Maintains configuration parameters associated with vsys scripts.
 All slice attributes with the prefix vsys_ are written into configuration files on the
@@ -9,15 +6,14 @@ node for the reference of vsys scripts.
 
 import logger
 import os
-from sets import Set
 
 VSYS_PRIV_DIR = "/etc/planetlab/vsys-attributes"
 
-def start(options, conf):
-    logger.log("vsys_privs plugin v0.1")
+def start():
+    logger.log("vsys_privs: plugin starting")
     if (not os.path.exists(VSYS_PRIV_DIR)):
         os.makedirs(VSYS_PRIV_DIR)
-        logger.log("Created vsys attributes dir")
+        logger.log("vsys_privs: Created vsys attributes dir")
 
 def GetSlivers(data, config=None, plc=None):
 
@@ -68,11 +64,12 @@ def read_privs():
                 pass
 
             for tagname in tagnames:
-                tagfile = os.path.join(slicedir,tagname)
-                values_n = file(tagfile).readlines()
-                values = map(lambda s:s.rstrip(),values_n)
-                slice = os.path.basename(slicedir)
-                cur_privs[slice][tagname]=values
+                tagfilename = os.path.join(slicedir,tagname)
+                with open(tagfilename) as tagfile:
+                    values_n = tagfile.readlines()
+                    values = [ v.rstrip() for v in values_n ]
+                    slice = os.path.basename(slicedir)
+                    cur_privs[slice][tagname] = values
 
     return cur_privs
 
@@ -86,7 +83,7 @@ def write_privs(cur_privs,privs):
         # Add values that do not exist
         for k in variables.keys():
             v = variables[k]
-            if (cur_privs.has_key(slice) 
+            if (cur_privs.has_key(slice)
                     and cur_privs[slice].has_key(k)
                     and cur_privs[slice][k] == v):
                 # The binding has not changed
@@ -97,9 +94,9 @@ def write_privs(cur_privs,privs):
                 data = '\n'.join(v)
                 f.write(data)
                 f.close()
-                logger.log("Added vsys attribute %s for %s"%(k,slice))
+                logger.log("vsys_privs: added vsys attribute %s for %s"%(k,slice))
 
-    # Remove files and directories 
+    # Remove files and directories
     # that are invalid
     for slice in cur_privs.keys():
         variables = cur_privs[slice]
@@ -107,19 +104,19 @@ def write_privs(cur_privs,privs):
 
         # Add values that do not exist
         for k in variables.keys():
-            if (privs.has_key(slice) 
+            if (privs.has_key(slice)
                     and cur_privs[slice].has_key(k)):
                 # ok, spare this tag
-                print "Sparing  %s, %s "%(slice,k) 
+                print "Sparing  %s, %s "%(slice,k)
             else:
                 v_file = os.path.join(slice_dir, k)
-                os.remove(v_file)    
+                os.remove(v_file)
 
         if (not privs.has_key(slice)):
             os.rmdir(slice_dir)
 
 
-if __name__ == "__main__":           
+if __name__ == "__main__":
     test_slivers = {'slivers':[
         {'name':'foo','attributes':[
             {'tagname':'vsys_m','value':'2'},