1ca0551cfSopenharmony_ci#!/usr/bin/env python3
2ca0551cfSopenharmony_ci# coding: utf-8
3ca0551cfSopenharmony_ci#
4ca0551cfSopenharmony_ci# Copyright (c) 2023-2024 Huawei Device Co., Ltd.
5ca0551cfSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6ca0551cfSopenharmony_ci# you may not use this file except in compliance with the License.
7ca0551cfSopenharmony_ci# You may obtain a copy of the License at
8ca0551cfSopenharmony_ci#
9ca0551cfSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10ca0551cfSopenharmony_ci#
11ca0551cfSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12ca0551cfSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
13ca0551cfSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ca0551cfSopenharmony_ci# See the License for the specific language governing permissions and
15ca0551cfSopenharmony_ci# limitations under the License.
16ca0551cfSopenharmony_ci
17ca0551cfSopenharmony_ciimport os
18ca0551cfSopenharmony_ciimport subprocess
19ca0551cfSopenharmony_ciimport argparse
20ca0551cfSopenharmony_ci
21ca0551cfSopenharmony_ci
22ca0551cfSopenharmony_cidef parse_args():
23ca0551cfSopenharmony_ci    parser = argparse.ArgumentParser()
24ca0551cfSopenharmony_ci    parser.add_argument('--src-idl', help='idl source file')
25ca0551cfSopenharmony_ci    parser.add_argument('--dst-path', help='the converted target path')
26ca0551cfSopenharmony_ci    parser.add_argument('--dst-file', help='the converted target file')
27ca0551cfSopenharmony_ci    parser.add_argument('--idl-tool-path', help='path of the idl conversion tool')
28ca0551cfSopenharmony_ci    parser.add_argument('--log-domainid', help='hilog domain id')
29ca0551cfSopenharmony_ci    parser.add_argument('--log-tag', help='hilog tag')
30ca0551cfSopenharmony_ci    parser.add_argument('--hitrace', help='hitrace switch, default off')
31ca0551cfSopenharmony_ci    parser.add_argument('--language', help='language switch, default cpp')
32ca0551cfSopenharmony_ci    arguments = parser.parse_args()
33ca0551cfSopenharmony_ci    return arguments
34ca0551cfSopenharmony_ci
35ca0551cfSopenharmony_ci
36ca0551cfSopenharmony_cidef run_command(cmd, execution_path, input_arguments):
37ca0551cfSopenharmony_ci    print(" ".join(cmd) + " | execution_path: " + execution_path)
38ca0551cfSopenharmony_ci    proc = subprocess.Popen(cmd, cwd=execution_path, stdout=subprocess.PIPE)
39ca0551cfSopenharmony_ci    proc.wait()
40ca0551cfSopenharmony_ci
41ca0551cfSopenharmony_ci
42ca0551cfSopenharmony_cidef idl_gen_interface(input_arguments):
43ca0551cfSopenharmony_ci    (path, name) = os.path.split(input_arguments.idl_tool_path)
44ca0551cfSopenharmony_ci    is_exists = os.path.exists(input_arguments.dst_path)
45ca0551cfSopenharmony_ci    if not is_exists:
46ca0551cfSopenharmony_ci        try:
47ca0551cfSopenharmony_ci            os.makedirs(input_arguments.dst_path, 0o750, exist_ok=True)
48ca0551cfSopenharmony_ci        except (OSError, TypeError) as excep:
49ca0551cfSopenharmony_ci            raise excep
50ca0551cfSopenharmony_ci        finally:
51ca0551cfSopenharmony_ci            pass
52ca0551cfSopenharmony_ci
53ca0551cfSopenharmony_ci    print("idl_gen_interface run os.remove start")
54ca0551cfSopenharmony_ci    dst_file_list = input_arguments.dst_file.split(',')
55ca0551cfSopenharmony_ci    for dst_file in dst_file_list:
56ca0551cfSopenharmony_ci        i_dst_file = 'i{0}'.format(dst_file)
57ca0551cfSopenharmony_ci        for file_name in os.listdir(input_arguments.dst_path):
58ca0551cfSopenharmony_ci            if ((file_name.startswith(dst_file) or file_name.startswith(i_dst_file)) and
59ca0551cfSopenharmony_ci                (file_name.endswith('.cpp') or file_name.endswith('.h'))):
60ca0551cfSopenharmony_ci                file_path = os.path.join(input_arguments.dst_path, file_name)
61ca0551cfSopenharmony_ci                os.remove(file_path)
62ca0551cfSopenharmony_ci                print("idl_gen_interface run os.remove", i_dst_file)
63ca0551cfSopenharmony_ci
64ca0551cfSopenharmony_ci    gen_language = "-gen-cpp"
65ca0551cfSopenharmony_ci    if input_arguments.language == "rust":
66ca0551cfSopenharmony_ci        gen_language = "-gen-rust"
67ca0551cfSopenharmony_ci    elif input_arguments.language == "ts":
68ca0551cfSopenharmony_ci        gen_language = "-gen-ts"
69ca0551cfSopenharmony_ci
70ca0551cfSopenharmony_ci    src_idls = input_arguments.src_idl.split(",")
71ca0551cfSopenharmony_ci    for src_idl in src_idls:
72ca0551cfSopenharmony_ci        cmd = [os.path.join("./", name, "idl"),
73ca0551cfSopenharmony_ci            gen_language, "-d", input_arguments.dst_path, "-c", src_idl]
74ca0551cfSopenharmony_ci        if input_arguments.log_domainid:
75ca0551cfSopenharmony_ci            cmd += ['-log-domainid', input_arguments.log_domainid]
76ca0551cfSopenharmony_ci        if input_arguments.log_tag:
77ca0551cfSopenharmony_ci            cmd += ['-log-tag', input_arguments.log_tag]
78ca0551cfSopenharmony_ci        if input_arguments.hitrace:
79ca0551cfSopenharmony_ci            cmd += ['-t', input_arguments.hitrace]
80ca0551cfSopenharmony_ci        run_command(cmd, path, input_arguments)
81ca0551cfSopenharmony_ci
82ca0551cfSopenharmony_ciif __name__ == '__main__':
83ca0551cfSopenharmony_ci    idl_gen_interface(parse_args())