diff options
Diffstat (limited to 'bitbake-dev/lib/bb/persist_data.py')
-rw-r--r-- | bitbake-dev/lib/bb/persist_data.py | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/bitbake-dev/lib/bb/persist_data.py b/bitbake-dev/lib/bb/persist_data.py deleted file mode 100644 index bc4045fe85..0000000000 --- a/bitbake-dev/lib/bb/persist_data.py +++ /dev/null | |||
@@ -1,121 +0,0 @@ | |||
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 getKeyValues(self, domain): | ||
73 | """ | ||
74 | Return a list of key + value pairs for a domain | ||
75 | """ | ||
76 | ret = {} | ||
77 | data = self.connection.execute("SELECT key, value from %s;" % domain) | ||
78 | for row in data: | ||
79 | ret[str(row[0])] = str(row[1]) | ||
80 | |||
81 | return ret | ||
82 | |||
83 | def getValue(self, domain, key): | ||
84 | """ | ||
85 | Return the value of a key for a domain | ||
86 | """ | ||
87 | data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key]) | ||
88 | for row in data: | ||
89 | return row[1] | ||
90 | |||
91 | def setValue(self, domain, key, value): | ||
92 | """ | ||
93 | Sets the value of a key for a domain | ||
94 | """ | ||
95 | data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key]) | ||
96 | rows = 0 | ||
97 | for row in data: | ||
98 | rows = rows + 1 | ||
99 | if rows: | ||
100 | self._execute("UPDATE %s SET value=? WHERE key=?;" % domain, [value, key]) | ||
101 | else: | ||
102 | self._execute("INSERT into %s(key, value) values (?, ?);" % domain, [key, value]) | ||
103 | |||
104 | def delValue(self, domain, key): | ||
105 | """ | ||
106 | Deletes a key/value pair | ||
107 | """ | ||
108 | self._execute("DELETE from %s where key=?;" % domain, [key]) | ||
109 | |||
110 | def _execute(self, *query): | ||
111 | while True: | ||
112 | try: | ||
113 | self.connection.execute(*query) | ||
114 | return | ||
115 | except sqlite3.OperationalError, e: | ||
116 | if 'database is locked' in str(e): | ||
117 | continue | ||
118 | raise | ||
119 | |||
120 | |||
121 | |||