writedict: use write_atomic for exceptions
authorZac Medico <zmedico@gentoo.org>
Wed, 25 May 2011 05:42:24 +0000 (22:42 -0700)
committerZac Medico <zmedico@gentoo.org>
Wed, 25 May 2011 05:42:24 +0000 (22:42 -0700)
Also, fix calling code to handle InvalidLocation exceptions.

pym/portage/dbapi/vartree.py
pym/portage/util/__init__.py

index 4173283c034d749514e1c720e3d4939a8ed43139..021191f92e96f9d694c7609ad484d1f9185acb2a 100644 (file)
@@ -3726,9 +3726,7 @@ class dblink(object):
                        cfgfiledict.pop("IGNORE", None)
                        try:
                                writedict(cfgfiledict, self.vartree.dbapi._conf_mem_file)
-                       except IOError as e:
-                               if e.errno != errno.ENOENT:
-                                       raise
+                       except InvalidLocation:
                                self.settings._init_dirs()
                                writedict(cfgfiledict, self.vartree.dbapi._conf_mem_file)
 
index 0dff25ff82443454cccc928976b08d029bf051a4..94e4451adeb41bab24319a3a273134dbc9c611f9 100644 (file)
@@ -492,20 +492,14 @@ def grablines(myfilename, recursive=0, remember_source_file=False):
 def writedict(mydict,myfilename,writekey=True):
        """Writes out a dict to a file; writekey=0 mode doesn't write out
        the key and assumes all values are strings, not lists."""
-       myfile = None
-       try:
-               myfile = atomic_ofstream(myfilename)
-               if not writekey:
-                       for x in mydict.values():
-                               myfile.write(x+"\n")
-               else:
-                       for x in mydict:
-                               myfile.write("%s %s\n" % (x, " ".join(mydict[x])))
-               myfile.close()
-       except IOError:
-               if myfile is not None:
-                       myfile.abort()
-               raise
+       lines = []
+       if not writekey:
+               for v in mydict.values():
+                       lines.append(v + "\n")
+       else:
+               for k, v in mydict.items():
+                       lines.append("%s %s\n" % (k, " ".join(v)))
+       write_atomic(myfilename, "".join(lines))
 
 def shlex_split(s):
        """