1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2022-2024 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 "SamplerTest.abc") 70 71 etsstdlib_abc_path = os.path.realpath(etsstdlib_abc_path) 72 sampler_test_abc_path = os.path.realpath(sampler_test_abc_path) 73 74 modules_list = [etsstdlib_abc_path, sampler_test_abc_path] 75 76 self.fill_map(modules_list) 77 78 modules_from_file = [] 79 80 with open(self._file_name, "r") as my_file: 81 while line := my_file.readline(): 82 line = line.rstrip("\n") 83 line_content = line.split(" ") 84 85 pathname: str = os.path.realpath(line_content[1]) 86 modules_from_file.append(pathname) 87 88 checksum: np.uint32 = np.uint32(line_content[0]) 89 expected_checksum: np.uint32 | None = self._checksum_name_map.get(pathname, None) 90 91 if (expected_checksum is None): 92 logging.error("sampler_module_file_check: can not find expected checksum for ", pathname) 93 return False 94 95 if (checksum != expected_checksum): 96 logging.error("sampler_module_file_check: for file", 97 pathname, 98 "checksum is not equal", 99 expected_checksum, 100 "vs", 101 checksum) 102 return False 103 104 if (len(modules_from_file) != len(modules_list)): 105 error_message = ( 106 "sampler_module_file_check: wrong number of modules in module file: " 107 f"expected {len(modules_list)}, in file {len(modules_from_file)}" 108 ) 109 logging.error(error_message) 110 return False 111 112 return True 113 114 115def main(): 116 args = ModulesDumpTest.parse_args() 117 test = ModulesDumpTest(args) 118 119 if not test.module_file_check(): 120 logging.error("sampler_module_file_check: test failed") 121 return 1 122 123 return 0 124 125 126if __name__ == "__main__": 127 sys.exit(main()) 128