When renaming a splitstate file, don't fail if it doesn't exist.
authorAdam Sampson <ats@offog.org>
Sun, 26 Jan 2014 19:22:06 +0000 (19:22 +0000)
committerAdam Sampson <ats@offog.org>
Sun, 26 Jan 2014 19:22:06 +0000 (19:22 +0000)
This has just occurred to me when a feed got a redirect on its first
fetch.

Also rename the lock file along with the state file.

NEWS
rawdoglib/persister.py

diff --git a/NEWS b/NEWS
index 1513ab078b4424193e8075d6f1efff9314aaa57c..7be35f5d2b3a691fe585b82d9aa3a4e1046c7082 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,11 @@
 Make test-rawdog not depend on having a host it can test connection
 timeouts against, and add a -T option if you do have one.
 
+When renaming a feed's state file in splitstate mode, don't fail if the
+state file doesn't exist -- which can happen if we get a 301 response
+for a feed the first time we fetch it. Also rename the lock file along
+with the state file.
+
 Add some more comprehensive tests for the changeconfig option; in
 particular, test it more thoroughly with splitstate both on and off.
 
index bda6c756d737fc0c35e82da12db428805c27675b..40169da1115dc9b221acf35d368dc65e34ccfeec 100644 (file)
@@ -1,5 +1,5 @@
 # persister: persist Python objects safely to pickle files
-# Copyright 2003, 2004, 2005, 2013 Adam Sampson <ats@offog.org>
+# Copyright 2003, 2004, 2005, 2013, 2014 Adam Sampson <ats@offog.org>
 #
 # rawdog is free software; you can redistribute and/or modify it
 # under the terms of that license as published by the Free Software
@@ -52,7 +52,15 @@ class Persisted:
                currently open or not."""
 
                self.persister._rename(self.filename, new_filename)
-               os.rename(self.filename, new_filename)
+               for ext in ("", ".lock"):
+                       try:
+                               os.rename(self.filename + ext,
+                                         new_filename + ext)
+                       except OSError, e:
+                               # If the file doesn't exist (yet),
+                               # that's OK.
+                               if e.errno != errno.ENOENT:
+                                       raise e
                self.filename = new_filename
 
        def __enter__(self):
@@ -177,8 +185,8 @@ class Persister:
        def delete(self, filename):
                """Delete a persisted file, along with its lock file,
                if they exist."""
-               for fn in (filename, filename + ".lock"):
+               for ext in ("", ".lock"):
                        try:
-                               os.unlink(fn)
+                               os.unlink(filename + ext)
                        except OSError:
                                pass