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