diff options
Diffstat (limited to 'bitbake/lib/bb/persist_data.py')
| -rw-r--r-- | bitbake/lib/bb/persist_data.py | 48 |
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 | |||
| 26 | import os.path | 26 | import os.path |
| 27 | import sys | 27 | import sys |
| 28 | import warnings | 28 | import warnings |
| 29 | import bb.msg, bb.data, bb.utils | 29 | from bb.compat import total_ordering |
| 30 | from collections import Mapping | ||
| 30 | 31 | ||
| 31 | try: | 32 | try: |
| 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 | ||
| 46 | class SQLTable(collections.MutableMapping): | 48 | class 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): | |
| 122 | class 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 | ||
| 150 | class PersistData(object): | 131 | class 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 | ||
| 178 | def connect(database): | ||
| 179 | return sqlite3.connect(database, timeout=30, isolation_level=None) | ||
| 197 | 180 | ||
| 198 | def persist(d): | 181 | def 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) | ||
