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 '4.0.9', 29 '5.0.0' 30 ] 31 32 def __init__(self, suite_name): 33 self._current_dir = os.path.dirname(os.path.realpath(__file__)) 34 self._suite_name = suite_name 35 self._xts_root_dir = os.path.realpath(os.path.join(self._current_dir, '../..', suite_name)) 36 37 def get_file_md5(self, file_path): 38 hash_md5 = hashlib.md5() 39 with open(file_path, "rb") as f: 40 for chunk in iter(lambda: f.read(4096), b""): 41 hash_md5.update(chunk) 42 return hash_md5.hexdigest() 43 44 def get_hvigor_version(self, json_file): 45 with open(json_file, 'r') as f: 46 data = json5.load(f) 47 version = data.get('hvigorVersion') 48 if version: 49 return version 50 return data.get('modelVersion') 51 52 def output_unmatched_project(self, prject_list, filename): 53 print("") 54 print("Error: The {} in the following directory does not meet the requirements:".format(filename)) 55 for prj in prject_list: 56 print(prj[0], prj[1]) 57 58 def check_hvigor_wrapper_js(self, hvigor_prj_list): 59 unmatch_info = [] 60 baseline_file = os.path.join(self._current_dir, 'hvigor-wrapper.js') 61 baseline_md5 = self.get_file_md5(os.path.join(baseline_file)) 62 for dir in hvigor_prj_list: 63 filename = os.path.join(dir, 'hvigor', 'hvigor-wrapper.js') 64 if not os.path.exists(filename): 65 return True 66 md5 = self.get_file_md5(filename) 67 if md5 != baseline_md5: 68 unmatch_info.append((md5, filename)) 69 70 if len(unmatch_info): 71 self.output_unmatched_project(unmatch_info, 'hvigor-wrapper.js') 72 print('Please copy from {}'.format(baseline_file)) 73 return False 74 return True 75 76 def check_hvigor_version(self, hvigor_prj_list): 77 unmatch_prj_list = [] 78 for dir in hvigor_prj_list: 79 filename = os.path.join(dir, 'hvigor', 'hvigor-config.json5') 80 version = self.get_hvigor_version(filename) 81 if version not in self.HVIGOR_BASE_VERSION: 82 unmatch_prj_list.append((version, filename)) 83 84 if len(unmatch_prj_list): 85 self.output_unmatched_project(unmatch_prj_list, 'hvigor-config.json5') 86 print("Plesse use {}".format(self.HVIGOR_BASE_VERSION)) 87 return False 88 return True 89 90 def check_hvigorw_bat(self, hvigor_prj_list): 91 unmatch_info = [] 92 baseline_file = os.path.join(self._current_dir, 'hvigorw.bat') 93 baseline_md5 = self.get_file_md5(os.path.join(baseline_file)) 94 for dir in hvigor_prj_list: 95 filename = os.path.join(dir, 'hvigorw.bat') 96 if not os.path.exists(filename): 97 return True 98 md5 = self.get_file_md5(filename) 99 if md5 != baseline_md5: 100 unmatch_info.append((md5, filename)) 101 102 if len(unmatch_info): 103 self.output_unmatched_project(unmatch_info, 'hvigorw.bat') 104 print('Please copy from {}'.format(baseline_file)) 105 return False 106 return True 107 108 def get_hvigor_prject_list(self): 109 hvigor_prj_list = [] 110 for root, dirs, files in os.walk(self._xts_root_dir): 111 if '.cxx' in dirs: 112 dirs.remove('.cxx') 113 for dir in dirs: 114 if dir == 'hvigor': 115 hvigor_prj_list.append(root) 116 return hvigor_prj_list 117 118 119def main(): 120 suite_name = "" 121 if 'XTS_SUITENAME' in os.environ: 122 suite_name = os.environ.get('XTS_SUITENAME') 123 elif 'xts_suitename' in os.environ: 124 suite_name = os.environ.get('xts_suitename') 125 else: 126 suite_name = sys.argv[1] 127 128 obj = HvigorChecker(suite_name) 129 130 hvigor_prj_list = obj.get_hvigor_prject_list() 131 132 js_valid = obj.check_hvigor_wrapper_js(hvigor_prj_list) 133 json_valid = obj.check_hvigor_version(hvigor_prj_list) 134 bat_valid = obj.check_hvigorw_bat(hvigor_prj_list) 135 136 if not js_valid or not json_valid or not bat_valid: 137 return 1 138 return 0 139 140 141if __name__ == "__main__": 142 sys.exit(main()) 143