• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20import sys
21
22
23def parse_args():
24    parser = argparse.ArgumentParser()
25    parser.add_argument('--src-idl', required=True, help='idl source file')
26    parser.add_argument('--dst-path', required=True, help='the converted target path')
27    parser.add_argument('--dst-file', required=True, help='the converted target file')
28    parser.add_argument('--idl-tool-path', required=True, help='path of the idl conversion tool')
29    parser.add_argument('--log-domainid', help='hilog domain id')
30    parser.add_argument('--log-tag', help='hilog tag')
31    parser.add_argument('--hitrace', help='hitrace switch, default off')
32    parser.add_argument('--language', help='language switch, default cpp')
33    parser.add_argument('--client-enable', help='gen client code')
34    arguments = parser.parse_args()
35    return arguments
36
37
38def run_command(cmd, execution_path):
39    print("cmd:", " ".join(cmd))
40    print("execution_path:", execution_path)
41    proc = subprocess.Popen(cmd, cwd=execution_path, stdout=subprocess.PIPE)
42    proc.wait()
43    return proc.returncode
44
45
46def idl_gen_interface(input_arguments):
47    if not os.path.exists(input_arguments.dst_path):
48        try:
49            os.makedirs(input_arguments.dst_path, 0o750, exist_ok=True)
50        except (OSError, TypeError) as excep:
51            raise excep
52        finally:
53            pass
54
55    print("idl_gen_interface run os.remove start")
56    dst_file_list = input_arguments.dst_file.split(',')
57    for dst_file in dst_file_list:
58        if os.path.exists(dst_file):
59            os.remove(dst_file)
60            print("idl_gen_interface run os.remove", dst_file)
61
62    gen_language = "-gen-cpp"
63    if input_arguments.language == "rust":
64        gen_language = "-gen-rust"
65    elif input_arguments.language == "ts":
66        gen_language = "-gen-ts"
67
68    src_idl_list = input_arguments.src_idl.split(",")
69    for src_idl in src_idl_list:
70        cmd = [os.path.join(".", "idl"), gen_language, "-d", input_arguments.dst_path, "-c", src_idl]
71        if input_arguments.log_domainid:
72            cmd += ['-log-domainid', input_arguments.log_domainid]
73        if input_arguments.log_tag:
74            cmd += ['-log-tag', input_arguments.log_tag]
75        if input_arguments.hitrace:
76            cmd += ['-t', input_arguments.hitrace]
77        if input_arguments.client_enable == 'true':
78            cmd += ['-client-enable']
79        ret = run_command(cmd, input_arguments.idl_tool_path)
80        if ret != 0:
81            return ret
82    return 0
83
84
85if __name__ == '__main__':
86    sys.exit(idl_gen_interface(parse_args()))
87