Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Initial revision

Who changed what in which revision?

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