Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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