/[gentoo-src]/portage/pym/portage_db_template.py
Gentoo

Contents of /portage/pym/portage_db_template.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download) (as text)
Wed Mar 9 09:10:18 2005 UTC (9 years, 6 months ago) by ferringb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +2 -2 lines
File MIME type: text/x-python
FILE REMOVED
banish the old cache classes to the attic.

1 # Copyright 2004 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-src/portage/pym/portage_db_template.py,v 1.13 2005/02/26 06:35:20 jstubbs Exp $
4 cvs_id_string="$Id: portage_db_template.py,v 1.13 2005/02/26 06:35:20 jstubbs Exp $"[5:-2]
5
6 import os.path,string
7 from portage_util import getconfig, ReadOnlyConfig
8 from portage_exception import CorruptionError
9
10 class database:
11 def __init__(self,path,category,dbkeys,uid,gid,config_path="/etc/portage/module_configs/"):
12 self.__cacheArray = [None, None, None]
13 self.__cacheKeyArray = [None, None, None]
14 self.__template_init_called = True
15 self.path = path
16 self.category = category
17 self.dbkeys = dbkeys
18 self.uid = uid
19 self.gid = gid
20
21 self.config = None
22 self.__load_config(config_path)
23
24 self.module_init()
25
26 def getModuleName(self):
27 return self.__module__+"."+self.__class__.__name__[:]
28
29 def __load_config(self,config_path):
30 config_file = config_path + "/" + self.getModuleName()
31 self.config = ReadOnlyConfig(config_file)
32
33 def __check_init(self):
34 try:
35 if self.__template_init_called:
36 pass
37 except SystemExit, e:
38 raise
39 except:
40 raise NotImplementedError("db_template.__init__ was overridden")
41
42 def check_key(self,key):
43 if (not key) or not isinstance(key, str):
44 raise KeyError, "No key provided. key: %s" % (key)
45
46 def clear(self):
47 for x in self.keys():
48 self.del_key(x)
49
50 def __addCache(self,key,val):
51 del self.__cacheArray[2]
52 self.__cacheArray.insert(0,val)
53 del self.__cacheKeyArray[2]
54 self.__cacheKeyArray.insert(0,key)
55
56 def __delCache(self,key):
57 i = self.__cacheKeyArray.index(key)
58 self.__cacheArray[i] = None
59 self.__cacheKeyArray[i] = None
60
61 def flushCache(self):
62 self.__cacheArray = [None, None, None]
63 self.__cacheKeyArray = [None, None, None]
64
65 def get_timestamp(self,key):
66 if key in self.__cacheKeyArray:
67 return self.__cacheArray[self.__cacheKeyArray.index(key)]["_mtime_"]
68 try:
69 d = self.__getitem__(key)
70 return d["_mtime_"]
71 except KeyError:
72 return None
73
74 def __getitem__(self,key):
75 if key in self.__cacheKeyArray:
76 i = self.__cacheKeyArray.index(key)
77 return self.__cacheArray[i]
78
79 self.check_key(key)
80 if self.has_key(key):
81 try:
82 values = self.get_values(key)
83 self.__addCache(key,values)
84 return values
85 except SystemExit, e:
86 raise
87 except Exception, e:
88 raise CorruptionError("Corruption detected when reading key '%s': %s" % (key,str(e)))
89 raise KeyError("Key not in db: '%s'" % (key))
90
91 def __setitem__(self,key,values):
92 self.check_key(key)
93 self.__addCache(key,values)
94 return self.set_values(key,values)
95
96 def __delitem__(self,key):
97 self.__delCache(key)
98 return self.del_key(key)
99
100 def has_key(self,key):
101 raise NotImplementedError("Method not defined")
102
103 def keys(self):
104 raise NotImplementedError("Method not defined")
105
106 def get_values(self,key):
107 raise NotImplementedError("Method not defined")
108
109 def set_values(self,key,val):
110 raise NotImplementedError("Method not defined")
111
112 def del_key(self,key):
113 raise NotImplementedError("Method not defined")
114
115 def sync(self):
116 raise NotImplementedError("Method not defined")
117
118 def close(self):
119 raise NotImplementedError("Method not defined")
120
121
122
123 def test_database(db_class,path,category,dbkeys,uid,gid):
124 if "_mtime_" not in dbkeys:
125 dbkeys+=["_mtime_"]
126 d = db_class(path,category,dbkeys,uid,gid)
127
128 print "Module: "+str(d.__module__)
129
130 # XXX: Need a way to do this that actually works.
131 for x in dir(database):
132 if x not in dir(d):
133 print "FUNCTION MISSING:",str(x)
134
135 list = d.keys()
136 if(len(list) == 0):
137 values = {}
138 for x in dbkeys:
139 values[x] = x[:]
140 values["_mtime_"] = "1079903037"
141 d.set_values("test-2.2.3-r1", values)
142 d.set_values("test-2.2.3-r2", values)
143 d.set_values("test-2.2.3-r3", values)
144 d.set_values("test-2.2.3-r4", values)
145
146 list = d.keys()
147 print "Key count:",len(list)
148
149 values = d.get_values(list[0])
150 print "value count:",len(values)
151
152 mykey = "foobar-1.2.3-r4"
153
154 d.check_key(mykey)
155 d.set_values(mykey, values)
156 d.sync()
157 del d
158
159 d = db_class(path,category,dbkeys,uid,gid)
160 new_vals = d.get_values(mykey)
161
162 if dbkeys and new_vals:
163 for x in dbkeys:
164 if x not in new_vals.keys():
165 print "---",x
166 for x in new_vals.keys():
167 if x not in dbkeys:
168 print "+++",x
169 else:
170 print "Mismatched:",dbkeys,new_vals
171
172 d.del_key(mykey)
173
174 print "Should be None:",d.get_values(mykey)
175
176 d.clear()
177
178 d.sync
179 d.close
180
181 del d
182
183 print "Done."

  ViewVC Help
Powered by ViewVC 1.1.20