1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import argparse 18import os 19import shutil 20import subprocess 21import zipfile 22import shutil 23 24 25def parse_args(): 26 parser = argparse.ArgumentParser(description="Verify abc files in system app.") 27 parser.add_argument( 28 "--hap-dir", required=True, help="Path to the HAP files directory.") 29 parser.add_argument( 30 "--verifier-dir", required=True, help="Path to the ark_verifier directory.") 31 return parser.parse_args() 32 33 34def copy_and_rename_hap_files(hap_folder, out_folder): 35 for file_path in os.listdir(hap_folder): 36 if file_path.endswith(".hap"): 37 destination_path = os.path.join(out_folder, file_path.replace(".hap", ".zip")) 38 shutil.copy(os.path.join(hap_folder, file_path), destination_path) 39 40 41def extract_zip(zip_path, extract_folder): 42 try: 43 with zipfile.ZipFile(zip_path, 'r') as zip_ref: 44 zip_ref.extractall(extract_folder) 45 except zipfile.BadZipFile as e: 46 print(f"Error extracting {zip_path}: {e}") 47 48 49def verify_file(file_path, ark_verifier_path): 50 verification_command = [ark_verifier_path, "--input_file", file_path] 51 result = subprocess.run(verification_command, capture_output=True, text=True) 52 status = 'pass' if result.returncode == 0 else 'fail' 53 print(f"Verifying: {file_path} {status}") 54 return result.returncode == 0 55 56 57def process_directory(directory, ark_verifier_path): 58 total_count = 0 59 passed_count = 0 60 failed_abc_list = [] 61 62 for root, dirs, files in os.walk(directory): 63 for file in files: 64 if not file.endswith(".abc"): 65 continue 66 abc_path = os.path.join(root, file) 67 if verify_file(abc_path, ark_verifier_path): 68 passed_count += 1 69 else: 70 failed_abc_list.append(os.path.relpath(abc_path, hap_folder)) 71 total_count += 1 72 73 return total_count, passed_count, failed_abc_list 74 75def verify_hap(hap_folder, ark_verifier_path): 76 failed_abc_list = [] 77 passed_count = 0 78 total_count = 0 79 80 for file in os.listdir(hap_folder): 81 if not file.endswith(".zip"): 82 continue 83 84 zip_path = os.path.join(hap_folder, file) 85 extract_folder = os.path.join(hap_folder, file.replace(".zip", "")) 86 extract_zip(zip_path, extract_folder) 87 88 ets_path = os.path.join(extract_folder, "ets") 89 if not os.path.exists(ets_path): 90 continue 91 92 modules_abc_path = os.path.join(ets_path, "modules.abc") 93 if os.path.isfile(modules_abc_path): 94 if verify_file(modules_abc_path, ark_verifier_path): 95 passed_count += 1 96 else: 97 failed_abc_list.append(os.path.relpath(modules_abc_path, hap_folder)) 98 total_count += 1 99 else: 100 total_inc, passed_inc, failed_abc_inc = process_directory(ets_path, ark_verifier_path) 101 total_count += total_inc 102 passed_count += passed_inc 103 failed_abc_list.extend(failed_abc_inc) 104 105 return total_count, passed_count, len(failed_abc_list), failed_abc_list 106 107 108def main(): 109 args = parse_args() 110 111 hap_folder_path = os.path.abspath(args.hap_dir) 112 ark_verifier_path = os.path.abspath(os.path.join(args.verifier_dir, "ark_verifier")) 113 114 out_folder = os.path.join(os.path.dirname(__file__), "out") 115 os.makedirs(out_folder, exist_ok=True) 116 117 copy_and_rename_hap_files(hap_folder_path, out_folder) 118 119 total_count, passed_count, failed_count, failed_abc_list = verify_hap(out_folder, ark_verifier_path) 120 121 print("Summary(abc verification):") 122 print(f"Total: {total_count}") 123 print(f"Passed: {passed_count}") 124 print(f"Failed: {failed_count}") 125 126 if failed_count > 0: 127 print("\nFailed abc files:") 128 for failed_abc in failed_abc_list: 129 print(f" - {failed_abc}") 130 131 shutil.rmtree(out_folder) 132 133 134if __name__ == "__main__": 135 main() 136