Parcourir la source

apply patch from ticket https://bind10.isc.org/ticket/184
(make temporary file with python tempfile module instead of manual with fixed name)


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1859 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen il y a 15 ans
Parent
commit
fe3e958331
1 fichiers modifiés avec 16 ajouts et 4 suppressions
  1. 16 4
      src/lib/python/isc/config/cfgmgr.py

+ 16 - 4
src/lib/python/isc/config/cfgmgr.py

@@ -25,6 +25,7 @@ import ast
 import pprint
 import os
 import copy
+import tempfile
 from isc.cc import data
 
 class ConfigManagerDataReadError(Exception):
@@ -84,24 +85,35 @@ class ConfigManagerData:
         """Writes the current configuration data to a file. If
            output_file_name is not specified, the file used in
            read_from_file is used."""
+        filename = None
         try:
-            tmp_filename = self.db_filename + ".tmp"
-            file = open(tmp_filename, 'w');
+            file = tempfile.NamedTemporaryFile(mode='w',
+                                               prefix="b10-config.db.",
+                                               dir=self.data_path,
+                                               delete=False)
+            filename = file.name
             pp = pprint.PrettyPrinter(indent=4)
             s = pp.pformat(self.data)
             file.write(s)
             file.write("\n")
             file.close()
             if output_file_name:
-                os.rename(tmp_filename, output_file_name)
+                os.rename(filename, output_file_name)
             else:
-                os.rename(tmp_filename, self.db_filename)
+                os.rename(filename, self.db_filename)
         except IOError as ioe:
             # TODO: log this (level critical)
             print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ioe))
+            # TODO: debug option to keep file?
         except OSError as ose:
             # TODO: log this (level critical)
             print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ose))
+        try:
+            if filename and os.path.exists(filename):
+                os.remove(filename)
+        except OSError:
+            # Ok if we really can't delete it anymore, leave it
+            pass
 
     def __eq__(self, other):
         """Returns True if the data contained is equal. data_path and