diff options
author | Chris Larson <chris_larson@mentor.com> | 2011-04-04 09:36:45 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-05-06 18:05:11 +0100 |
commit | 754d1c69839982b7cdd49839a398e688c0ad9a9b (patch) | |
tree | e848f86280222186e0cb7311afe2e55024192dd0 /bitbake/lib/bb/persist_data.py | |
parent | 824acff967ff74c0a678bf8accc4a514653f5783 (diff) | |
download | openembedded-core-contrib-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.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 import os.path import sys import warnings -import bb.msg, bb.data, bb.utils +from bb.compat import total_ordering +from collections import Mapping try: import sqlite3 @@ -43,6 +44,7 @@ if hasattr(sqlite3, 'enable_shared_cache'): sqlite3.enable_shared_cache(True) +@total_ordering class SQLTable(collections.MutableMapping): """Object representing a table/domain in the database""" def __init__(self, cursor, table): @@ -105,6 +107,10 @@ class SQLTable(collections.MutableMapping): for row in data: yield row[0] + def __lt__(self, other): + if not isinstance(other, Mapping): + raise NotImplemented + def iteritems(self): data = self._execute("SELECT * FROM %s;" % self.table) for row in data: @@ -118,33 +124,8 @@ class SQLTable(collections.MutableMapping): def has_key(self, key): return key in self - -class SQLData(object): - """Object representing the persistent data""" - def __init__(self, filename): - bb.utils.mkdirhier(os.path.dirname(filename)) - - self.filename = filename - self.connection = sqlite3.connect(filename, timeout=5, - isolation_level=None) - self.cursor = self.connection.cursor() - self._tables = {} - - def __getitem__(self, table): - if not isinstance(table, basestring): - raise TypeError("table argument must be a string, not '%s'" % - type(table)) - - if table in self._tables: - return self._tables[table] - else: - tableobj = self._tables[table] = SQLTable(self.cursor, table) - return tableobj - - def __delitem__(self, table): - if table in self._tables: - del self._tables[table] - self.cursor.execute("DROP TABLE IF EXISTS %s;" % table) + def clear(self): + self._execute("DELETE FROM %s;" % self.table) class PersistData(object): @@ -194,14 +175,19 @@ class PersistData(object): """ del self.data[domain][key] +def connect(database): + return sqlite3.connect(database, timeout=30, isolation_level=None) -def persist(d): - """Convenience factory for construction of SQLData based upon metadata""" +def persist(domain, d): + """Convenience factory for SQLTable objects based upon metadata""" + import bb.data, bb.utils cachedir = (bb.data.getVar("PERSISTENT_DIR", d, True) or bb.data.getVar("CACHE", d, True)) if not cachedir: logger.critical("Please set the 'PERSISTENT_DIR' or 'CACHE' variable") sys.exit(1) + bb.utils.mkdirhier(cachedir) cachefile = os.path.join(cachedir, "bb_persist_data.sqlite3") - return SQLData(cachefile) + connection = connect(cachefile) + return SQLTable(connection, domain) |