summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/persist_data.py
diff options
context:
space:
mode:
authorChris Larson <chris_larson@mentor.com>2011-04-04 09:36:45 -0700
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-05-06 18:05:11 +0100
commit754d1c69839982b7cdd49839a398e688c0ad9a9b (patch)
treee848f86280222186e0cb7311afe2e55024192dd0 /bitbake/lib/bb/persist_data.py
parent824acff967ff74c0a678bf8accc4a514653f5783 (diff)
downloadpoky-754d1c69839982b7cdd49839a398e688c0ad9a9b.tar.gz
persist_data: implement comparison, same as dict
(Bitbake rev: 1190406c526c7bb7cf415867be83e0403812a7dd) Signed-off-by: Chris Larson <chris_larson@mentor.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/persist_data.py')
-rw-r--r--bitbake/lib/bb/persist_data.py48
1 files changed, 17 insertions, 31 deletions
diff --git a/bitbake/lib/bb/persist_data.py b/bitbake/lib/bb/persist_data.py
index c9e80ba967..0ed0cd28e7 100644
--- a/bitbake/lib/bb/persist_data.py
+++ b/bitbake/lib/bb/persist_data.py
@@ -26,7 +26,8 @@ import logging
26import os.path 26import os.path
27import sys 27import sys
28import warnings 28import warnings
29import bb.msg, bb.data, bb.utils 29from bb.compat import total_ordering
30from collections import Mapping
30 31
31try: 32try:
32 import sqlite3 33 import sqlite3
@@ -43,6 +44,7 @@ if hasattr(sqlite3, 'enable_shared_cache'):
43 sqlite3.enable_shared_cache(True) 44 sqlite3.enable_shared_cache(True)
44 45
45 46
47@total_ordering
46class SQLTable(collections.MutableMapping): 48class SQLTable(collections.MutableMapping):
47 """Object representing a table/domain in the database""" 49 """Object representing a table/domain in the database"""
48 def __init__(self, cursor, table): 50 def __init__(self, cursor, table):
@@ -105,6 +107,10 @@ class SQLTable(collections.MutableMapping):
105 for row in data: 107 for row in data:
106 yield row[0] 108 yield row[0]
107 109
110 def __lt__(self, other):
111 if not isinstance(other, Mapping):
112 raise NotImplemented
113
108 def iteritems(self): 114 def iteritems(self):
109 data = self._execute("SELECT * FROM %s;" % self.table) 115 data = self._execute("SELECT * FROM %s;" % self.table)
110 for row in data: 116 for row in data:
@@ -118,33 +124,8 @@ class SQLTable(collections.MutableMapping):
118 def has_key(self, key): 124 def has_key(self, key):
119 return key in self 125 return key in self
120 126
121 127 def clear(self):
122class SQLData(object): 128 self._execute("DELETE FROM %s;" % self.table)
123 """Object representing the persistent data"""
124 def __init__(self, filename):
125 bb.utils.mkdirhier(os.path.dirname(filename))
126
127 self.filename = filename
128 self.connection = sqlite3.connect(filename, timeout=5,
129 isolation_level=None)
130 self.cursor = self.connection.cursor()
131 self._tables = {}
132
133 def __getitem__(self, table):
134 if not isinstance(table, basestring):
135 raise TypeError("table argument must be a string, not '%s'" %
136 type(table))
137
138 if table in self._tables:
139 return self._tables[table]
140 else:
141 tableobj = self._tables[table] = SQLTable(self.cursor, table)
142 return tableobj
143
144 def __delitem__(self, table):
145 if table in self._tables:
146 del self._tables[table]
147 self.cursor.execute("DROP TABLE IF EXISTS %s;" % table)
148 129
149 130
150class PersistData(object): 131class PersistData(object):
@@ -194,14 +175,19 @@ class PersistData(object):
194 """ 175 """
195 del self.data[domain][key] 176 del self.data[domain][key]
196 177
178def connect(database):
179 return sqlite3.connect(database, timeout=30, isolation_level=None)
197 180
198def persist(d): 181def persist(domain, d):
199 """Convenience factory for construction of SQLData based upon metadata""" 182 """Convenience factory for SQLTable objects based upon metadata"""
183 import bb.data, bb.utils
200 cachedir = (bb.data.getVar("PERSISTENT_DIR", d, True) or 184 cachedir = (bb.data.getVar("PERSISTENT_DIR", d, True) or
201 bb.data.getVar("CACHE", d, True)) 185 bb.data.getVar("CACHE", d, True))
202 if not cachedir: 186 if not cachedir:
203 logger.critical("Please set the 'PERSISTENT_DIR' or 'CACHE' variable") 187 logger.critical("Please set the 'PERSISTENT_DIR' or 'CACHE' variable")
204 sys.exit(1) 188 sys.exit(1)
205 189
190 bb.utils.mkdirhier(cachedir)
206 cachefile = os.path.join(cachedir, "bb_persist_data.sqlite3") 191 cachefile = os.path.join(cachedir, "bb_persist_data.sqlite3")
207 return SQLData(cachefile) 192 connection = connect(cachefile)
193 return SQLTable(connection, domain)