1
2 """
3 # DESCRIPTION:
4 # load/save a data to file by dumping its structure
5
6 Copyright (C) 2007-2009 Fabio Erculiani
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 """
22
23
24 from __future__ import with_statement
25 import os
26 from entropy.const import etpConst, const_setup_perms, const_setup_file
27 try:
28 import cPickle as pickle
29 except ImportError:
30 import pickle
31
32 D_EXT = etpConst['cachedumpext']
33 D_DIR = etpConst['dumpstoragedir']
34 E_GID = etpConst['entropygid']
35 if E_GID == None:
36 E_GID = 0
37
38
39 -def dumpobj(name, my_object, complete_path = False, ignore_exceptions = True):
40 """
41 Dump object to file
42
43 @param name -- name of the object
44 @param my_object -- object to dump
45 @param complete_path -- name is a complete path
46 @param ignore_exceptions -- ignore exceptions
47
48 @return None
49 """
50 while 1:
51 try:
52 if complete_path:
53 dmpfile = name
54 else:
55 dump_path = os.path.join(D_DIR, name)
56 dump_dir = os.path.dirname(dump_path)
57
58 my_dump_dir = dump_dir
59 d_paths = []
60 while not os.path.isdir(my_dump_dir):
61 d_paths.append(my_dump_dir)
62 my_dump_dir = os.path.dirname(my_dump_dir)
63 if d_paths:
64 d_paths = sorted(d_paths)
65 for d_path in d_paths:
66 os.mkdir(d_path)
67 const_setup_file(d_path, E_GID, 0775)
68
69 dmpfile = dump_path+D_EXT
70 with open(dmpfile,"wb") as dmp_f:
71 pickle.dump(my_object, dmp_f)
72 dmp_f.flush()
73 const_setup_file(dmpfile, E_GID, 0664)
74 except RuntimeError:
75 try:
76 os.remove(dmpfile)
77 except OSError:
78 pass
79 except (EOFError, IOError, OSError):
80 if not ignore_exceptions:
81 raise
82 break
83
85 """
86 Serialize object to ser_f (file)
87
88 @param myobj -- object to serialize
89 @type myobj -- any picklable object
90 @param ser_f -- file handle to write to
91 @type ser_f -- file object
92 @param do_seek -- move the file cursor to the beginning
93 @type do_seek -- bool
94 @return file object where data is stored to
95 """
96 pickle.dump(myobj, ser_f)
97 ser_f.flush()
98 if do_seek:
99 ser_f.seek(0)
100 return ser_f
101
103 """
104 Unserialize file to object (file)
105
106 @param serial_f -- file object to read the stream to
107 @type serial_f -- file object
108 @return object reconstructed
109 """
110 return pickle.load(serial_f)
111
113 """
114 Unserialize pickle string to object
115
116 @param mystring -- data stream in string form to reconstruct
117 @type mystring -- basestring
118 @return reconstructed object
119 """
120 return pickle.loads(mystring)
121
123 """
124 Serialize object to string
125
126 @param myobj -- object to serialize
127 @type myobj -- any picklable object
128 @return serialized string
129 """
130 return pickle.dumps(myobj)
131
132 -def loadobj(name, complete_path = False):
133 """
134 Load object from a file
135 @param name -- name of the object to load
136 @type name -- basestring
137 @param complete_path -- name is a complete serialized
138 object file path to load
139 @type complete_path -- basestring
140 @return object or None
141 """
142 while 1:
143 if complete_path:
144 dmpfile = name
145 else:
146 dump_path = os.path.join(D_DIR, name)
147
148
149 dmpfile = dump_path+D_EXT
150 if os.path.isfile(dmpfile) and os.access(dmpfile, os.R_OK):
151 try:
152 with open(dmpfile,"rb") as dmp_f:
153 obj = None
154 try:
155 obj = pickle.load(dmp_f)
156 except (ValueError, EOFError, IOError,
157 OSError, pickle.UnpicklingError):
158 pass
159 return obj
160 except (IOError, OSError,):
161 pass
162 break
163
165 """
166 Get dumped object mtime
167
168 @param name -- object name
169 @type name -- basestring
170 @return mtime -- integer
171 """
172 mtime = 0
173 dump_path = os.path.join(D_DIR, name+D_EXT)
174 if os.path.isfile(dump_path) and os.access(dump_path, os.R_OK):
175 mtime = os.path.getmtime(dump_path)
176 return int(mtime)
177
179 """
180 Remove cached object through its object name
181
182 @param name -- object name
183 @type name -- basestring
184 @return bool -- removed or not
185 """
186 filepath = D_DIR+"/"+name+D_EXT
187 if os.path.isfile(filepath) and os.access(filepath, os.W_OK):
188 os.remove(filepath)
189 return True
190 return False
191