1#!/usr/bin/env python3 2#coding: utf-8 3""" 4Copyright (c) 2022-2023 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: run script 18 input: resource file 19 output: output file 20""" 21 22import argparse 23import os 24import sys 25 26 27def resource_file_to_cpp(input_dir, input_file, output_path): 28 with open(os.path.join(input_dir, input_file), 'rb')\ 29 as resource_file_object: 30 with open(output_path, 'a') as cpp_file_object: 31 length = 0; 32 all_the_content = resource_file_object.read(); 33 template0 = "#include <cstdint>\n"; 34 template1 = "extern const uint8_t _binary_$1_start[$2] = {$3};\n"; 35 template2 = \ 36 "extern const uint32_t _binary_$1_length = $2;"; 37 38 formats = "," 39 seq = [] 40 for content in all_the_content: 41 seq.append(str(hex(content))) 42 length = length + 1 43 byte_code = formats.join(seq); 44 input_file = input_file.replace(".", "_") 45 template1 = template1.replace("$1", str(input_file)) \ 46 .replace("$2", str(length)) \ 47 .replace("$3", str(byte_code)) 48 template2 = template2.replace("$1", str(input_file)) \ 49 .replace("$2", str(length)) 50 cpp_file_object.seek(0) 51 cpp_file_object.truncate(); 52 cpp_file_object.write(template0 + template1 + template2); 53 54 55def main(): 56 parser = argparse.ArgumentParser() 57 parser.add_argument('--input', type=str, required=True) 58 parser.add_argument('--output', type=str, required=True) 59 60 args = parser.parse_args() 61 62 input_dir, input_file = os.path.split(args.input) 63 output_path = os.path.abspath(args.output) 64 resource_file_to_cpp(input_dir, input_file, output_path) 65 66 67if __name__ == '__main__': 68 sys.exit(main()) 69