1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# persister: persist Python objects safely to pickle files
# 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
# Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# rawdog is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rawdog; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA, or see http://www.gnu.org/.
import cPickle as pickle
import errno
import fcntl
import os
import sys
class Persistable:
"""An object which can be persisted."""
def __init__(self):
self._modified = False
def modified(self, state=True):
"""Mark the object as having been modified (or not)."""
self._modified = state
def is_modified(self):
return self._modified
class Persisted:
"""Context manager for a persistent object. The object being persisted
must implement the Persistable interface."""
def __init__(self, klass, filename, persister):
self.klass = klass
self.filename = filename
self.persister = persister
self.lock_file = None
self.object = None
self.refcount = 0
def rename(self, new_filename):
"""Rename the persisted file. This works whether the file is
currently open or not."""
self.persister._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):
"""As open()."""
return self.open()
def __exit__(self, type, value, tb):
"""As close(), unless an exception occurred in which case do
nothing."""
if tb is None:
self.close()
def open(self, no_block=True):
"""Return the persistent object, loading it from its file if it
isn't already open. You must call close() once you're finished
with the object.
If no_block is True, then this will return None if loading the
object would otherwise block (i.e. if it's locked by another
process)."""
if self.refcount > 0:
# Already loaded.
self.refcount += 1
return self.object
try:
self._open(no_block)
except KeyboardInterrupt:
sys.exit(1)
except:
print "An error occurred while reading state from " + os.path.abspath(self.filename) + "."
print "This usually means the file is corrupt, and removing it will fix the problem."
sys.exit(1)
self.refcount = 1
return self.object
def _get_lock(self, no_block):
if not self.persister.use_locking:
return True
self.lock_file = open(self.filename + ".lock", "w+")
try:
mode = fcntl.LOCK_EX
if no_block:
mode |= fcntl.LOCK_NB
fcntl.lockf(self.lock_file.fileno(), mode)
except IOError, e:
if no_block and e.errno in (errno.EACCES, errno.EAGAIN):
return False
raise e
return True
def _open(self, no_block):
self.persister.log("Loading state file: ", self.filename)
if not self._get_lock(no_block):
return None
try:
f = open(self.filename, "rb")
except IOError:
# File can't be opened.
# Create a new object.
self.object = self.klass()
self.object.modified()
return
self.object = pickle.load(f)
self.object.modified(False)
f.close()
def close(self):
"""Reduce the reference count of the persisted object, saving
it back to its file if necessary."""
self.refcount -= 1
if self.refcount > 0:
# Still in use.
return
if self.object.is_modified():
self.persister.log("Saving state file: ", self.filename)
newname = "%s.new-%d" % (self.filename, os.getpid())
newfile = open(newname, "w")
pickle.dump(self.object, newfile, pickle.HIGHEST_PROTOCOL)
newfile.close()
os.rename(newname, self.filename)
if self.lock_file is not None:
self.lock_file.close()
self.persister._remove(self.filename)
class Persister:
"""Manage the collection of persisted files."""
def __init__(self, config):
self.files = {}
self.log = config.log
self.use_locking = config.locking
def get(self, klass, filename):
"""Get a context manager for a persisted file.
If the file is already open, this will return
the existing context manager."""
if filename in self.files:
return self.files[filename]
p = Persisted(klass, filename, self)
self.files[filename] = p
return p
def _rename(self, old_filename, new_filename):
self.files[new_filename] = self.files[old_filename]
del self.files[old_filename]
def _remove(self, filename):
del self.files[filename]
def delete(self, filename):
"""Delete a persisted file, along with its lock file,
if they exist."""
for ext in ("", ".lock"):
try:
os.unlink(filename + ext)
except OSError:
pass
|