Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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