summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/persist_data.py
diff options
context:
space:
mode:
authorRichard Purdie <richard@openedhand.com>2007-08-03 13:40:52 +0000
committerRichard Purdie <richard@openedhand.com>2007-08-03 13:40:52 +0000
commitbfc70eb24e3ded25007811b1531673fa70b02401 (patch)
treefa4a29290d3178937fa085c147e8a51f815c6fdc /bitbake/lib/bb/persist_data.py
parent034bbb805be0002fe6d689abde19662868b57b2c (diff)
downloadpoky-bfc70eb24e3ded25007811b1531673fa70b02401.tar.gz
bitbake: Update along 1.8 branch
git-svn-id: https://svn.o-hand.com/repos/poky/trunk@2345 311d38ba-8fff-0310-9ca6-ca027cbcb966
Diffstat (limited to 'bitbake/lib/bb/persist_data.py')
-rw-r--r--bitbake/lib/bb/persist_data.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/bitbake/lib/bb/persist_data.py b/bitbake/lib/bb/persist_data.py
new file mode 100644
index 0000000000..4df335a6a1
--- /dev/null
+++ b/bitbake/lib/bb/persist_data.py
@@ -0,0 +1,94 @@
1# BitBake Persistent Data Store
2#
3# Copyright (C) 2007 Richard Purdie
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18import bb, os
19
20try:
21 import sqlite3
22except ImportError:
23 try:
24 from pysqlite2 import dbapi2 as sqlite3
25 except ImportError:
26 bb.msg.fatal(bb.msg.domain.PersistData, "Importing sqlite3 and pysqlite2 failed, please install one of them. A 'python-pysqlite2' like package is likely to be what you need.")
27
28class PersistData:
29 """
30 BitBake Persistent Data Store
31
32 Used to store data in a central location such that other threads/tasks can
33 access them at some future date.
34
35 The "domain" is used as a key to isolate each data pool and in this
36 implementation corresponds to an SQL table. The SQL table consists of a
37 simple key and value pair.
38
39 Why sqlite? It handles all the locking issues for us.
40 """
41 def __init__(self, d):
42 self.cachedir = bb.data.getVar("CACHE", d, True)
43 if self.cachedir in [None, '']:
44 bb.msg.fatal(bb.msg.domain.PersistData, "Please set the 'CACHE' variable.")
45 try:
46 os.stat(self.cachedir)
47 except OSError:
48 bb.mkdirhier(self.cachedir)
49
50 self.cachefile = os.path.join(self.cachedir,"bb_persist_data.sqlite3")
51 bb.msg.debug(1, bb.msg.domain.PersistData, "Using '%s' as the persistent data cache" % self.cachefile)
52
53 self.connection = sqlite3.connect(self.cachefile, timeout=5, isolation_level=None)
54
55 def addDomain(self, domain):
56 """
57 Should be called before any domain is used
58 Creates it if it doesn't exist.
59 """
60 self.connection.execute("CREATE TABLE IF NOT EXISTS %s(key TEXT, value TEXT);" % domain)
61
62 def delDomain(self, domain):
63 """
64 Removes a domain and all the data it contains
65 """
66 self.connection.execute("DROP TABLE IF EXISTS %s;" % domain)
67
68 def getValue(self, domain, key):
69 """
70 Return the value of a key for a domain
71 """
72 data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key])
73 for row in data:
74 return row[1]
75
76 def setValue(self, domain, key, value):
77 """
78 Sets the value of a key for a domain
79 """
80 data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key])
81 rows = 0
82 for row in data:
83 rows = rows + 1
84 if rows:
85 self.connection.execute("UPDATE %s SET value=? WHERE key=?;" % domain, [value, key])
86 else:
87 self.connection.execute("INSERT into %s(key, value) values (?, ?);" % domain, [key, value])
88
89 def delValue(self, domain, key):
90 """
91 Deletes a key/value pair
92 """
93 self.connection.execute("DELETE from %s where key=?;" % domain, [key])
94