systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / util / sfaapi.py
index 9c3b5d3..136ae70 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -220,10 +219,9 @@ class SFAAPI(object):
 
             resources_urn = self._get_resources_urn(resources_hrn_new)
             rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, None, leases)
-            f = open("/tmp/rspec_input.rspec", "w")
-            f.truncate(0)
-            f.write(rspec)
-            f.close()
+            with open("/tmp/rspec_input.rspec", "w") as f:
+                f.truncate(0)
+                f.write(rspec)
             
             if not os.path.getsize("/tmp/rspec_input.rspec") > 0:
                 raise RuntimeError("Fail to create rspec file to allocate resource in slice %s" % slicename)
@@ -270,10 +268,9 @@ class SFAAPI(object):
                 # Re implementing urn from hrn because the library sfa-common doesn't work for wilabt
                 resources_urn = self._get_urn(resources_hrn_new)
                 rspec = self.rspec_proc.build_sfa_rspec(slicename, resources_urn, properties, leases)
-                f = open("/tmp/rspec_input.rspec", "w")
-                f.truncate(0)
-                f.write(rspec)
-                f.close()
+                with open("/tmp/rspec_input.rspec", "w") as f:
+                    f.truncate(0)
+                    f.write(rspec)
 
                 if not os.path.getsize("/tmp/rspec_input.rspec") > 0:
                     raise RuntimeError("Fail to create rspec file to allocate resources in slice %s" % slicename)