[Zodb-checkins] CVS: ZODB3/ZODB - FileStorage.py:1.122

Barry Warsaw barry@wooz.org
Tue, 10 Dec 2002 16:40:45 -0500


Update of /cvs-repository/ZODB3/ZODB
In directory cvs.zope.org:/tmp/cvs-serv19002

Modified Files:
	FileStorage.py 
Log Message:
UndoError() constructor now takes its arguments in reverse order,
i.e. reason first, then oid.  This is because there's always a reason
but sometimes there isn't an oid (so we can default arg the oid).

cleanup(): Add both a method to FileStorage and a module global
function to clean up all the files that a FileStorage creates.
Sometimes it's handy to do a cleanup if you have the instance, other
times you only have the Data.fs file name.


=== ZODB3/ZODB/FileStorage.py 1.121 => 1.122 ===
--- ZODB3/ZODB/FileStorage.py:1.121	Fri Dec  6 18:04:36 2002
+++ ZODB3/ZODB/FileStorage.py	Tue Dec 10 16:40:45 2002
@@ -990,7 +990,7 @@
         h=read(DATA_HDR_LEN)
         roid,serial,sprev,stloc,vlen,splen = unpack(DATA_HDR, h)
         if roid != oid:
-            raise UndoError(oid, 'Invalid undo transaction id')
+            raise UndoError('Invalid undo transaction id', oid)
         if vlen:
             read(16) # skip nv pointer and version previous pointer
             version=read(vlen)
@@ -1054,7 +1054,7 @@
                 oid, ipos, tpos)
             # Versions of undone record and current record *must* match!
             if cver != version:
-                raise UndoError(oid, 'Current and undone versions differ')
+                raise UndoError('Current and undone versions differ', oid)
 
             if cdataptr != pos:
                 # We aren't sure if we are talking about the same data
@@ -1073,10 +1073,10 @@
                             # We bail if:
                             # - We don't have a previous record, which should
                             #   be impossible.
-                            raise UndoError(oid, "no previous record")
+                            raise UndoError("no previous record", oid)
                 except KeyError:
                     # LoadBack gave us a key error. Bail.
-                    raise UndoError(oid, "_loadBack() failed")
+                    raise UndoError("_loadBack() failed", oid)
 
         # Return the data that should be written in the undo record.
         if not pre:
@@ -1094,13 +1094,13 @@
             bdata = _loadBack(self._file, oid, p64(pre))[0]
         except KeyError:
             # couldn't find oid; what's the real explanation for this?
-            raise UndoError(oid, "_loadBack() failed for %s")
+            raise UndoError("_loadBack() failed for %s", oid)
         data=self.tryToResolveConflict(oid, cserial, serial, bdata, cdata)
 
         if data:
             return data, 0, version, snv, ipos
 
-        raise UndoError(oid, "Some data were modified by a later transaction")
+        raise UndoError("Some data were modified by a later transaction", oid)
 
     # undoLog() returns a description dict that includes an id entry.
     # The id is opaque to the client, but contains the transaction id.
@@ -1179,7 +1179,7 @@
                 # check the status field of the transaction header
                 if h[16] == 'p' or _tid < self._packt:
                     break
-        raise UndoError(None, "Invalid transaction id")
+        raise UndoError("Invalid transaction id")
 
     def _txn_undo_write(self, tpos):
         # a helper function to write the data records for transactional undo
@@ -1192,7 +1192,7 @@
         if h[16] == 'u':
             return
         if h[16] != ' ':
-            raise UndoError(None, 'non-undoable transaction')
+            raise UndoError('non-undoable transaction')
         tl = u64(h[8:16])
         ul, dl, el = struct.unpack(">HHH", h[17:TRANS_HDR_LEN])
         tend = tpos + tl
@@ -1245,7 +1245,7 @@
 
             pos += dlen
             if pos > tend:
-                raise UndoError(None, "non-undoable transaction")
+                raise UndoError("non-undoable transaction")
 
         if failures:
             raise MultipleUndoErrors(failures.items())
@@ -1772,6 +1772,11 @@
             raise CorruptedDataError(h)
         return h[8:]
 
+    def cleanup(self):
+        """Remove all files created by this storage."""
+        cleanup(self._file_name)
+
+
 def shift_transactions_forward(index, vindex, tindex, file, pos, opos):
     """Copy transactions forward in the data file
 
@@ -2472,3 +2477,13 @@
              'description': d}
         d.update(e)
         return d
+
+
+def cleanup(filename):
+    """Remove all FileStorage related files."""
+    for ext in '', '.old', '.tmp', '.lock', '.index', '.pack':
+        try:
+            os.remove(filename + ext)
+        except OSError, e:
+            if e.errno != errno.ENOENT:
+                raise