Merge lp:~therve/landscape-client/load-old-persist-on-empty into lp:~landscape/landscape-client/trunk

Proposed by Thomas Herve
Status: Merged
Approved by: Alberto Donato
Approved revision: 344
Merged at revision: 344
Proposed branch: lp:~therve/landscape-client/load-old-persist-on-empty
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 97 lines (+59/-11)
2 files modified
landscape/lib/persist.py (+19/-11)
landscape/lib/tests/test_persist.py (+40/-0)
To merge this branch: bzr merge lp:~therve/landscape-client/load-old-persist-on-empty
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Jerry Seutter (community) Approve
Review via email: mp+67672@code.launchpad.net

Description of the change

I spotted the problem while auditing for #788605. Hopefully it will reduce the chances of it happening.

To post a comment you must log in.
Revision history for this message
Jerry Seutter (jseutter) wrote :

+1 looks good

review: Approve
Revision history for this message
Alberto Donato (ack) wrote :

Looks good, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/lib/persist.py'
2--- landscape/lib/persist.py 2011-06-23 23:45:58 +0000
3+++ landscape/lib/persist.py 2011-07-12 09:38:36 +0000
4@@ -106,14 +106,8 @@
5
6 def load(self, filepath):
7 """Load a persisted database."""
8- filepath = os.path.expanduser(filepath)
9- if not os.path.isfile(filepath):
10- raise PersistError("File not found: %s" % filepath)
11- if os.path.getsize(filepath) == 0:
12- return
13- try:
14- self._hardmap = self._backend.load(filepath)
15- except:
16+
17+ def load_old():
18 filepathold = filepath + ".old"
19 if (os.path.isfile(filepathold) and
20 os.path.getsize(filepathold) > 0):
21@@ -124,9 +118,23 @@
22 except:
23 raise PersistError("Broken configuration file at %s" %
24 filepathold)
25- else:
26- raise PersistError("Broken configuration file at %s" %
27- filepath)
28+ return True
29+ return False
30+
31+ filepath = os.path.expanduser(filepath)
32+ if not os.path.isfile(filepath):
33+ if load_old():
34+ return
35+ raise PersistError("File not found: %s" % filepath)
36+ if os.path.getsize(filepath) == 0:
37+ load_old()
38+ return
39+ try:
40+ self._hardmap = self._backend.load(filepath)
41+ except:
42+ if load_old():
43+ return
44+ raise PersistError("Broken configuration file at %s" % filepath)
45
46 def save(self, filepath=None):
47 """Save the persist to the given C{filepath}.
48
49=== modified file 'landscape/lib/tests/test_persist.py'
50--- landscape/lib/tests/test_persist.py 2011-07-05 05:09:11 +0000
51+++ landscape/lib/tests/test_persist.py 2011-07-12 09:38:36 +0000
52@@ -397,6 +397,46 @@
53 filename = self.makeFile("")
54 self.persist.load(filename)
55
56+ def test_load_empty_files_restore_backup(self):
57+ """
58+ If the current file is empty, it tries to load the old one if it
59+ exists.
60+ """
61+ filename = self.makeFile("")
62+ filename_old = filename + ".old"
63+
64+ self.persist.set("a", 1)
65+ self.persist.save(filename_old)
66+
67+ persist = self.build_persist()
68+ persist.load(filename)
69+
70+ self.assertEqual(persist.get("a"), 1)
71+
72+ def test_non_existing_raise_error(self):
73+ """
74+ Trying to load a file that doesn't exist result in a L{PersistError}.
75+ """
76+ persist = self.build_persist()
77+ self.assertRaises(PersistError, persist.load, "/nonexistent")
78+
79+ def test_non_existing_restore_backup(self):
80+ """
81+ If the file doesn't exist, it tries to load the old one if present and
82+ valid.
83+ """
84+ filename = self.makeFile("")
85+ filename_old = filename + ".old"
86+ os.unlink(filename)
87+
88+ self.persist.set("a", 1)
89+ self.persist.save(filename_old)
90+
91+ persist = self.build_persist()
92+ persist.load(filename)
93+
94+ self.assertEqual(persist.get("a"), 1)
95+
96
97 class PicklePersistTest(GeneralPersistTest, SaveLoadPersistTest):
98

Subscribers

People subscribed via source and target branches

to all changes: