diff --git a/tir/technologies/core/config.py b/tir/technologies/core/config.py index c808e10d..20e91cf1 100644 --- a/tir/technologies/core/config.py +++ b/tir/technologies/core/config.py @@ -1,163 +1,174 @@ import json import os from datetime import datetime +import sys class ConfigLoader: """ This class is instantiated to contain all config information used throughout the execution of the methods. """ + + _json_data = None + def __init__(self, path="config.json"): - valid = os.path.isfile(path) + if ConfigLoader._json_data is None: + + if not path: + path = os.path.join(sys.path[0], r"config.json") + + valid = os.path.isfile(path) + + if valid: + with open(path) as json_data_file: + try: + data = json.load(json_data_file) + if self.validar_chaves(data): + raise Exception(self.validar_chaves(data)) + ConfigLoader._json_data = data + except Exception as e: + raise Exception(f"JSON file issue: {e}. \n* Please check your config.json *") - if valid: - with open(path) as json_data_file: - try: - data = json.load(json_data_file) - if self.validar_chaves(data): - raise Exception(self.validar_chaves(data)) - except Exception as e: - raise Exception(f"JSON file issue: {e}. \n* Please check your config.json *") - else: - data = {} + if ConfigLoader._json_data: - today = datetime.today() + data = ConfigLoader._json_data - self.json_data = data - self.autostart = True - self.ipExec = str(data["ipExec"]) if "ipExec" in data else "" - self.url_set_start_exec = str(data["UrlSetStartExec"]) if "UrlSetStartExec" in data else "" - self.url_set_end_exec = str(data["UrlSetEndExec"]) if "UrlSetEndExec" in data else "" - self.screenshot = bool(data["ScreenShot"]) if "ScreenShot" in data else True - self.country = str(data["Country"]) if "Country" in data else "BRA" - self.execution_id = str(data["ExecId"]) if "ExecId" in data else today.strftime('%Y%m%d') - self.num_exec = str(data["NumExec"]) if "NumExec" in data else "" - self.issue = str(data["MotExec"]) if "MotExec" in data else "" - self.url = str(data["Url"]) if "Url" in data else "" - self.browser = str(data["Browser"]) if "Browser" in data else "" - self.environment = str(data["Environment"]) if "Environment" in data else "" - self.user = str(data["User"]) if "User" in data else "" - self.password = str(data["Password"]) if "Password" in data else "" - self.language = str(data["Language"]) if "Language" in data else "" - self.skip_environment = ("SkipEnvironment" in data and bool(data["SkipEnvironment"])) - self.headless = ("Headless" in data and bool(data["Headless"])) - self.log_folder = str(data["LogFolder"]) if "LogFolder" in data else "" - self.log_file = ("LogFile" in data and bool(data["LogFile"])) - self.debug_log = ("DebugLog" in data and bool(data["DebugLog"])) - self.time_out = int(data["TimeOut"]) if "TimeOut" in data else 90 - self.parameter_menu = str(data["ParameterMenu"]) if "ParameterMenu" in data else "" - self.screenshot_folder = str(data["ScreenshotFolder"]) if "ScreenshotFolder" in data else "" - self.coverage = ("Coverage" in data and bool(data["Coverage"])) - self.skip_restart = ("SkipRestart" in data and bool(data["SkipRestart"])) - self.smart_test = ("SmartTest" in data and bool(data["SmartTest"])) - self.smart_erp = ("SmartERP" in data and bool(data["SmartERP"])) - self.valid_language = self.language != "" - self.initial_program = "" - self.routine = "" - self.date = "" - self.group = "" - self.branch = "" - self.module = "" - self.user_cfg = str(data["UserCfg"]) if "UserCfg" in data else "" - self.password_cfg = str(data["PasswordCfg"]) if "PasswordCfg" in data else "" - self.electron_binary_path = (str(data["BinPath"]) if "BinPath" in data else "") - self.csv_path = (str(data["CSVPath"]) if "CSVPath" in data else "") - self.database_driver = str(data["DBDriver"]) if "DBDriver" in data else "" - self.database_server = str(data["DBServer"]) if "DBServer" in data else "" - self.database_port = str(data["DBPort"]) if "DBPort" in data else "" - self.database_name = str(data["DBName"]) if "DBName" in data else "" - self.database_user = str(data["DBUser"]) if "DBUser" in data else "" - self.database_password = str(data["DBPassword"]) if "DBPassword" in data else "" - self.dbq_oracle_server = str(data["DBQOracleServer"]) if "DBQOracleServer" in data else "" - self.url_tss = str(data["URL_TSS"]) if "URL_TSS" in data else "" - self.start_program = str(data["StartProgram"]) if "StartProgram" in data else "" - self.new_log = ("NewLog" in data and bool(data["NewLog"])) - self.logurl1 = str(data["LogUrl1"]) if "LogUrl1" in data else "" - self.logurl2 = str(data["LogUrl2"]) if "LogUrl2" in data else "" - self.parameter_url = bool(data["ParameterUrl"]) if "ParameterUrl" in data else False - self.log_http = str(data["LogHttp"]) if "LogHttp" in data else "" - self.baseline_spool = str(data["BaseLine_Spool"]) if "BaseLine_Spool" in data else "" - self.check_value = (bool(data["CheckValue"]) if "CheckValue" in data else None) - self.poui_login = bool(data["POUILogin"]) if "POUILogin" in data else False - self.poui = bool(data["POUI"]) if "POUI" in data else False - self.log_info_config = bool(data["LogInfoConfig"]) if "LogInfoConfig" in data else False - self.release = str(data["Release"]) if "Release" in data else "12.1.2210" - self.top_database = str(data["TopDataBase"]) if "TopDataBase" in data else "MSSQL" - self.lib_version = str(data["Lib"]) if "Lib" in data else "lib_version" - self.build_version = str(data["Build"]) if "Build" in data else "build_version" - self.appserver_folder = str(data["AppServerFolder"]) if "AppServerFolder" in data else "" - self.destination_folder = str(data["DestinationFolder"]) if "DestinationFolder" in data else "" - self.appserver_service = str(data["AppServerService"]) if "AppServerService" in data else "" - self.check_dump = ("CheckDump" in data and bool(data["CheckDump"])) - self.chromedriver_auto_install = ("ChromeDriverAutoInstall" in data and bool(data["ChromeDriverAutoInstall"])) - self.ssl_chrome_auto_install_disable = ( - "SSLChromeInstallDisable" in data and bool(data["SSLChromeInstallDisable"])) - self.data_delimiter = str(data["DataDelimiter"]) if "DataDelimiter" in data else "/" - self.procedure_menu = str(data["ProcedureMenu"]) if "ProcedureMenu" in data else "" + today = datetime.today() + self.json_data = data + self.autostart = True + self.ipExec = str(data["ipExec"]) if "ipExec" in data else "" + self.url_set_start_exec = str(data["UrlSetStartExec"]) if "UrlSetStartExec" in data else "" + self.url_set_end_exec = str(data["UrlSetEndExec"]) if "UrlSetEndExec" in data else "" + self.screenshot = bool(data["ScreenShot"]) if "ScreenShot" in data else True + self.country = str(data["Country"]) if "Country" in data else "BRA" + self.execution_id = str(data["ExecId"]) if "ExecId" in data else today.strftime('%Y%m%d') + self.num_exec = str(data["NumExec"]) if "NumExec" in data else "" + self.issue = str(data["MotExec"]) if "MotExec" in data else "" + self.url = str(data["Url"]) if "Url" in data else "" + self.browser = str(data["Browser"]) if "Browser" in data else "" + self.environment = str(data["Environment"]) if "Environment" in data else "" + self.user = str(data["User"]) if "User" in data else "" + self.password = str(data["Password"]) if "Password" in data else "" + self.language = str(data["Language"]) if "Language" in data else "" + self.skip_environment = ("SkipEnvironment" in data and bool(data["SkipEnvironment"])) + self.headless = ("Headless" in data and bool(data["Headless"])) + self.log_folder = str(data["LogFolder"]) if "LogFolder" in data else "" + self.log_file = ("LogFile" in data and bool(data["LogFile"])) + self.debug_log = ("DebugLog" in data and bool(data["DebugLog"])) + self.time_out = int(data["TimeOut"]) if "TimeOut" in data else 90 + self.parameter_menu = str(data["ParameterMenu"]) if "ParameterMenu" in data else "" + self.screenshot_folder = str(data["ScreenshotFolder"]) if "ScreenshotFolder" in data else "" + self.coverage = ("Coverage" in data and bool(data["Coverage"])) + self.skip_restart = ("SkipRestart" in data and bool(data["SkipRestart"])) + self.smart_test = ("SmartTest" in data and bool(data["SmartTest"])) + self.smart_erp = ("SmartERP" in data and bool(data["SmartERP"])) + self.valid_language = self.language != "" + self.initial_program = "" + self.routine = "" + self.date = "" + self.group = "" + self.branch = "" + self.module = "" + self.user_cfg = str(data["UserCfg"]) if "UserCfg" in data else "" + self.password_cfg = str(data["PasswordCfg"]) if "PasswordCfg" in data else "" + self.electron_binary_path = (str(data["BinPath"]) if "BinPath" in data else "") + self.csv_path = (str(data["CSVPath"]) if "CSVPath" in data else "") + self.database_driver = str(data["DBDriver"]) if "DBDriver" in data else "" + self.database_server = str(data["DBServer"]) if "DBServer" in data else "" + self.database_port = str(data["DBPort"]) if "DBPort" in data else "" + self.database_name = str(data["DBName"]) if "DBName" in data else "" + self.database_user = str(data["DBUser"]) if "DBUser" in data else "" + self.database_password = str(data["DBPassword"]) if "DBPassword" in data else "" + self.dbq_oracle_server = str(data["DBQOracleServer"]) if "DBQOracleServer" in data else "" + self.url_tss = str(data["URL_TSS"]) if "URL_TSS" in data else "" + self.start_program = str(data["StartProgram"]) if "StartProgram" in data else "" + self.new_log = ("NewLog" in data and bool(data["NewLog"])) + self.logurl1 = str(data["LogUrl1"]) if "LogUrl1" in data else "" + self.logurl2 = str(data["LogUrl2"]) if "LogUrl2" in data else "" + self.parameter_url = bool(data["ParameterUrl"]) if "ParameterUrl" in data else False + self.log_http = str(data["LogHttp"]) if "LogHttp" in data else "" + self.baseline_spool = str(data["BaseLine_Spool"]) if "BaseLine_Spool" in data else "" + self.check_value = (bool(data["CheckValue"]) if "CheckValue" in data else None) + self.poui_login = bool(data["POUILogin"]) if "POUILogin" in data else False + self.poui = bool(data["POUI"]) if "POUI" in data else False + self.log_info_config = bool(data["LogInfoConfig"]) if "LogInfoConfig" in data else False + self.release = str(data["Release"]) if "Release" in data else "12.1.2210" + self.top_database = str(data["TopDataBase"]) if "TopDataBase" in data else "MSSQL" + self.lib_version = str(data["Lib"]) if "Lib" in data else "lib_version" + self.build_version = str(data["Build"]) if "Build" in data else "build_version" + self.appserver_folder = str(data["AppServerFolder"]) if "AppServerFolder" in data else "" + self.destination_folder = str(data["DestinationFolder"]) if "DestinationFolder" in data else "" + self.appserver_service = str(data["AppServerService"]) if "AppServerService" in data else "" + self.check_dump = ("CheckDump" in data and bool(data["CheckDump"])) + self.chromedriver_auto_install = ("ChromeDriverAutoInstall" in data and bool(data["ChromeDriverAutoInstall"])) + self.ssl_chrome_auto_install_disable = ( + "SSLChromeInstallDisable" in data and bool(data["SSLChromeInstallDisable"])) + self.data_delimiter = str(data["DataDelimiter"]) if "DataDelimiter" in data else "/" + self.procedure_menu = str(data["ProcedureMenu"]) if "ProcedureMenu" in data else "" def validar_chaves(self, json_data): valid_keys = [ - "ipExec", - "UrlSetStartExec", - "UrlSetEndExec", - "ScreenShot", - "Country", - "ExecId", - "NumExec", - "MotExec", - "Url", - "Browser", - "Environment", - "User", - "Password", - "Language", - "SkipEnvironment", - "Headless", - "LogFolder", - "LogFile", - "DebugLog", - "TimeOut", - "ParameterMenu", - "ScreenshotFolder", - "Coverage", - "SkipRestart", - "SmartTest", - "SmartERP", - "UserCfg", - "PasswordCfg", - "BinPath", - "CSVPath", - "DBDriver", - "DBServer", - "DBPort", - "DBName", - "DBUser", - "DBPassword", - "DBQOracleServer", - "URL_TSS", - "StartProgram", - "NewLog", - "LogUrl1", - "LogUrl2", - "ParameterUrl", - "LogHttp", - "BaseLine_Spool", - "CheckValue", - "POUILogin", - "POUI", - "LogInfoConfig", - "Release", - "TopDataBase", - "Lib", - "Build", - "AppServerFolder", - "DestinationFolder", - "AppServerService", - "CheckDump", - "ChromeDriverAutoInstall", - "SSLChromeInstallDisable", - "DataDelimiter", - "ProcedureMenu" -] + "ipExec", + "UrlSetStartExec", + "UrlSetEndExec", + "ScreenShot", + "Country", + "ExecId", + "NumExec", + "MotExec", + "Url", + "Browser", + "Environment", + "User", + "Password", + "Language", + "SkipEnvironment", + "Headless", + "LogFolder", + "LogFile", + "DebugLog", + "TimeOut", + "ParameterMenu", + "ScreenshotFolder", + "Coverage", + "SkipRestart", + "SmartTest", + "SmartERP", + "UserCfg", + "PasswordCfg", + "BinPath", + "CSVPath", + "DBDriver", + "DBServer", + "DBPort", + "DBName", + "DBUser", + "DBPassword", + "DBQOracleServer", + "URL_TSS", + "StartProgram", + "NewLog", + "LogUrl1", + "LogUrl2", + "ParameterUrl", + "LogHttp", + "BaseLine_Spool", + "CheckValue", + "POUILogin", + "POUI", + "LogInfoConfig", + "Release", + "TopDataBase", + "Lib", + "Build", + "AppServerFolder", + "DestinationFolder", + "AppServerService", + "CheckDump", + "ChromeDriverAutoInstall", + "SSLChromeInstallDisable", + "DataDelimiter", + "ProcedureMenu" + ] keys_json = set(json_data.keys()) wrong_keys = keys_json - set(valid_keys) diff --git a/tir/technologies/core/logging_config.py b/tir/technologies/core/logging_config.py index 7c64eb69..f1338c97 100644 --- a/tir/technologies/core/logging_config.py +++ b/tir/technologies/core/logging_config.py @@ -8,98 +8,11 @@ import socket import inspect - -config = ConfigLoader() - filename = None folder = None file_path = None - -def logger(logger='root'): - """ - :return: - """ - - global filename - global folder - global file_path - - if config.smart_test or config.debug_log: - - logger = 'console' # TODO configuraĆ§Ć£o temporaria para o server. - - today = datetime.today() - - file_handler = True if logger == 'root' else False - - if not file_path and file_handler: - filename = f"TIR_{get_file_name('testsuite')}_{today.strftime('%Y%m%d%H%M%S%f')[:-3]}.log" - - folder = create_folder() - - file_path = create_file(folder, filename) - - logging_config = { - 'version': 1, - 'loggers': { - 'root': { # root logger - 'level': 'DEBUG', - 'handlers': ['debug_console_handler', 'debug_file_handler'] - }, - 'console': { # console logger - 'level': 'DEBUG', - 'handlers': ['debug_console_handler'] - } - }, - 'handlers': { - 'debug_console_handler': { - 'level': 'DEBUG', - 'formatter': 'info', - 'class': 'logging.StreamHandler', - 'stream': 'ext://sys.stdout', - }, - 'debug_file_handler': { - 'level': 'DEBUG', - 'formatter': 'info', - 'filename': Path(folder, filename) if file_handler else 'none.log', - 'class': 'logging.FileHandler', - 'mode': 'a' - }, - }, - 'formatters': { - 'info': { - 'format': '%(asctime)s-%(levelname)s-%(name)s-%(process)d::%(module)s::%(funcName)s|%(lineno)s:: %(message)s' - }, - }, - } - - else: - - logging_config = { - 'version': 1, - 'loggers': { - 'root': { # root logger - 'level': 'INFO', - 'handlers': ['debug_console_handler'] - }, - }, - 'handlers': { - 'debug_console_handler': { - 'level': 'INFO', - 'formatter': 'info', - 'class': 'logging.StreamHandler', - 'stream': 'ext://sys.stdout', - }, - }, - 'formatters': { - 'info': { - 'format': '%(asctime)s-%(levelname)s:: %(message)s' - }, - }, - } - - dictConfig(logging_config) - return logging.getLogger(logger) +config = None +_logger = None def get_file_name(file_name): """ @@ -125,36 +38,151 @@ def create_folder(): """ path = None + folder_path = None + error = None try: if config.log_http: - folder_path = Path(config.log_http, config.country, config.release, config.issue, - config.execution_id, get_file_name('testsuite')) - path = Path(folder_path) + folder_path = Path(config.log_http, config.country, config.release, config.issue,config.execution_id, get_file_name('testsuite')) + os.makedirs(Path(folder_path)) + elif config.log_folder: + folder_path = Path(config.log_folder) os.makedirs(Path(folder_path)) else: path = Path("Log", socket.gethostname()) os.makedirs(Path("Log", socket.gethostname())) - except OSError: - pass + except Exception as e: + error = e + + if folder_path: + path = str(Path(folder_path)) - return path + if os.path.isdir(path): + return str(path) + else: + raise Exception(f"Folder path not found: {path}: {str(error)}") -def create_file(folder, filename): +def create_file(): """ Creates an empty file before logger [Internal] """ + today = datetime.today() + + filename = f"TIR_{get_file_name('testsuite')}_{today.strftime('%Y%m%d%H%M%S%f')[:-3]}.log" + + folder = create_folder() + success = False - endtime = time.time() + config.time_out + error = None + endtime = time.time() + config.time_out while (time.time() < endtime and not success): try: with open(Path(folder, filename), "w", ): - return True - except Exception as error: - time.sleep(30) - logger().debug(str(error)) + return str(Path(folder, filename)) + except Exception as e: + time.sleep(5) + error = str(e) + print(error) + + if error: + return error + +def configure_logger(): + """ + :return: + """ + + global _logger + global filename + global folder + global file_path + global config + + config = ConfigLoader() + + if not config._json_data: + return + + logger_profile = 'user_console' + + if config.debug_log: + logger_profile = 'root' + else: + logger_profile = 'debug_console' if config.smart_test else logger_profile + + if logger_profile == 'root': + file_path = create_file() + + logging_config = { + 'version': 1, + 'formatters': { + 'debug': { + 'format': '%(asctime)s-%(levelname)s-%(name)s-%(process)d::%(module)s::%(funcName)s|%(lineno)s:: %(message)s' + }, + 'info':{ + 'format': '%(asctime)s-%(levelname)s:: %(message)s' + } + }, + 'handlers': { + 'debug_console_handler': { + 'level': 'DEBUG', + 'formatter': 'debug', + 'class': 'logging.StreamHandler', + 'stream': 'ext://sys.stdout', + }, + 'info_console_handler': { + 'level': 'INFO', + 'formatter': 'info', + 'class': 'logging.StreamHandler', + 'stream': 'ext://sys.stdout', + } + }, + 'loggers': { + 'root': { # root logger + 'level': 'DEBUG', + 'handlers': ['debug_console_handler'] + }, + 'debug_console': { # console logger + 'level': 'DEBUG', + 'handlers': ['debug_console_handler'] + }, + 'user_console': { # user console logger + 'level': 'INFO', + 'handlers': ['info_console_handler'] + }, + }, + } + + if file_path and os.path.exists(file_path): + logging_config['handlers']['memory_handler'] = { + 'level': 'DEBUG', + 'formatter': 'debug', + 'class': 'logging.handlers.MemoryHandler', + 'capacity': 5*1024*1024, + 'flushLevel': logging.CRITICAL, + 'target': 'debug_file_handler' + } + logging_config['handlers']['debug_file_handler'] = { + 'level': 'DEBUG', + 'formatter': 'debug', + 'class': 'logging.FileHandler', + 'filename': file_path, + 'mode': 'a' + } + logging_config['loggers']['root']['handlers'].append('memory_handler') + logging_config['loggers']['root']['handlers'].append('debug_file_handler') + + dictConfig(logging_config) + _logger = logging.getLogger(logger_profile) + +def logger(): + global _logger + if _logger is None: + configure_logger() + _logger.debug(f"Log file created: '{file_path}'") + return _logger