########################################################################### # # Copyright (c) 2019-2020 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 build_alarm_defs.py # # @author (last) Peter Lucia # @date (last) 05-Apr-2021 # @author (original) Peter Lucia # @date (original) 05-Apr-2021 # ############################################################################ import heapq import os import subprocess from datetime import datetime def create_enum_file(in_path: str, out_path:str, common_branch: str, author: str): """ Generates an enum source code file @param in_path: (str) input txt file @param out_path: (str) output .py file @param common_branch: (str) the common branch used to generate the alarms @param author: (str) name of author for header @return: True upon success, False upon failure """ if os.path.exists(in_path): enums = [] try: with open(in_path, 'r') as f: for line in f: split_str = line.split('=') enums.append((int(split_str[1].strip()), split_str[0].strip())) except FileNotFoundError as e: print("Error reading in txt file: {0}".format(e)) return False heapq.heapify(enums) current_time = datetime.now() code_header = \ """ ########################################################################### # # Copyright (c) 2019-{0} 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 alarm_defs.py # # @author (last) {3} # @date (last) {2}-{1}-{0} # @author (original) {3} # @date (original) {2}-{1}-{0} # ############################################################################""" \ .format(current_time.strftime("%Y"), current_time.strftime("%b"), current_time.strftime("%d"), author) code_imports = \ """ from enum import unique from ..utils.base import AlarmEnum""" code_class_def = \ """ # Branch: {0} @unique class AlarmList(AlarmEnum): """.format(common_branch) # build the resultant source code code_result = code_header code_result += code_imports code_result += code_class_def # pop the enums from the min heap until its empty code = "" while enums: enum = heapq.heappop(enums) code += " {0} = {1}\n".format(enum[1], enum[0]) code_result += code try: # write the generated file to disk with open(out_path, 'w') as f: f.write(code_result) return True except FileNotFoundError as e: print("Error writing source code: {0}".format(e)) return False else: print("Error: Input file {0} does not exist!".format(in_path)) return False def run(dst_alarms_txt: str, dst_common: str, common_branch: str, dst_python: str, author: str): """ Driver function to build alarm_defs.py @param dst_alarms_txt: (str) the intermediary alarms txt file to create @param dst_common: (str) where to temporarily clone common @param common_branch: (str) the common branch to clone @param dst_python: (str) the python source file to generate @param author: (str) the author's name to put in the header of the source file @return: None """ if subprocess.call("bash gen_alarm_ids.sh {0} {1} {2}".format(dst_alarms_txt, dst_common, common_branch), shell=True) != 0: print("Failure: Could not generate alarm ids.") return if create_enum_file(dst_alarms_txt, dst_python, common_branch, author): print("Created enum file: {0}".format(dst_python)) print("Done.") else: print("Failure: Could not create enum file.") if __name__ == '__main__': run(dst_alarms_txt="/tmp/AlarmIds.txt", dst_common="/tmp/common", common_branch="Sprint37", dst_python=os.path.join(os.path.abspath("../"), "dialin/common/alarm_defs.py"), author="Peter Lucia")