14514f5e3Sopenharmony_ci#!/usr/bin/env python3
24514f5e3Sopenharmony_ci#coding: utf-8
34514f5e3Sopenharmony_ci"""
44514f5e3Sopenharmony_ciCopyright (c) 2022-2023 Huawei Device Co., Ltd.
54514f5e3Sopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
64514f5e3Sopenharmony_ciyou may not use this file except in compliance with the License.
74514f5e3Sopenharmony_ciYou may obtain a copy of the License at
84514f5e3Sopenharmony_ci
94514f5e3Sopenharmony_ci    http://www.apache.org/licenses/LICENSE-2.0
104514f5e3Sopenharmony_ci
114514f5e3Sopenharmony_ciUnless required by applicable law or agreed to in writing, software
124514f5e3Sopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
134514f5e3Sopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144514f5e3Sopenharmony_ciSee the License for the specific language governing permissions and
154514f5e3Sopenharmony_cilimitations under the License.
164514f5e3Sopenharmony_ci
174514f5e3Sopenharmony_ciDescription: run script
184514f5e3Sopenharmony_ci    input: resource file
194514f5e3Sopenharmony_ci    output: output file
204514f5e3Sopenharmony_ci"""
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ciimport argparse
234514f5e3Sopenharmony_ciimport os
244514f5e3Sopenharmony_ciimport sys
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cidef resource_file_to_cpp(input_dir, input_file, output_path):
284514f5e3Sopenharmony_ci    with open(os.path.join(input_dir, input_file), 'rb')\
294514f5e3Sopenharmony_ci            as resource_file_object:
304514f5e3Sopenharmony_ci        with open(output_path, 'a') as cpp_file_object:
314514f5e3Sopenharmony_ci            length = 0;
324514f5e3Sopenharmony_ci            all_the_content = resource_file_object.read();
334514f5e3Sopenharmony_ci            template0 = "#include <cstdint>\n";
344514f5e3Sopenharmony_ci            template1 = "extern const uint8_t  _binary_$1_start[$2] = {$3};\n";
354514f5e3Sopenharmony_ci            template2 = \
364514f5e3Sopenharmony_ci                "extern const uint32_t _binary_$1_length = $2;";
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci            formats = ","
394514f5e3Sopenharmony_ci            seq = []
404514f5e3Sopenharmony_ci            for content in all_the_content:
414514f5e3Sopenharmony_ci                seq.append(str(hex(content)))
424514f5e3Sopenharmony_ci                length = length + 1
434514f5e3Sopenharmony_ci            byte_code = formats.join(seq);
444514f5e3Sopenharmony_ci            input_file = input_file.replace(".", "_")
454514f5e3Sopenharmony_ci            template1 = template1.replace("$1", str(input_file)) \
464514f5e3Sopenharmony_ci                .replace("$2", str(length)) \
474514f5e3Sopenharmony_ci                .replace("$3", str(byte_code))
484514f5e3Sopenharmony_ci            template2 = template2.replace("$1", str(input_file)) \
494514f5e3Sopenharmony_ci                .replace("$2", str(length))
504514f5e3Sopenharmony_ci            cpp_file_object.seek(0)
514514f5e3Sopenharmony_ci            cpp_file_object.truncate();
524514f5e3Sopenharmony_ci            cpp_file_object.write(template0 + template1 + template2);
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_cidef main():
564514f5e3Sopenharmony_ci    parser = argparse.ArgumentParser()
574514f5e3Sopenharmony_ci    parser.add_argument('--input', type=str, required=True)
584514f5e3Sopenharmony_ci    parser.add_argument('--output', type=str, required=True)
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    args = parser.parse_args()
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    input_dir, input_file = os.path.split(args.input)
634514f5e3Sopenharmony_ci    output_path = os.path.abspath(args.output)
644514f5e3Sopenharmony_ci    resource_file_to_cpp(input_dir, input_file, output_path)
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ciif __name__ == '__main__':
684514f5e3Sopenharmony_ci    sys.exit(main())
69