mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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