• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import sys
19import shutil
20import subprocess
21sys.path.append(
22    os.path.dirname(
23        os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
24from scripts.util.file_utils import read_json_file, write_json_file
25
26INTERFACE_PATH = "interface/sdk-js"
27OUT_ROOT = "out/sdk-public"
28OUTPATH = os.path.join(OUT_ROOT, "public_interface/sdk-js")
29API_MODIFY_DIR = os.path.join(OUTPATH, "build-tools")
30API_MODIFY_TOOL = os.path.join(API_MODIFY_DIR, "delete_systemapi_plugin.js")
31API_PATH = os.path.join(OUTPATH, "api")
32API_GEN_PATH = os.path.join(OUTPATH, "build-tools/api")
33KITS_PATH = os.path.join(OUTPATH, "kits")
34KITS_GEN_PATH = os.path.join(OUTPATH, "build-tools/kits")
35DEL_TARGET = ["//interface/sdk-js:bundle_api"]
36
37
38def copy_sdk_interface(source_root: str):
39    source = os.path.join(source_root, INTERFACE_PATH)
40    dest = os.path.join(source_root, OUTPATH)
41    if os.path.exists(dest):
42        shutil.rmtree(dest)
43    shutil.copytree(source, dest)
44
45
46def replace_sdk_api_dir(source_root: str):
47    dest = os.path.join(source_root, API_PATH)
48    if os.path.exists:
49        shutil.rmtree(dest)
50    source = os.path.join(source_root, API_GEN_PATH)
51    shutil.copytree(source, dest)
52
53
54def replace_sdk_kits_dir(source_root: str):
55    dest = os.path.join(source_root, KITS_PATH)
56    if os.path.exists:
57        shutil.rmtree(dest)
58    source = os.path.join(source_root, KITS_GEN_PATH)
59    shutil.copytree(source, dest)
60
61
62def remove_system_api_method(source_root: str, nodejs: str):
63    tool = os.path.join(source_root, API_MODIFY_TOOL)
64    tool = os.path.abspath(tool)
65    nodejs = os.path.abspath(nodejs)
66    cmd = "{} {}".format(nodejs, tool)
67    p = subprocess.Popen(cmd, shell=True,
68                        cwd=os.path.abspath(os.path.join(source_root, API_MODIFY_DIR)),
69                        stdout=subprocess.PIPE)
70    p.wait()
71
72def regenerate_sdk_description_file(source_root: str, sdk_description_file: str, output_pub_sdk_desc_file: str):
73    info_list = read_json_file(sdk_description_file)
74    public_info_list = []
75    for info in info_list:
76        label = str(info.get("module_label"))
77        if label in DEL_TARGET:
78            continue
79        if label.startswith("//{}".format(INTERFACE_PATH)):
80            label = label.replace(INTERFACE_PATH, os.path.join(OUT_ROOT, "public_interface/sdk-js"))
81            info["module_label"] = label
82        public_info_list.append(info)
83    write_json_file(output_pub_sdk_desc_file, public_info_list)
84
85
86def parse_step(sdk_description_file: str, source_root: str, nodejs: str, output_pub_sdk_desc_file: str):
87    copy_sdk_interface(source_root)
88    remove_system_api_method(source_root, nodejs)
89    replace_sdk_api_dir(source_root)
90    replace_sdk_kits_dir(source_root)
91    regenerate_sdk_description_file(source_root, sdk_description_file, output_pub_sdk_desc_file)
92
93
94def main():
95    parser = argparse.ArgumentParser()
96    parser.add_argument('--sdk-description-file', required=True)
97    parser.add_argument('--root-build-dir', required=True)
98    parser.add_argument('--node-js', required=True)
99    parser.add_argument('--output-pub-sdk-desc-file', required=True)
100
101    options = parser.parse_args()
102    parse_step(options.sdk_description_file, options.root_build_dir,
103            options.node_js, options.output_pub_sdk_desc_file)
104
105
106if __name__ == '__main__':
107    sys.exit(main())
108