########################################################################### # # Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file utils.py # # @author (last) LTTS # @date (last) 08-July-2022 # ############################################################################ import os import csv import test from configuration import config from datetime import timezone from datetime import datetime def get_current_date_and_time_in_utc(date_format='%Y/%m/%d - %H:%M:%S'): """ Method to get current date and time. @input date_format (str) - format of date to be retrieved. @return (str) - date in specified format. """ date = datetime.now(timezone.utc) return str(date.strftime(date_format)) def append_cloudsync_credentials_file(): """ This function is the handler for getting file from log folder. Application log file is automatically created on '/home/denali/Desktop/sd-card/cloudsync/ {current_date}_out.log' @return latest_file - (string) returns latest file that append on log folder from sd-data """ try: os.makedirs(config.CLOUD_CREDENTIALS_LOCATION, exist_ok = True) test.log("Directory created successfully") for file_handler in range(len(config.PEM_FILES)): path = os.path.join(config.CLOUD_CREDENTIALS_LOCATION, config.PEM_FILES[file_handler]) with open(path, 'w') as file_reader: pass except OSError as error: test.log("Directory can not be created") def get_cloud_sync_input_file(): """ This function is the handler for getting file from log folder. Application log file is automatically created on '/home/denali/Desktop/sd-card/cloudsync/ {current_date}_inp.log' @return latest_file - (string) returns latest file that append on log folder from sd-data """ try: current_date = get_current_date_and_time_in_utc(date_format = "%Y_%m_%d") latest_file = config.INP_BUF_FILE_LOCATION+current_date+'_inp.buf' return latest_file except: return False def retrive_log_data(readline_count = 1): """ This function is the handler for getting file from log folder. Application log data is automatically appended on '/home/denali/Desktop/sd-card/cloudsync/ {current_date}_out.log' @return latest_file - (list) returns latest file that append on log folder from sd-data """ cloudsync_data = [] count = 0 file_name = get_cloud_sync_input_file() try: with open(file_name,mode = 'r') as filereader: contents = csv.reader(filereader) try: for reader in reversed(list(contents)): if readline_count == count: return cloudsync_data cloudsync_data.append(reader) count = count + 1 except: test.fail("application log data is corrupted") except: test.fail("Log file is not created or log file is not created based on standard log naming format.") def get_epoch_value_consistancy(current_epoch_value, expected_epoch_value): """ This function is verify consistancy of epoch value. @input current_epoch_value (float) - current epoch time @input expected_epoch_value (str) - epoch time from the cloud-sync log. @return (bool) - True/False """ expected_epoch_value = int(expected_epoch_value) maximum_epoch_value = int(current_epoch_value + 25) minimum_epoch_value = int(current_epoch_value - 25) if expected_epoch_value > minimum_epoch_value and expected_epoch_value < maximum_epoch_value : return True return False