Maxim nexpaq / nexpaq_dev
Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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