mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 """
elessair 0:f269e3021894 2 mbed SDK
elessair 0:f269e3021894 3 Copyright (c) 2011-2014 ARM Limited
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 6 you may not use this file except in compliance with the License.
elessair 0:f269e3021894 7 You may obtain a copy of the License at
elessair 0:f269e3021894 8
elessair 0:f269e3021894 9 http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10
elessair 0:f269e3021894 11 Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 limitations under the License.
elessair 0:f269e3021894 16
elessair 0:f269e3021894 17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
elessair 0:f269e3021894 18 """
elessair 0:f269e3021894 19
elessair 0:f269e3021894 20 import re
elessair 0:f269e3021894 21 import MySQLdb as mdb
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 # Imports from TEST API
elessair 0:f269e3021894 24 from tools.test_db import BaseDBAccess
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 class MySQLDBAccess(BaseDBAccess):
elessair 0:f269e3021894 28 """ Wrapper for MySQL DB access for common test suite interface
elessair 0:f269e3021894 29 """
elessair 0:f269e3021894 30 def __init__(self):
elessair 0:f269e3021894 31 BaseDBAccess.__init__(self)
elessair 0:f269e3021894 32 self.DB_TYPE = 'mysql'
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 def detect_database(self, verbose=False):
elessair 0:f269e3021894 35 """ detect database and return VERION data structure or string (verbose=True)
elessair 0:f269e3021894 36 """
elessair 0:f269e3021894 37 query = 'SHOW VARIABLES LIKE "%version%"'
elessair 0:f269e3021894 38 rows = self.select_all(query)
elessair 0:f269e3021894 39 if verbose:
elessair 0:f269e3021894 40 result = []
elessair 0:f269e3021894 41 for row in rows:
elessair 0:f269e3021894 42 result.append("\t%s: %s"% (row['Variable_name'], row['Value']))
elessair 0:f269e3021894 43 result = "\n".join(result)
elessair 0:f269e3021894 44 else:
elessair 0:f269e3021894 45 result = rows
elessair 0:f269e3021894 46 return result
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 def parse_db_connection_string(self, str):
elessair 0:f269e3021894 49 """ Parsing SQL DB connection string. String should contain:
elessair 0:f269e3021894 50 - DB Name, user name, password, URL (DB host), name
elessair 0:f269e3021894 51 Function should return tuple with parsed (host, user, passwd, db) or None if error
elessair 0:f269e3021894 52 E.g. connection string: 'mysql://username:password@127.0.0.1/db_name'
elessair 0:f269e3021894 53 """
elessair 0:f269e3021894 54 result = BaseDBAccess().parse_db_connection_string(str)
elessair 0:f269e3021894 55 if result is not None:
elessair 0:f269e3021894 56 (db_type, username, password, host, db_name) = result
elessair 0:f269e3021894 57 if db_type != 'mysql':
elessair 0:f269e3021894 58 result = None
elessair 0:f269e3021894 59 return result
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 def is_connected(self):
elessair 0:f269e3021894 62 """ Returns True if we are connected to database
elessair 0:f269e3021894 63 """
elessair 0:f269e3021894 64 return self.db_object is not None
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 def connect(self, host, user, passwd, db):
elessair 0:f269e3021894 67 """ Connects to DB and returns DB object
elessair 0:f269e3021894 68 """
elessair 0:f269e3021894 69 try:
elessair 0:f269e3021894 70 self.db_object = mdb.connect(host=host, user=user, passwd=passwd, db=db)
elessair 0:f269e3021894 71 # Let's remember connection credentials
elessair 0:f269e3021894 72 self.db_type = self.DB_TYPE
elessair 0:f269e3021894 73 self.host = host
elessair 0:f269e3021894 74 self.user = user
elessair 0:f269e3021894 75 self.passwd = passwd
elessair 0:f269e3021894 76 self.db = db
elessair 0:f269e3021894 77 except mdb.Error, e:
elessair 0:f269e3021894 78 print "Error %d: %s"% (e.args[0], e.args[1])
elessair 0:f269e3021894 79 self.db_object = None
elessair 0:f269e3021894 80 self.db_type = None
elessair 0:f269e3021894 81 self.host = None
elessair 0:f269e3021894 82 self.user = None
elessair 0:f269e3021894 83 self.passwd = None
elessair 0:f269e3021894 84 self.db = None
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 def connect_url(self, db_url):
elessair 0:f269e3021894 87 """ Connects to database using db_url (database url parsing),
elessair 0:f269e3021894 88 store host, username, password, db_name
elessair 0:f269e3021894 89 """
elessair 0:f269e3021894 90 result = self.parse_db_connection_string(db_url)
elessair 0:f269e3021894 91 if result is not None:
elessair 0:f269e3021894 92 (db_type, username, password, host, db_name) = result
elessair 0:f269e3021894 93 if db_type == self.DB_TYPE:
elessair 0:f269e3021894 94 self.connect(host, username, password, db_name)
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 def reconnect(self):
elessair 0:f269e3021894 97 """ Reconnects to DB and returns DB object using stored host name,
elessair 0:f269e3021894 98 database name and credentials (user name and password)
elessair 0:f269e3021894 99 """
elessair 0:f269e3021894 100 self.connect(self.host, self.user, self.passwd, self.db)
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 def disconnect(self):
elessair 0:f269e3021894 103 """ Close DB connection
elessair 0:f269e3021894 104 """
elessair 0:f269e3021894 105 if self.db_object:
elessair 0:f269e3021894 106 self.db_object.close()
elessair 0:f269e3021894 107 self.db_object = None
elessair 0:f269e3021894 108 self.db_type = None
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 def escape_string(self, str):
elessair 0:f269e3021894 111 """ Escapes string so it can be put in SQL query between quotes
elessair 0:f269e3021894 112 """
elessair 0:f269e3021894 113 con = self.db_object
elessair 0:f269e3021894 114 result = con.escape_string(str)
elessair 0:f269e3021894 115 return result if result else ''
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 def select_all(self, query):
elessair 0:f269e3021894 118 """ Execute SELECT query and get all results
elessair 0:f269e3021894 119 """
elessair 0:f269e3021894 120 con = self.db_object
elessair 0:f269e3021894 121 cur = con.cursor(mdb.cursors.DictCursor)
elessair 0:f269e3021894 122 cur.execute(query)
elessair 0:f269e3021894 123 rows = cur.fetchall()
elessair 0:f269e3021894 124 return rows
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 def insert(self, query, commit=True):
elessair 0:f269e3021894 127 """ Execute INSERT query, define if you want to commit
elessair 0:f269e3021894 128 """
elessair 0:f269e3021894 129 con = self.db_object
elessair 0:f269e3021894 130 cur = con.cursor()
elessair 0:f269e3021894 131 cur.execute(query)
elessair 0:f269e3021894 132 if commit:
elessair 0:f269e3021894 133 con.commit()
elessair 0:f269e3021894 134 return cur.lastrowid
elessair 0:f269e3021894 135
elessair 0:f269e3021894 136 def get_next_build_id(self, name, desc='', location='', type=None, status=None):
elessair 0:f269e3021894 137 """ Insert new build_id (DB unique build like ID number to send all test results)
elessair 0:f269e3021894 138 """
elessair 0:f269e3021894 139 if status is None:
elessair 0:f269e3021894 140 status = self.BUILD_ID_STATUS_STARTED
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 if type is None:
elessair 0:f269e3021894 143 type = self.BUILD_ID_TYPE_TEST
elessair 0:f269e3021894 144
elessair 0:f269e3021894 145 query = """INSERT INTO `%s` (%s_name, %s_desc, %s_location, %s_type_fk, %s_status_fk)
elessair 0:f269e3021894 146 VALUES ('%s', '%s', '%s', %d, %d)"""% (self.TABLE_BUILD_ID,
elessair 0:f269e3021894 147 self.TABLE_BUILD_ID,
elessair 0:f269e3021894 148 self.TABLE_BUILD_ID,
elessair 0:f269e3021894 149 self.TABLE_BUILD_ID,
elessair 0:f269e3021894 150 self.TABLE_BUILD_ID,
elessair 0:f269e3021894 151 self.TABLE_BUILD_ID,
elessair 0:f269e3021894 152 self.escape_string(name),
elessair 0:f269e3021894 153 self.escape_string(desc),
elessair 0:f269e3021894 154 self.escape_string(location),
elessair 0:f269e3021894 155 type,
elessair 0:f269e3021894 156 status)
elessair 0:f269e3021894 157 index = self.insert(query) # Provide inserted record PK
elessair 0:f269e3021894 158 return index
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 def get_table_entry_pk(self, table, column, value, update_db=True):
elessair 0:f269e3021894 161 """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>)
elessair 0:f269e3021894 162 If update_db is True updates table entry if value in specified column doesn't exist
elessair 0:f269e3021894 163 """
elessair 0:f269e3021894 164 # TODO: table buffering
elessair 0:f269e3021894 165 result = None
elessair 0:f269e3021894 166 table_pk = '%s_pk'% table
elessair 0:f269e3021894 167 query = """SELECT `%s`
elessair 0:f269e3021894 168 FROM `%s`
elessair 0:f269e3021894 169 WHERE `%s`='%s'"""% (table_pk,
elessair 0:f269e3021894 170 table,
elessair 0:f269e3021894 171 column,
elessair 0:f269e3021894 172 self.escape_string(value))
elessair 0:f269e3021894 173 rows = self.select_all(query)
elessair 0:f269e3021894 174 if len(rows) == 1:
elessair 0:f269e3021894 175 result = rows[0][table_pk]
elessair 0:f269e3021894 176 elif len(rows) == 0 and update_db:
elessair 0:f269e3021894 177 # Update DB with new value
elessair 0:f269e3021894 178 result = self.update_table_entry(table, column, value)
elessair 0:f269e3021894 179 return result
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 def update_table_entry(self, table, column, value):
elessair 0:f269e3021894 182 """ Updates table entry if value in specified column doesn't exist
elessair 0:f269e3021894 183 Locks table to perform atomic read + update
elessair 0:f269e3021894 184 """
elessair 0:f269e3021894 185 result = None
elessair 0:f269e3021894 186 con = self.db_object
elessair 0:f269e3021894 187 cur = con.cursor()
elessair 0:f269e3021894 188 cur.execute("LOCK TABLES `%s` WRITE"% table)
elessair 0:f269e3021894 189 table_pk = '%s_pk'% table
elessair 0:f269e3021894 190 query = """SELECT `%s`
elessair 0:f269e3021894 191 FROM `%s`
elessair 0:f269e3021894 192 WHERE `%s`='%s'"""% (table_pk,
elessair 0:f269e3021894 193 table,
elessair 0:f269e3021894 194 column,
elessair 0:f269e3021894 195 self.escape_string(value))
elessair 0:f269e3021894 196 cur.execute(query)
elessair 0:f269e3021894 197 rows = cur.fetchall()
elessair 0:f269e3021894 198 if len(rows) == 0:
elessair 0:f269e3021894 199 query = """INSERT INTO `%s` (%s)
elessair 0:f269e3021894 200 VALUES ('%s')"""% (table,
elessair 0:f269e3021894 201 column,
elessair 0:f269e3021894 202 self.escape_string(value))
elessair 0:f269e3021894 203 cur.execute(query)
elessair 0:f269e3021894 204 result = cur.lastrowid
elessair 0:f269e3021894 205 con.commit()
elessair 0:f269e3021894 206 cur.execute("UNLOCK TABLES")
elessair 0:f269e3021894 207 return result
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 def update_build_id_info(self, build_id, **kw):
elessair 0:f269e3021894 210 """ Update additional data inside build_id table
elessair 0:f269e3021894 211 Examples:
elessair 0:f269e3021894 212 db.update_build_id_info(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789):
elessair 0:f269e3021894 213 """
elessair 0:f269e3021894 214 if len(kw):
elessair 0:f269e3021894 215 con = self.db_object
elessair 0:f269e3021894 216 cur = con.cursor()
elessair 0:f269e3021894 217 # Prepare UPDATE query
elessair 0:f269e3021894 218 # ["`mtest_build_id_pk`=[value-1]", "`mtest_build_id_name`=[value-2]", "`mtest_build_id_desc`=[value-3]"]
elessair 0:f269e3021894 219 set_list = []
elessair 0:f269e3021894 220 for col_sufix in kw:
elessair 0:f269e3021894 221 assign_str = "`%s%s`='%s'"% (self.TABLE_BUILD_ID, col_sufix, self.escape_string(str(kw[col_sufix])))
elessair 0:f269e3021894 222 set_list.append(assign_str)
elessair 0:f269e3021894 223 set_str = ', '.join(set_list)
elessair 0:f269e3021894 224 query = """UPDATE `%s`
elessair 0:f269e3021894 225 SET %s
elessair 0:f269e3021894 226 WHERE `mtest_build_id_pk`=%d"""% (self.TABLE_BUILD_ID,
elessair 0:f269e3021894 227 set_str,
elessair 0:f269e3021894 228 build_id)
elessair 0:f269e3021894 229 cur.execute(query)
elessair 0:f269e3021894 230 con.commit()
elessair 0:f269e3021894 231
elessair 0:f269e3021894 232 def insert_test_entry(self, build_id, target, toolchain, test_type, test_id, test_result, test_output, test_time, test_timeout, test_loop, test_extra=''):
elessair 0:f269e3021894 233 """ Inserts test result entry to database. All checks regarding existing
elessair 0:f269e3021894 234 toolchain names in DB are performed.
elessair 0:f269e3021894 235 If some data is missing DB will be updated
elessair 0:f269e3021894 236 """
elessair 0:f269e3021894 237 # Get all table FK and if entry is new try to insert new value
elessair 0:f269e3021894 238 target_fk = self.get_table_entry_pk(self.TABLE_TARGET, self.TABLE_TARGET + '_name', target)
elessair 0:f269e3021894 239 toolchain_fk = self.get_table_entry_pk(self.TABLE_TOOLCHAIN, self.TABLE_TOOLCHAIN + '_name', toolchain)
elessair 0:f269e3021894 240 test_type_fk = self.get_table_entry_pk(self.TABLE_TEST_TYPE, self.TABLE_TEST_TYPE + '_name', test_type)
elessair 0:f269e3021894 241 test_id_fk = self.get_table_entry_pk(self.TABLE_TEST_ID, self.TABLE_TEST_ID + '_name', test_id)
elessair 0:f269e3021894 242 test_result_fk = self.get_table_entry_pk(self.TABLE_TEST_RESULT, self.TABLE_TEST_RESULT + '_name', test_result)
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 con = self.db_object
elessair 0:f269e3021894 245 cur = con.cursor()
elessair 0:f269e3021894 246
elessair 0:f269e3021894 247 query = """ INSERT INTO `%s` (`mtest_build_id_fk`,
elessair 0:f269e3021894 248 `mtest_target_fk`,
elessair 0:f269e3021894 249 `mtest_toolchain_fk`,
elessair 0:f269e3021894 250 `mtest_test_type_fk`,
elessair 0:f269e3021894 251 `mtest_test_id_fk`,
elessair 0:f269e3021894 252 `mtest_test_result_fk`,
elessair 0:f269e3021894 253 `mtest_test_output`,
elessair 0:f269e3021894 254 `mtest_test_time`,
elessair 0:f269e3021894 255 `mtest_test_timeout`,
elessair 0:f269e3021894 256 `mtest_test_loop_no`,
elessair 0:f269e3021894 257 `mtest_test_result_extra`)
elessair 0:f269e3021894 258 VALUES (%d, %d, %d, %d, %d, %d, '%s', %.2f, %.2f, %d, '%s')"""% (self.TABLE_TEST_ENTRY,
elessair 0:f269e3021894 259 build_id,
elessair 0:f269e3021894 260 target_fk,
elessair 0:f269e3021894 261 toolchain_fk,
elessair 0:f269e3021894 262 test_type_fk,
elessair 0:f269e3021894 263 test_id_fk,
elessair 0:f269e3021894 264 test_result_fk,
elessair 0:f269e3021894 265 self.escape_string(test_output),
elessair 0:f269e3021894 266 test_time,
elessair 0:f269e3021894 267 test_timeout,
elessair 0:f269e3021894 268 test_loop,
elessair 0:f269e3021894 269 self.escape_string(test_extra))
elessair 0:f269e3021894 270 cur.execute(query)
elessair 0:f269e3021894 271 con.commit()