1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import argparse 17import os 18import sys 19 20from util.file_utils import read_json_file 21from util.build_utils import touch, check_instance 22 23 24def read_hap_file(file_path: str): 25 if not os.path.exists(file_path): 26 raise Exception(f'the file: {file_path} is not exist') 27 hvigor_compile_hap_allow_info = read_json_file(file_path) 28 return hvigor_compile_hap_allow_info 29 30 31def check_sdk_version(args, hvigor_compile_hap_allow_info): 32 if not args.sdk_home or not hvigor_compile_hap_allow_info: 33 return 0 34 35 sdk_allow_version_list = hvigor_compile_hap_allow_info.get("sdk_version") 36 check_instance(sdk_allow_version_list, "sdk_version", dict) 37 38 message = "The hap path name: '{}' use the sdk version: '{}', but it is not exist in whitelist, please check target: '{}'".format(args.target_path, args.sdk_home, args.hvigor_compile_hap_allow_file) 39 40 if args.sdk_home not in sdk_allow_version_list: 41 return 0 42 43 if args.target_path not in sdk_allow_version_list.get(args.sdk_home): 44 raise Exception(message) 45 46 return 0 47 48 49def check_hvigor_version(args, hvigor_compile_hap_allow_info): 50 if not args.hvigor_home or not hvigor_compile_hap_allow_info: 51 return 0 52 53 hvigor_allow_version_list = hvigor_compile_hap_allow_info.get("hvigor_version") 54 check_instance(hvigor_allow_version_list, "hvigor_version", dict) 55 56 message = "The hap path name: '{}' use the hvigor version: '{}', but it is not exist in whitelist, please check target: '{}'".format(args.target_path, args.hvigor_home, args.hvigor_compile_hap_allow_file) 57 58 if args.hvigor_home not in hvigor_allow_version_list: 59 return 0 60 61 if args.target_path not in hvigor_allow_version_list.get(args.hvigor_home): 62 raise Exception(message) 63 64 return 0 65 66 67def main(): 68 parser = argparse.ArgumentParser() 69 parser.add_argument('--target-path', required=True, default='') 70 parser.add_argument('--output', required=True) 71 parser.add_argument('--hvigor-compile-hap-allow-file', required=True) 72 parser.add_argument('--sdk-home', help='sdk home') 73 parser.add_argument('--hvigor-home', help='hvigor home') 74 args = parser.parse_args() 75 76 # read hvigor compile hap allow list file 77 hvigor_compile_hap_allow_info = read_hap_file(args.hvigor_compile_hap_allow_file) 78 79 # check sdk version 80 check_sdk_version(args, hvigor_compile_hap_allow_info) 81 82 # check hvigor version 83 check_hvigor_version(args, hvigor_compile_hap_allow_info) 84 85 touch(args.output) 86 87 return 0 88 89 90if __name__ == '__main__': 91 sys.exit(main()) 92