mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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