1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2022-2025 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import sys 21import argparse 22import logging 23import numpy as np 24 25 26class ModulesDumpTest: 27 _file_name: str 28 _binary_dir: str 29 _checksum_name_map: dict 30 31 def __init__(self, args) -> None: 32 self._file_name = args.file 33 self._binary_dir = args.bindir 34 self._checksum_name_map = dict() 35 36 @staticmethod 37 def parse_args(): 38 parser = argparse.ArgumentParser(description="Sampler module file check test") 39 required = parser.add_argument_group("required arguments") 40 required.add_argument("--file", type=str, required=True) 41 required.add_argument("--bindir", type=str, required=True) 42 43 args = parser.parse_args() 44 45 return args 46 47 @staticmethod 48 def get_checksum_from_abc_file(module_path) -> np.uint32: 49 checksum_offset = 8 # bytes 50 51 with open(module_path, mode="r+") as file: 52 checksum = np.fromfile(file, dtype=np.uint32, count=1, sep='', offset=checksum_offset) 53 return checksum[0] 54 55 def fill_map(self, modules_list: list): 56 for module in modules_list: 57 checksum = self.get_checksum_from_abc_file(module) 58 self._checksum_name_map[module] = checksum 59 60 def module_file_check(self) -> bool: 61 etsstdlib_abc_path = os.path.join(self._binary_dir, "plugins", "ets", "etsstdlib.abc") 62 sampler_test_abc_path = os.path.join(self._binary_dir, 63 "plugins", 64 "ets", 65 "tests", 66 "runtime", 67 "tooling", 68 "sampler", 69 "managed", 70 "SamplerTest.abc") 71 72 etsstdlib_abc_path = os.path.realpath(etsstdlib_abc_path) 73 sampler_test_abc_path = os.path.realpath(sampler_test_abc_path) 74 75 modules_list = [etsstdlib_abc_path, sampler_test_abc_path] 76 77 self.fill_map(modules_list) 78 79 modules_from_file = [] 80 81 with open(self._file_name, "r") as my_file: 82 while line := my_file.readline(): 83 line = line.rstrip("\n") 84 line_content = line.split(" ") 85 86 pathname: str = os.path.realpath(line_content[1]) 87 modules_from_file.append(pathname) 88 89 checksum: np.uint32 = np.uint32(line_content[0]) 90 expected_checksum: np.uint32 | None = self._checksum_name_map.get(pathname, None) 91 92 if (expected_checksum is None): 93 logging.error("sampler_module_file_check: can not find expected checksum for ", pathname) 94 return False 95 96 if (checksum != expected_checksum): 97 logging.error("sampler_module_file_check: for file", 98 pathname, 99 "checksum is not equal", 100 expected_checksum, 101 "vs", 102 checksum) 103 return False 104 105 if (len(modules_from_file) != len(modules_list)): 106 error_message = ( 107 "sampler_module_file_check: wrong number of modules in module file: " 108 f"expected {len(modules_list)}, in file {len(modules_from_file)}" 109 ) 110 logging.error(error_message) 111 return False 112 113 return True 114 115 116def main(): 117 args = ModulesDumpTest.parse_args() 118 test = ModulesDumpTest(args) 119 120 if not test.module_file_check(): 121 logging.error("sampler_module_file_check: test failed") 122 return 1 123 124 return 0 125 126 127if __name__ == "__main__": 128 sys.exit(main()) 129