nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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