1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 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 json 17import os 18import sys 19import zipfile 20import argparse 21import time 22 23sys.path.append( 24 os.path.dirname( 25 os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) 26from scripts.util.file_utils import read_json_file # noqa: E402 27 28sdk_package_list = [] 29sdk_package_set = set() 30sdk_package_location = "" 31product_name = "" 32sdk_version = "" 33 34 35def parse_sdk_check_list(sdk_check_list: list): 36 sdk_check_files = [] 37 sdk_check_directories = [] 38 sdk_delivery_list = read_json_file(sdk_check_list) 39 40 if sdk_delivery_list is None: 41 raise Exception("read file '{}' failed.".format(sdk_check_list)) 42 43 if sys.platform == 'linux': 44 if sdk_platform == 'default': 45 sdk_check_files = sdk_delivery_list['linux']['checkFiles'] 46 sdk_check_directories = sdk_delivery_list['linux']['checkDirectories'] 47 win_sdk_check_files = sdk_delivery_list['windows']['checkFiles'] 48 win_sdk_check_directories = sdk_delivery_list['windows']['checkDirectories'] 49 sdk_check_files.extend(win_sdk_check_files) 50 sdk_check_directories.extend(win_sdk_check_directories) 51 elif sdk_platform == 'win': 52 sdk_check_files = sdk_delivery_list['windows']['checkFiles'] 53 sdk_check_directories = sdk_delivery_list['windows']['checkDirectories'] 54 elif sdk_platform == 'linux': 55 sdk_check_files = sdk_delivery_list['linux']['checkFiles'] 56 sdk_check_directories = sdk_delivery_list['linux']['checkDirectories'] 57 else: 58 sdk_check_files = sdk_delivery_list['darwin']['checkFiles'] 59 sdk_check_directories = sdk_delivery_list['darwin']['checkDirectories'] 60 61 return sdk_check_files, sdk_check_directories 62 63 64def add_files_to_sdk_package(sdk_package_directory: dict, compressed_file: str): 65 archive_file = os.path.join(sdk_package_directory, compressed_file) 66 is_zipfile = zipfile.is_zipfile(archive_file) 67 if is_zipfile: 68 sdk_package_path = sdk_package_directory[sdk_package_directory.find(product_name) + len(product_name) + 1:] 69 zip_file = zipfile.ZipFile(archive_file, 'r') 70 global sdk_package_list 71 zip_file_path = [] 72 zip_file_namelist = zip_file.namelist() 73 for zip_file in zip_file_namelist: 74 zip_file_path.append(os.path.join(sdk_package_path, zip_file)) 75 sdk_package_list.extend(zip_file_path) 76 else: 77 raise Exception("Error: {} is not zip".format(archive_file)) 78 79 80def get_sdk_package_directories(): 81 if sys.platform == 'linux': 82 if sdk_platform == 'default': 83 os_types = ['linux', 'windows'] 84 elif sdk_platform == 'win': 85 os_types = ['windows'] 86 elif sdk_platform == 'linux': 87 os_types = ['linux'] 88 else: 89 os_types = ['darwin'] 90 91 cur_path = os.path.dirname(os.path.realpath(__file__)) 92 sdk_package_location_prefix = cur_path[0:cur_path.find('build')] 93 sdk_package_directories = [] 94 95 for os_type in os_types: 96 sdk_package_directories.append(os.path.join(sdk_package_location_prefix, sdk_package_location, os_type)) 97 for sdk_package_directory in sdk_package_directories: 98 if not os.path.exists(sdk_package_directory): 99 raise Exception("Error: directory {} not exits!".format(sdk_package_directory)) 100 101 return sdk_package_directories 102 103 104def get_all_sdk_package_list(): 105 global sdk_package_list 106 compressed_file_dict = {} 107 sdk_package_directories = get_sdk_package_directories() 108 for sdk_package_directory in sdk_package_directories: 109 for file_name in os.listdir(sdk_package_directory): 110 if file_name.endswith(".zip") and file_name.find(sdk_version) != -1: 111 compressed_file_dict.setdefault(sdk_package_directory, []).append(file_name) 112 if sdk_package_directory in compressed_file_dict.keys(): 113 for compressed_file in compressed_file_dict[sdk_package_directory]: 114 add_files_to_sdk_package(sdk_package_directory, compressed_file) 115 else: 116 raise Exception("Error: {} not in {}, please check.".format(sdk_package_directory, compressed_file_dict)) 117 118 return sdk_package_list 119 120 121def get_redundant_set(sdk_check_list: list): 122 outside_the_list_set = set() 123 sdk_check_files, sdk_check_directories = parse_sdk_check_list(sdk_check_list) 124 sdk_list_set = set(sdk_check_files) 125 sym_intersection = sdk_package_set.symmetric_difference(sdk_list_set) 126 remove_check_from_package_set = sdk_package_set.intersection(sym_intersection) 127 sdk_directorys_tuple = tuple(map(str, sdk_check_directories)) 128 for file in remove_check_from_package_set: 129 if not file.startswith(sdk_directorys_tuple): 130 outside_the_list_set.add(file) 131 132 return outside_the_list_set 133 134 135def get_unpacked_directories(sdk_check_list: list): 136 sdk_check_directories = parse_sdk_check_list(sdk_check_list)[1] 137 sdk_check_directories_set = set(sdk_check_directories) 138 for directory in sdk_check_directories: 139 for file in sdk_package_list: 140 if file.startswith(directory): 141 sdk_check_directories_set.discard(directory) 142 break 143 144 return sdk_check_directories_set 145 146 147def get_missing_set(sdk_check_list: list): 148 sdk_list_set = set(parse_sdk_check_list(sdk_check_list)[0]) 149 sym_intersection = sdk_package_set.symmetric_difference(sdk_list_set) 150 missing_set = sdk_list_set.intersection(sym_intersection) 151 sdk_unpacked_directories = get_unpacked_directories(sdk_check_list) 152 if len(sdk_unpacked_directories) != 0: 153 for directory in sdk_unpacked_directories: 154 missing_set.add(directory) 155 156 return missing_set 157 158 159def output_the_verification_result(sdk_check_list: list): 160 sdk_package_missing_set = get_missing_set(sdk_check_list) 161 sdk_package_redundant_set = get_redundant_set(sdk_check_list) 162 163 if len(sdk_package_missing_set) == 0 and len(sdk_package_redundant_set) == 0: 164 print("package and verify successful!") 165 else: 166 if len(sdk_package_missing_set) != 0 and len(sdk_package_redundant_set) != 0: 167 print("SDK package is less than SDK delivery list, missing: {}.".format(sdk_package_missing_set)) 168 print("SDK package is more than SDK delivery list, extra: {}.".format(sdk_package_redundant_set)) 169 sys.exit(1) 170 elif len(sdk_package_missing_set) != 0: 171 print("SDK package is less than SDK delivery list, missing: {}.".format(sdk_package_missing_set)) 172 sys.exit(1) 173 else: 174 print("SDK package is more than SDK delivery list, extra: {}.".format(sdk_package_redundant_set)) 175 sys.exit(1) 176 177 178def main(): 179 parser = argparse.ArgumentParser() 180 parser.add_argument('--sdk-delivery-list') 181 parser.add_argument('root_build_dir') 182 parser.add_argument('--sdk-archive-dir') 183 parser.add_argument('product_name') 184 parser.add_argument('sdk_version') 185 parser.add_argument('--sdk-platform') 186 options = parser.parse_args() 187 188 sdk_check_list = options.sdk_delivery_list 189 root_build_directory = options.root_build_dir[2:] 190 global sdk_package_location 191 sdk_package_location = os.path.join(root_build_directory, options.sdk_archive_dir) 192 global product_name 193 product_name = options.product_name 194 global sdk_version 195 sdk_version = options.sdk_version 196 global sdk_platform 197 sdk_platform = options.sdk_platform 198 199 global sdk_package_set 200 sdk_package_set = set(get_all_sdk_package_list()) 201 output_the_verification_result(sdk_check_list) 202 203 204if __name__ == '__main__': 205 sys.exit(main()) 206