Wrap common write_atomic exceptions for easier error handling.
authorZac Medico <zmedico@gentoo.org>
Sun, 9 Jul 2006 20:18:59 +0000 (20:18 -0000)
committerZac Medico <zmedico@gentoo.org>
Sun, 9 Jul 2006 20:18:59 +0000 (20:18 -0000)
svn path=/main/trunk/; revision=3822

pym/portage_util.py

index a8f396c51925a9833ad649018f83d49b7fb01871..79babe05c5ac09c9dd6729599e9d9779240e3b3a 100644 (file)
@@ -707,13 +707,25 @@ class atomic_ofstream(file):
                        base_destructor()
 
 def write_atomic(file_path, content):
-       f = atomic_ofstream(file_path)
+       f = None
        try:
+               f = atomic_ofstream(file_path)
                f.write(content)
                f.close()
-       except IOError, ioe:
-               f.abort()
-               raise ioe
+       except (IOError, OSError), e:
+               if f:
+                       f.abort()
+               func_call = "write_atomic('%s')" % file_path
+               if e.errno == errno.EPERM:
+                       raise OperationNotPermitted(func_call)
+               elif e.errno == errno.EACCES:
+                       raise PermissionDenied(func_call)
+               elif e.errno == errno.EROFS:
+                       raise ReadOnlyFileSystem(func_call)
+               elif e.errno == errno.ENOENT:
+                       raise FileNotFound(file_path)
+               else:
+                       raise
 
 def ensure_dirs(dir_path, *args, **kwargs):
        """Create a directory and call apply_permissions.