diff options
author | Richard Purdie <richard@openedhand.com> | 2008-09-30 15:08:33 +0000 |
---|---|---|
committer | Richard Purdie <richard@openedhand.com> | 2008-09-30 15:08:33 +0000 |
commit | c30eddb243e7e65f67f656e62848a033cf6f2e5c (patch) | |
tree | 110dd95788b76f55d31cb8d30aac2de8400b6f4a /bitbake-dev/lib/bb/persist_data.py | |
parent | 5ef0510474004eeb2ae8a99b64e2febb1920e077 (diff) | |
download | poky-c30eddb243e7e65f67f656e62848a033cf6f2e5c.tar.gz |
Add bitbake-dev to allow ease of testing and development of bitbake trunk
git-svn-id: https://svn.o-hand.com/repos/poky/trunk@5337 311d38ba-8fff-0310-9ca6-ca027cbcb966
Diffstat (limited to 'bitbake-dev/lib/bb/persist_data.py')
-rw-r--r-- | bitbake-dev/lib/bb/persist_data.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/bitbake-dev/lib/bb/persist_data.py b/bitbake-dev/lib/bb/persist_data.py new file mode 100644 index 0000000000..79e7448bee --- /dev/null +++ b/bitbake-dev/lib/bb/persist_data.py | |||
@@ -0,0 +1,110 @@ | |||
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 | |||
18 | import bb, os | ||
19 | |||
20 | try: | ||
21 | import sqlite3 | ||
22 | except 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. Python 2.5 or a 'python-pysqlite2' like package is likely to be what you need.") | ||
27 | |||
28 | sqlversion = sqlite3.sqlite_version_info | ||
29 | if sqlversion[0] < 3 or (sqlversion[0] == 3 and sqlversion[1] < 3): | ||
30 | bb.msg.fatal(bb.msg.domain.PersistData, "sqlite3 version 3.3.0 or later is required.") | ||
31 | |||
32 | class PersistData: | ||
33 | """ | ||
34 | BitBake Persistent Data Store | ||
35 | |||
36 | Used to store data in a central location such that other threads/tasks can | ||
37 | access them at some future date. | ||
38 | |||
39 | The "domain" is used as a key to isolate each data pool and in this | ||
40 | implementation corresponds to an SQL table. The SQL table consists of a | ||
41 | simple key and value pair. | ||
42 | |||
43 | Why sqlite? It handles all the locking issues for us. | ||
44 | """ | ||
45 | def __init__(self, d): | ||
46 | self.cachedir = bb.data.getVar("PERSISTENT_DIR", d, True) or bb.data.getVar("CACHE", d, True) | ||
47 | if self.cachedir in [None, '']: | ||
48 | bb.msg.fatal(bb.msg.domain.PersistData, "Please set the 'PERSISTENT_DIR' or 'CACHE' variable.") | ||
49 | try: | ||
50 | os.stat(self.cachedir) | ||
51 | except OSError: | ||
52 | bb.mkdirhier(self.cachedir) | ||
53 | |||
54 | self.cachefile = os.path.join(self.cachedir,"bb_persist_data.sqlite3") | ||
55 | bb.msg.debug(1, bb.msg.domain.PersistData, "Using '%s' as the persistent data cache" % self.cachefile) | ||
56 | |||
57 | self.connection = sqlite3.connect(self.cachefile, timeout=5, isolation_level=None) | ||
58 | |||
59 | def addDomain(self, domain): | ||
60 | """ | ||
61 | Should be called before any domain is used | ||
62 | Creates it if it doesn't exist. | ||
63 | """ | ||
64 | self.connection.execute("CREATE TABLE IF NOT EXISTS %s(key TEXT, value TEXT);" % domain) | ||
65 | |||
66 | def delDomain(self, domain): | ||
67 | """ | ||
68 | Removes a domain and all the data it contains | ||
69 | """ | ||
70 | self.connection.execute("DROP TABLE IF EXISTS %s;" % domain) | ||
71 | |||
72 | def getValue(self, domain, key): | ||
73 | """ | ||
74 | Return the value of a key for a domain | ||
75 | """ | ||
76 | data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key]) | ||
77 | for row in data: | ||
78 | return row[1] | ||
79 | |||
80 | def setValue(self, domain, key, value): | ||
81 | """ | ||
82 | Sets the value of a key for a domain | ||
83 | """ | ||
84 | data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key]) | ||
85 | rows = 0 | ||
86 | for row in data: | ||
87 | rows = rows + 1 | ||
88 | if rows: | ||
89 | self._execute("UPDATE %s SET value=? WHERE key=?;" % domain, [value, key]) | ||
90 | else: | ||
91 | self._execute("INSERT into %s(key, value) values (?, ?);" % domain, [key, value]) | ||
92 | |||
93 | def delValue(self, domain, key): | ||
94 | """ | ||
95 | Deletes a key/value pair | ||
96 | """ | ||
97 | self._execute("DELETE from %s where key=?;" % domain, [key]) | ||
98 | |||
99 | def _execute(self, *query): | ||
100 | while True: | ||
101 | try: | ||
102 | self.connection.execute(*query) | ||
103 | return | ||
104 | except sqlite3.OperationalError, e: | ||
105 | if 'database is locked' in str(e): | ||
106 | continue | ||
107 | raise | ||
108 | |||
109 | |||
110 | |||