1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024-2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16""" 17 18import os 19import sys 20import hashlib 21import json5 22 23 24class HvigorChecker: 25 26 HVIGOR_BASE_VERSION = [ 27 '4.0.5', 28 '5.0.0' 29 ] 30 31 def __init__(self, suite_name): 32 self._current_dir = os.path.dirname(os.path.realpath(__file__)) 33 self._suite_name = suite_name 34 self._xts_root_dir = os.path.realpath(os.path.join(self._current_dir, '../..', suite_name)) 35 36 def get_file_md5(self, file_path): 37 hash_md5 = hashlib.md5() 38 with open(file_path, "rb") as f: 39 for chunk in iter(lambda: f.read(4096), b""): 40 hash_md5.update(chunk) 41 return hash_md5.hexdigest() 42 43 def get_hvigor_version(self, json_file): 44 with open(json_file, 'r') as f: 45 data = json5.load(f) 46 version = data.get('hvigorVersion') 47 if version: 48 return version 49 return data.get('modelVersion') 50 51 def output_unmatched_project(self, prject_list, filename): 52 print("") 53 print("Error: The {} in the following directory does not meet the requirements:".format(filename)) 54 for prj in prject_list: 55 print(prj[0], prj[1]) 56 57 def check_hvigor_wrapper_js(self, hvigor_prj_list): 58 unmatch_info = [] 59 baseline_file = os.path.join(self._current_dir, 'hvigor-wrapper.js') 60 baseline_md5 = self.get_file_md5(os.path.join(baseline_file)) 61 for dir in hvigor_prj_list: 62 filename = os.path.join(dir, 'hvigor', 'hvigor-wrapper.js') 63 if not os.path.exists(filename): 64 return True 65 md5 = self.get_file_md5(filename) 66 if md5 != baseline_md5: 67 unmatch_info.append((md5, filename)) 68 69 if len(unmatch_info): 70 self.output_unmatched_project(unmatch_info, 'hvigor-wrapper.js') 71 print('Please copy from {}'.format(baseline_file)) 72 return False 73 return True 74 75 def check_hvigor_version(self, hvigor_prj_list): 76 unmatch_prj_list = [] 77 for dir in hvigor_prj_list: 78 filename = os.path.join(dir, 'hvigor', 'hvigor-config.json5') 79 version = self.get_hvigor_version(filename) 80 if version not in self.HVIGOR_BASE_VERSION: 81 unmatch_prj_list.append((version, filename)) 82 83 if len(unmatch_prj_list): 84 self.output_unmatched_project(unmatch_prj_list, 'hvigor-config.json5') 85 print("Plesse use {}".format(self.HVIGOR_BASE_VERSION)) 86 return False 87 return True 88 89 def check_hvigorw_bat(self, hvigor_prj_list): 90 unmatch_info = [] 91 baseline_file = os.path.join(self._current_dir, 'hvigorw.bat') 92 baseline_md5 = self.get_file_md5(os.path.join(baseline_file)) 93 for dir in hvigor_prj_list: 94 filename = os.path.join(dir, 'hvigorw.bat') 95 if not os.path.exists(filename): 96 return True 97 md5 = self.get_file_md5(filename) 98 if md5 != baseline_md5: 99 unmatch_info.append((md5, filename)) 100 101 if len(unmatch_info): 102 self.output_unmatched_project(unmatch_info, 'hvigorw.bat') 103 print('Please copy from {}'.format(baseline_file)) 104 return False 105 return True 106 107 def get_hvigor_prject_list(self): 108 hvigor_prj_list = [] 109 for root, dirs, files in os.walk(self._xts_root_dir): 110 if '.cxx' in dirs: 111 dirs.remove('.cxx') 112 for dir in dirs: 113 if dir == 'hvigor': 114 hvigor_prj_list.append(root) 115 return hvigor_prj_list 116 117 118def main(): 119 suite_name = "" 120 if 'XTS_SUITENAME' in os.environ: 121 suite_name = os.environ.get('XTS_SUITENAME') 122 elif 'xts_suitename' in os.environ: 123 suite_name = os.environ.get('xts_suitename') 124 else: 125 suite_name = sys.argv[1] 126 127 obj = HvigorChecker(suite_name) 128 129 hvigor_prj_list = obj.get_hvigor_prject_list() 130 131 js_valid = obj.check_hvigor_wrapper_js(hvigor_prj_list) 132 json_valid = obj.check_hvigor_version(hvigor_prj_list) 133 bat_valid = obj.check_hvigorw_bat(hvigor_prj_list) 134 135 if not js_valid or not json_valid or not bat_valid: 136 return 1 137 return 0 138 139 140if __name__ == "__main__": 141 sys.exit(main()) 142