persist_data.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import bb, os
  18. try:
  19. import sqlite3
  20. except ImportError:
  21. try:
  22. from pysqlite2 import dbapi2 as sqlite3
  23. except ImportError:
  24. 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.")
  25. sqlversion = sqlite3.sqlite_version_info
  26. if sqlversion[0] < 3 or (sqlversion[0] == 3 and sqlversion[1] < 3):
  27. bb.msg.fatal(bb.msg.domain.PersistData, "sqlite3 version 3.3.0 or later is required.")
  28. class PersistData:
  29. """
  30. BitBake Persistent Data Store
  31. Used to store data in a central location such that other threads/tasks can
  32. access them at some future date.
  33. The "domain" is used as a key to isolate each data pool and in this
  34. implementation corresponds to an SQL table. The SQL table consists of a
  35. simple key and value pair.
  36. Why sqlite? It handles all the locking issues for us.
  37. """
  38. def __init__(self, d):
  39. self.cachedir = bb.data.getVar("CACHE", d, True)
  40. if self.cachedir in [None, '']:
  41. bb.msg.fatal(bb.msg.domain.PersistData, "Please set the 'CACHE' variable.")
  42. try:
  43. os.stat(self.cachedir)
  44. except OSError:
  45. bb.mkdirhier(self.cachedir)
  46. self.cachefile = os.path.join(self.cachedir,"bb_persist_data.sqlite3")
  47. bb.msg.debug(1, bb.msg.domain.PersistData, "Using '%s' as the persistent data cache" % self.cachefile)
  48. self.connection = sqlite3.connect(self.cachefile, timeout=5, isolation_level=None)
  49. def addDomain(self, domain):
  50. """
  51. Should be called before any domain is used
  52. Creates it if it doesn't exist.
  53. """
  54. self.connection.execute("CREATE TABLE IF NOT EXISTS %s(key TEXT, value TEXT);" % domain)
  55. def delDomain(self, domain):
  56. """
  57. Removes a domain and all the data it contains
  58. """
  59. self.connection.execute("DROP TABLE IF EXISTS %s;" % domain)
  60. def getValue(self, domain, key):
  61. """
  62. Return the value of a key for a domain
  63. """
  64. data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key])
  65. for row in data:
  66. return row[1]
  67. def setValue(self, domain, key, value):
  68. """
  69. Sets the value of a key for a domain
  70. """
  71. data = self.connection.execute("SELECT * from %s where key=?;" % domain, [key])
  72. rows = 0
  73. for row in data:
  74. rows = rows + 1
  75. if rows:
  76. self._execute("UPDATE %s SET value=? WHERE key=?;" % domain, [value, key])
  77. else:
  78. self._execute("INSERT into %s(key, value) values (?, ?);" % domain, [key, value])
  79. def delValue(self, domain, key):
  80. """
  81. Deletes a key/value pair
  82. """
  83. self._execute("DELETE from %s where key=?;" % domain, [key])
  84. def _execute(self, *query):
  85. while True:
  86. try:
  87. self.connection.execute(*query)
  88. return
  89. except sqlite3.OperationalError, e:
  90. if 'database is locked' in str(e):
  91. continue
  92. raise