From 4d9d4e5fef9126804163405a2b69895d7d208ba8 Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 8 Apr 2021 11:38:50 +0100 Subject: [PATCH] Initial version of report tools Signed-off-by: Nikita --- report-tools/adaptors/sql/sqlite.py | 37 ++++++++ report-tools/adaptors/sql/yaml_parser.py | 116 +++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100755 report-tools/adaptors/sql/sqlite.py create mode 100755 report-tools/adaptors/sql/yaml_parser.py diff --git a/report-tools/adaptors/sql/sqlite.py b/report-tools/adaptors/sql/sqlite.py new file mode 100755 index 0000000..0adf493 --- /dev/null +++ b/report-tools/adaptors/sql/sqlite.py @@ -0,0 +1,37 @@ +############################################################################## +# Copyright (c) 2021, ARM Limited and Contributors. All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause +############################################################################## +""" +SQL adaptor for SQLite queries +""" +import sqlite3 + + +class Database: + """ + Class used to represent an sqlite database + + Methods: + execute_query: Executes and sqlite query and returns response + """ + + def __init__(self, db): + """Inits Database class with an sqlite db instance""" + self.mydb = sqlite3.connect(db) + self.cursor = self.mydb.cursor() + + def execute_query(self, query): + """Executes a sqlite query + Args: + query(str): sqlite query + Returns: + response to the query + """ + try: + self.cursor.execute(query) + self.mydb.commit() + return self.cursor.fetchall() + except sqlite3.Error as err: + raise err diff --git a/report-tools/adaptors/sql/yaml_parser.py b/report-tools/adaptors/sql/yaml_parser.py new file mode 100755 index 0000000..b0ce15e --- /dev/null +++ b/report-tools/adaptors/sql/yaml_parser.py @@ -0,0 +1,116 @@ +############################################################################## +# Copyright (c) 2021, ARM Limited and Contributors. All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause +############################################################################## +""" +Class to parse .yaml file to generate a report.db +""" +import sys +import yaml +import sqlite + + +class YAMLParser: + """ + Class to represent a YAML Parser and creates database + + Methods: + create_table: Creates sqlite db table with necessary fields. + parse_file: Parses the yaml file to obtain necessary data for the test result table and updates it. + update_test_config_table: Parses the yaml file to obtain necessary data fot the test config table and updates it + """ + root_string = "" + test_suite_list = [] + + # contents of the test_config table + test_config_table = [ + "build_id", + "target", + "bitbake_version", + "yocto_version" + ] + + # contents of test_result table + test_result_table = [ + "build_id", + "date", + "test_suite", + "test_case", + "result" + ] + + def __init__(self, file_name=sys.argv[1]): + """Creates an instance for sqlite_obj and loads the contents of the yamlfile to be parsed """ + + try: + self.sqlite_obj = sqlite.Database("report.db") + with open(file_name) as file: + self.contents = yaml.load(file, Loader=yaml.FullLoader) + self.root_string = [i for i in self.contents.keys()][0] + except Exception as err: + print(err) + + def create_table(self): + """Creates empty tables in the sqlite database from the contents of test_config_table and test_result_table""" + + test_config_query = """ + CREATE TABLE `test_configuration` ( + {0} TEXT, + {1} TEXT, + {2} TEXT, + {3} TEXT, + PRIMARY KEY ({0}) + ); + """.format(self.test_config_table[0], self.test_config_table[1], self.test_config_table[2], + self.test_config_table[3]) + + test_results_query = """ + CREATE TABLE `test_results` ( + {0} TEXT, + {1} TEXT, + {2} TEXT, + {3} TEXT, + {4} TEXT, + FOREIGN KEY ({0}) REFERENCES `test_configuration`({0}) + ); + """.format(self.test_result_table[0], self.test_result_table[1], self.test_result_table[2], + self.test_result_table[3], self.test_result_table[4]) + + self.sqlite_obj.execute_query(test_config_query) + self.sqlite_obj.execute_query(test_results_query) + + def parse_file(self): + """Parses the yaml file""" + + build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID'] + # dependent on the generated yaml file. Code will be uncommented based + # on the yaml file + # self.contents[self.root_string]['metadata']['CI_COMMIT_TIMESTAMP'] + date = "" + for test_suite in self.contents[self.root_string]['test-suites'].keys(): + for test_case in self.contents[self.root_string]['test-suites'][test_suite]['test-results'].keys(): + result = self.contents[self.root_string]['test-suites'][test_suite]['test-results'][test_case]["status"] + update_result_table_query = "INSERT INTO test_results VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')". \ + format(build_id, date, test_suite, test_case, result) + self.sqlite_obj.execute_query(update_result_table_query) + + def update_test_config_table(self): + """Updates tables in the report.db with the values from the yaml file""" + + build_id = self.contents[self.root_string]['metadata']['CI_PIPELINE_ID'] + target = self.contents[self.root_string]['target']['platform'] + \ + "_" + self.contents[self.root_string]['target']['version'] + + bitbake_version = "1.0" + yocto_version = "2.0" + update_table_query = "INSERT INTO test_configuration VALUES ('{0}', '{1}', '{2}', '{3}')".\ + format(build_id, target, bitbake_version, yocto_version) + self.sqlite_obj.execute_query(update_table_query) + + +if __name__ == "__main__": + yaml_obj = YAMLParser() + yaml_obj.create_table() + yaml_obj.parse_file() + yaml_obj.update_test_config_table() -- GitLab