1 # Copyright 2009 Google Inc. Released under the GPL v2
3 # This file contains the classes used for the known kernel versions persistent
6 import cPickle
, fcntl
, os
, tempfile
10 """Wrap a file item stored in a database."""
11 def __init__(self
, name
, size
, timestamp
):
12 assert type(size
) == int
13 assert type(timestamp
) == int
17 self
.timestamp
= timestamp
21 return ("database.item('%s', %d, %d)" %
22 (self
.name
, self
.size
, self
.timestamp
))
25 def __eq__(self
, other
):
26 if not isinstance(other
, item
):
29 return (self
.name
== other
.name
and self
.size
== other
.size
and
30 self
.timestamp
== other
.timestamp
)
33 def __ne__(self
, other
):
34 return not self
.__eq
__(other
)
37 class database(object):
39 This is an Abstract Base Class for the file items database, not strictly
40 needed in Python because of the dynamic nature of the language but useful
41 to document the expected common API of the implementations.
44 def get_dictionary(self
):
46 Should be implemented to open and read the persistent contents of
47 the database and return it as a key->value dictionary.
49 raise NotImplemented('get_dictionary not implemented')
52 def merge_dictionary(self
, values
):
54 Should be implemented to merge the "values" dictionary into the
55 database persistent contents (ie to update existent entries and to add
56 those that do not exist).
58 raise NotImplemented('merge_dictionary not implemented')
61 class dict_database(database
):
63 A simple key->value database that uses standard python pickle dump of
64 a dictionary object for persistent storage.
66 def __init__(self
, path
):
70 def get_dictionary(self
, _open_func
=open):
72 Return the key/value pairs as a standard dictionary.
75 fd
= _open_func(self
.path
, 'rb')
77 # no db file, considering as if empty dictionary
81 res
= cPickle
.load(fd
)
88 def _aquire_lock(self
):
89 fd
= os
.open(self
.path
+ '.lock', os
.O_RDONLY | os
.O_CREAT
)
92 fcntl
.flock(fd
, fcntl
.LOCK_EX
)
93 except Exception, err
:
100 def merge_dictionary(self
, values
):
102 Merge the contents of "values" with the current contents of the
108 # use file locking to make the read/write of the file atomic
109 lock_fd
= self
._aquire
_lock
()
111 # make sure we release locks in case of exceptions (in case the
112 # process dies the OS will release them for us)
114 contents
= self
.get_dictionary()
115 contents
.update(values
)
117 # use a tempfile/atomic-rename technique to not require
118 # synchronization for get_dictionary() calls and also protect
119 # against full disk file corruption situations
120 fd
, fname
= tempfile
.mkstemp(prefix
=os
.path
.basename(self
.path
),
121 dir=os
.path
.dirname(self
.path
))
122 write_file
= os
.fdopen(fd
, 'wb')
125 cPickle
.dump(contents
, write_file
,
126 protocol
=cPickle
.HIGHEST_PROTOCOL
)
130 # this is supposed to be atomic on POSIX systems
131 os
.rename(fname
, self
.path
)
136 # close() releases any locks on that fd