diff options
| author | Lianhao Lu <lianhao.lu@intel.com> | 2011-05-27 14:31:45 +0800 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-05-27 17:55:49 +0100 |
| commit | ecdbd6ab03749f603ffbde2983b3e8ea3bbd73cd (patch) | |
| tree | afa8913e66b049d40848020e3da257a2f52137e0 /bitbake/lib/prserv/db.py | |
| parent | 859e21aac1584fe07e5cc7030e7f8918170291f9 (diff) | |
| download | poky-ecdbd6ab03749f603ffbde2983b3e8ea3bbd73cd.tar.gz | |
Add PR service deamon to bitbake
Added the initial implementation of the server side PR service.
(Bitbake rev: 4d0e79e5591ff58ce35c7fb96f6e9217ddc27466)
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/prserv/db.py')
| -rw-r--r-- | bitbake/lib/prserv/db.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/bitbake/lib/prserv/db.py b/bitbake/lib/prserv/db.py new file mode 100644 index 0000000000..bbee9316b2 --- /dev/null +++ b/bitbake/lib/prserv/db.py | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | import logging | ||
| 2 | import os.path | ||
| 3 | import errno | ||
| 4 | import sys | ||
| 5 | import warnings | ||
| 6 | import sqlite3 | ||
| 7 | |||
| 8 | try: | ||
| 9 | import sqlite3 | ||
| 10 | except ImportError: | ||
| 11 | from pysqlite2 import dbapi2 as sqlite3 | ||
| 12 | |||
| 13 | sqlversion = sqlite3.sqlite_version_info | ||
| 14 | if sqlversion[0] < 3 or (sqlversion[0] == 3 and sqlversion[1] < 3): | ||
| 15 | raise Exception("sqlite3 version 3.3.0 or later is required.") | ||
| 16 | |||
| 17 | class NotFoundError(StandardError): | ||
| 18 | pass | ||
| 19 | |||
| 20 | class PRTable(): | ||
| 21 | def __init__(self,cursor,table): | ||
| 22 | self.cursor = cursor | ||
| 23 | self.table = table | ||
| 24 | |||
| 25 | #create the table | ||
| 26 | self._execute("CREATE TABLE IF NOT EXISTS %s \ | ||
| 27 | (version TEXT NOT NULL, \ | ||
| 28 | checksum TEXT NOT NULL, \ | ||
| 29 | value INTEGER, \ | ||
| 30 | PRIMARY KEY (version,checksum));" | ||
| 31 | % table) | ||
| 32 | |||
| 33 | def _execute(self, *query): | ||
| 34 | """Execute a query, waiting to acquire a lock if necessary""" | ||
| 35 | count = 0 | ||
| 36 | while True: | ||
| 37 | try: | ||
| 38 | return self.cursor.execute(*query) | ||
| 39 | except sqlite3.OperationalError as exc: | ||
| 40 | if 'database is locked' in str(exc) and count < 500: | ||
| 41 | count = count + 1 | ||
| 42 | continue | ||
| 43 | raise | ||
| 44 | except sqlite3.IntegrityError as exc: | ||
| 45 | print "Integrity error %s" % str(exc) | ||
| 46 | break | ||
| 47 | |||
| 48 | def getValue(self, version, checksum): | ||
| 49 | data=self._execute("SELECT value FROM %s WHERE version=? AND checksum=?;" % self.table, | ||
| 50 | (version,checksum)) | ||
| 51 | row=data.fetchone() | ||
| 52 | if row != None: | ||
| 53 | return row[0] | ||
| 54 | else: | ||
| 55 | #no value found, try to insert | ||
| 56 | self._execute("INSERT INTO %s VALUES (?, ?, (select ifnull(max(value)+1,0) from %s where version=?));" | ||
| 57 | % (self.table,self.table), | ||
| 58 | (version,checksum,version)) | ||
| 59 | data=self._execute("SELECT value FROM %s WHERE version=? AND checksum=?;" % self.table, | ||
| 60 | (version,checksum)) | ||
| 61 | row=data.fetchone() | ||
| 62 | if row != None: | ||
| 63 | return row[0] | ||
| 64 | else: | ||
| 65 | raise NotFoundError | ||
| 66 | |||
| 67 | class PRData(object): | ||
| 68 | """Object representing the PR database""" | ||
| 69 | def __init__(self, filename): | ||
| 70 | self.filename=os.path.abspath(filename) | ||
| 71 | #build directory hierarchy | ||
| 72 | try: | ||
| 73 | os.makedirs(os.path.dirname(self.filename)) | ||
| 74 | except OSError as e: | ||
| 75 | if e.errno != errno.EEXIST: | ||
| 76 | raise e | ||
| 77 | self.connection=sqlite3.connect(self.filename, timeout=5, | ||
| 78 | isolation_level=None) | ||
| 79 | self.cursor=self.connection.cursor() | ||
| 80 | self._tables={} | ||
| 81 | |||
| 82 | def __del__(self): | ||
| 83 | print "PRData: closing DB %s" % self.filename | ||
| 84 | self.connection.close() | ||
| 85 | |||
| 86 | def __getitem__(self,tblname): | ||
| 87 | if not isinstance(tblname, basestring): | ||
| 88 | raise TypeError("tblname argument must be a string, not '%s'" % | ||
| 89 | type(tblname)) | ||
| 90 | if tblname in self._tables: | ||
| 91 | return self._tables[tblname] | ||
| 92 | else: | ||
| 93 | tableobj = self._tables[tblname] = PRTable(self.cursor, tblname) | ||
| 94 | return tableobj | ||
| 95 | |||
| 96 | def __delitem__(self, tblname): | ||
| 97 | if tblname in self._tables: | ||
| 98 | del self._tables[tblname] | ||
| 99 | logging.info("drop table %s" % (tblname)) | ||
| 100 | self.cursor.execute("DROP TABLE IF EXISTS %s;" % tblname) | ||
