1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2024 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import subprocess 21import time 22 23 24def get_time_stamp(): 25 return int(round(time.time() * 1000)) 26 27 28def print_success(info): 29 print("\033[32m{}\033[0m".format(info)) 30 31 32def print_failure(info): 33 print("\033[31m{}\033[0m".format(info)) 34 35 36def print_info(info): 37 print("\033[33m{}\033[0m".format(info)) 38 39 40def is_subsequence(first_file, second_file): 41 first_info = first_file.read() 42 second_info = second_file.readline() 43 while second_info: 44 if first_info.find(second_info) == -1: 45 print("line\n", second_info, "is not in output file") 46 return False 47 second_info = second_file.readline() 48 return True 49 50 51def content_compare(file1, file2, lhd, rhd): 52 line_num = 0 53 for line1, line2 in zip(lhd, rhd): 54 line_num += 1 55 if line1 != line2: 56 print_failure(f"{file1}:{line_num}:{line1}") 57 print_failure(f"{file2}:{line_num}:{line2}") 58 return False 59 return True 60 61 62def compare_file(first_file_path, second_file_path): 63 with open(first_file_path, 'r') as first_file: 64 with open(second_file_path, 'r') as second_file: 65 return content_compare(first_file_path, second_file_path, first_file.readlines(), second_file.readlines()) 66 67 68def compare_target_files(first_file_path, second_file_path): 69 first_files_list = get_all_files(first_file_path) 70 second_files_list = get_all_files(second_file_path) 71 72 first_files = set([file[len(first_file_path):] for file in first_files_list]) 73 second_files = set([file[len(second_file_path):-4] for file in second_files_list]) 74 if len(first_files) != len(second_files): 75 print(f"[ERROR] result file num({len(first_files)}) != expect file num({len(second_files)})") 76 return False 77 78 common_files = first_files & second_files 79 80 for files in common_files: 81 if not compare_file("{}{}".format(first_file_path, files), "{}{}.txt".format(second_file_path, files)): 82 print("file ", "{}{}".format(first_file_path, files), "{}{}.txt".format(second_file_path, files), \ 83 "is different") 84 return False 85 return True 86 87 88def exec_command(command): 89 return subprocess.getstatusoutput(command) 90 91 92def file_exists(file_path): 93 return os.path.isfile(file_path) 94 95 96def make_binary_file(file_path): 97 print("making idl-gen...") 98 return exec_command("make --directory={} --jobs=4".format(file_path)) 99 100 101def clean_binary_file(file_path): 102 return exec_command("make --directory={} clean".format(file_path)) 103 104 105def get_all_files(path): 106 file_list = [] 107 items = os.listdir(path) 108 for item in items: 109 item_path = os.path.join(path, item) 110 if not os.path.isdir(item_path): 111 file_list.append(item_path) 112 else: 113 file_list += get_all_files(item_path) 114 return file_list 115 116 117def get_all_idl_files(idl_path): 118 file_list = get_all_files(idl_path) 119 idl_file_list = [] 120 for file in file_list: 121 if os.path.splitext(file)[-1] == ".idl": 122 idl_file_list.append(file) 123 return idl_file_list 124 125 126def get_idl(): 127 current_path = os.path.dirname(os.path.abspath(__file__)) 128 relative_path = os.path.join(current_path, get_idl_name()) 129 return os.path.realpath(relative_path) 130 131 132def get_idl_name(): 133 idl_name = 'idl-gen' 134 if os.name == 'nt': 135 idl_name = 'idl-gen.exe' 136 elif os.name == 'posix': 137 idl_name = 'idl-gen' 138 return idl_name 139 140 141def get_subclasses(cls): 142 return cls.__subclasses__() 143