Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 """
switches 0:0e018d759a2a 2 mbed SDK
switches 0:0e018d759a2a 3 Copyright (c) 2011-2014 ARM Limited
switches 0:0e018d759a2a 4
switches 0:0e018d759a2a 5 Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 6 you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 7 You may obtain a copy of the License at
switches 0:0e018d759a2a 8
switches 0:0e018d759a2a 9 http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 10
switches 0:0e018d759a2a 11 Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 12 distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 14 See the License for the specific language governing permissions and
switches 0:0e018d759a2a 15 limitations under the License.
switches 0:0e018d759a2a 16
switches 0:0e018d759a2a 17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
switches 0:0e018d759a2a 18 """
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20 import re
switches 0:0e018d759a2a 21 import json
switches 0:0e018d759a2a 22
switches 0:0e018d759a2a 23
switches 0:0e018d759a2a 24 class BaseDBAccess():
switches 0:0e018d759a2a 25 """ Class used to connect with test database and store test results
switches 0:0e018d759a2a 26 """
switches 0:0e018d759a2a 27 def __init__(self):
switches 0:0e018d759a2a 28 self.db_object = None
switches 0:0e018d759a2a 29 self.db_type = None
switches 0:0e018d759a2a 30 # Connection credentials
switches 0:0e018d759a2a 31 self.host = None
switches 0:0e018d759a2a 32 self.user = None
switches 0:0e018d759a2a 33 self.passwd = None
switches 0:0e018d759a2a 34 self.db = None
switches 0:0e018d759a2a 35
switches 0:0e018d759a2a 36 # Test Suite DB scheme (table names)
switches 0:0e018d759a2a 37 self.TABLE_BUILD_ID = 'mtest_build_id'
switches 0:0e018d759a2a 38 self.TABLE_BUILD_ID_STATUS = 'mtest_build_id_status'
switches 0:0e018d759a2a 39 self.TABLE_BUILD_ID_TYPE = 'mtest_build_id_type'
switches 0:0e018d759a2a 40 self.TABLE_TARGET = 'mtest_target'
switches 0:0e018d759a2a 41 self.TABLE_TEST_ENTRY = 'mtest_test_entry'
switches 0:0e018d759a2a 42 self.TABLE_TEST_ID = 'mtest_test_id'
switches 0:0e018d759a2a 43 self.TABLE_TEST_RESULT = 'mtest_test_result'
switches 0:0e018d759a2a 44 self.TABLE_TEST_TYPE = 'mtest_test_type'
switches 0:0e018d759a2a 45 self.TABLE_TOOLCHAIN = 'mtest_toolchain'
switches 0:0e018d759a2a 46 # Build ID status PKs
switches 0:0e018d759a2a 47 self.BUILD_ID_STATUS_STARTED = 1 # Started
switches 0:0e018d759a2a 48 self.BUILD_ID_STATUS_IN_PROGRESS = 2 # In Progress
switches 0:0e018d759a2a 49 self.BUILD_ID_STATUS_COMPLETED = 3 #Completed
switches 0:0e018d759a2a 50 self.BUILD_ID_STATUS_FAILED = 4 # Failed
switches 0:0e018d759a2a 51 # Build ID type PKs
switches 0:0e018d759a2a 52 self.BUILD_ID_TYPE_TEST = 1 # Test
switches 0:0e018d759a2a 53 self.BUILD_ID_TYPE_BUILD_ONLY = 2 # Build Only
switches 0:0e018d759a2a 54
switches 0:0e018d759a2a 55 def get_hostname(self):
switches 0:0e018d759a2a 56 """ Useful when creating build_id in database
switches 0:0e018d759a2a 57 Function returns (hostname, uname) which can be used as (build_id_name, build_id_desc)
switches 0:0e018d759a2a 58 """
switches 0:0e018d759a2a 59 # Get hostname from socket
switches 0:0e018d759a2a 60 import socket
switches 0:0e018d759a2a 61 hostname = socket.gethostbyaddr(socket.gethostname())[0]
switches 0:0e018d759a2a 62 # Get uname from platform resources
switches 0:0e018d759a2a 63 import platform
switches 0:0e018d759a2a 64 uname = json.dumps(platform.uname())
switches 0:0e018d759a2a 65 return (hostname, uname)
switches 0:0e018d759a2a 66
switches 0:0e018d759a2a 67 def get_db_type(self):
switches 0:0e018d759a2a 68 """ Returns database type. E.g. 'mysql', 'sqlLite' etc.
switches 0:0e018d759a2a 69 """
switches 0:0e018d759a2a 70 return self.db_type
switches 0:0e018d759a2a 71
switches 0:0e018d759a2a 72 def detect_database(self, verbose=False):
switches 0:0e018d759a2a 73 """ detect database and return VERION data structure or string (verbose=True)
switches 0:0e018d759a2a 74 """
switches 0:0e018d759a2a 75 return None
switches 0:0e018d759a2a 76
switches 0:0e018d759a2a 77 def parse_db_connection_string(self, str):
switches 0:0e018d759a2a 78 """ Parsing SQL DB connection string. String should contain:
switches 0:0e018d759a2a 79 - DB Name, user name, password, URL (DB host), name
switches 0:0e018d759a2a 80 Function should return tuple with parsed (db_type, username, password, host, db_name) or None if error
switches 0:0e018d759a2a 81
switches 0:0e018d759a2a 82 (db_type, username, password, host, db_name) = self.parse_db_connection_string(db_url)
switches 0:0e018d759a2a 83
switches 0:0e018d759a2a 84 E.g. connection string: 'mysql://username:password@127.0.0.1/db_name'
switches 0:0e018d759a2a 85 """
switches 0:0e018d759a2a 86 result = None
switches 0:0e018d759a2a 87 if type(str) == type(''):
switches 0:0e018d759a2a 88 PATTERN = '^([\w]+)://([\w]+):([\w]*)@(.*)/([\w]+)'
switches 0:0e018d759a2a 89 result = re.match(PATTERN, str)
switches 0:0e018d759a2a 90 if result is not None:
switches 0:0e018d759a2a 91 result = result.groups() # Tuple (db_name, host, user, passwd, db)
switches 0:0e018d759a2a 92 return result # (db_type, username, password, host, db_name)
switches 0:0e018d759a2a 93
switches 0:0e018d759a2a 94 def is_connected(self):
switches 0:0e018d759a2a 95 """ Returns True if we are connected to database
switches 0:0e018d759a2a 96 """
switches 0:0e018d759a2a 97 pass
switches 0:0e018d759a2a 98
switches 0:0e018d759a2a 99 def connect(self, host, user, passwd, db):
switches 0:0e018d759a2a 100 """ Connects to DB and returns DB object
switches 0:0e018d759a2a 101 """
switches 0:0e018d759a2a 102 pass
switches 0:0e018d759a2a 103
switches 0:0e018d759a2a 104 def connect_url(self, db_url):
switches 0:0e018d759a2a 105 """ Connects to database using db_url (database url parsing),
switches 0:0e018d759a2a 106 store host, username, password, db_name
switches 0:0e018d759a2a 107 """
switches 0:0e018d759a2a 108 pass
switches 0:0e018d759a2a 109
switches 0:0e018d759a2a 110 def reconnect(self):
switches 0:0e018d759a2a 111 """ Reconnects to DB and returns DB object using stored host name,
switches 0:0e018d759a2a 112 database name and credentials (user name and password)
switches 0:0e018d759a2a 113 """
switches 0:0e018d759a2a 114 pass
switches 0:0e018d759a2a 115
switches 0:0e018d759a2a 116 def disconnect(self):
switches 0:0e018d759a2a 117 """ Close DB connection
switches 0:0e018d759a2a 118 """
switches 0:0e018d759a2a 119 pass
switches 0:0e018d759a2a 120
switches 0:0e018d759a2a 121 def escape_string(self, str):
switches 0:0e018d759a2a 122 """ Escapes string so it can be put in SQL query between quotes
switches 0:0e018d759a2a 123 """
switches 0:0e018d759a2a 124 pass
switches 0:0e018d759a2a 125
switches 0:0e018d759a2a 126 def select_all(self, query):
switches 0:0e018d759a2a 127 """ Execute SELECT query and get all results
switches 0:0e018d759a2a 128 """
switches 0:0e018d759a2a 129 pass
switches 0:0e018d759a2a 130
switches 0:0e018d759a2a 131 def insert(self, query, commit=True):
switches 0:0e018d759a2a 132 """ Execute INSERT query, define if you want to commit
switches 0:0e018d759a2a 133 """
switches 0:0e018d759a2a 134 pass
switches 0:0e018d759a2a 135
switches 0:0e018d759a2a 136 def get_next_build_id(self, name, desc='', location='', type=None, status=None):
switches 0:0e018d759a2a 137 """ Insert new build_id (DB unique build like ID number to send all test results)
switches 0:0e018d759a2a 138 """
switches 0:0e018d759a2a 139 pass
switches 0:0e018d759a2a 140
switches 0:0e018d759a2a 141 def get_table_entry_pk(self, table, column, value, update_db=True):
switches 0:0e018d759a2a 142 """ Checks for entries in tables with two columns (<TABLE_NAME>_pk, <column>)
switches 0:0e018d759a2a 143 If update_db is True updates table entry if value in specified column doesn't exist
switches 0:0e018d759a2a 144 """
switches 0:0e018d759a2a 145 pass
switches 0:0e018d759a2a 146
switches 0:0e018d759a2a 147 def update_table_entry(self, table, column, value):
switches 0:0e018d759a2a 148 """ Updates table entry if value in specified column doesn't exist
switches 0:0e018d759a2a 149 Locks table to perform atomic read + update
switches 0:0e018d759a2a 150 """
switches 0:0e018d759a2a 151 pass
switches 0:0e018d759a2a 152
switches 0:0e018d759a2a 153 def update_build_id_info(self, build_id, **kw):
switches 0:0e018d759a2a 154 """ Update additional data inside build_id table
switches 0:0e018d759a2a 155 Examples:
switches 0:0e018d759a2a 156 db.update_build_is(build_id, _status_fk=self.BUILD_ID_STATUS_COMPLETED, _shuffle_seed=0.0123456789):
switches 0:0e018d759a2a 157 """
switches 0:0e018d759a2a 158 pass
switches 0:0e018d759a2a 159
switches 0:0e018d759a2a 160 def insert_test_entry(self, build_id, target, toolchain, test_type, test_id, test_result, test_time, test_timeout, test_loop, test_extra=''):
switches 0:0e018d759a2a 161 """ Inserts test result entry to database. All checks regarding existing
switches 0:0e018d759a2a 162 toolchain names in DB are performed.
switches 0:0e018d759a2a 163 If some data is missing DB will be updated
switches 0:0e018d759a2a 164 """
switches 0:0e018d759a2a 165 pass