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