• 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")
33DEL_TARGET = ["//interface/sdk-js:bundle_api"]
34
35
36def copy_sdk_interface(source_root):
37    source = os.path.join(source_root, INTERFACE_PATH)
38    dest = os.path.join(source_root, OUTPATH)
39    if os.path.exists(dest):
40        shutil.rmtree(dest)
41    shutil.copytree(source, dest)
42
43
44def replace_sdk_api_dir(source_root):
45    dest = os.path.join(source_root, API_PATH)
46    if os.path.exists:
47        shutil.rmtree(dest)
48    source = os.path.join(source_root, API_GEN_PATH)
49    shutil.copytree(source, dest)
50
51
52def remove_system_api_method(source_root, nodejs):
53    tool = os.path.join(source_root, API_MODIFY_TOOL)
54    tool = os.path.abspath(tool)
55    nodejs = os.path.abspath(nodejs)
56    cmd = "{} {}".format(nodejs, tool)
57    p = subprocess.Popen(cmd, shell=True,
58                        cwd=os.path.abspath(os.path.join(source_root, API_MODIFY_DIR)),
59                        stdout=subprocess.PIPE)
60    p.wait()
61
62def regenerate_sdk_description_file(source_root, sdk_description_file, output_pub_sdk_desc_file):
63    info_list = read_json_file(sdk_description_file)
64    public_info_list = []
65    for info in info_list:
66        label = str(info.get("module_label"))
67        if label in DEL_TARGET:
68            continue
69        if label.startswith("//{}".format(INTERFACE_PATH)):
70            label = label.replace(INTERFACE_PATH, os.path.join(OUT_ROOT, "public_interface/sdk-js"))
71            info["module_label"] = label
72        public_info_list.append(info)
73    write_json_file(output_pub_sdk_desc_file, public_info_list)
74
75
76def parse_step(sdk_description_file, source_root, nodejs, output_pub_sdk_desc_file):
77    copy_sdk_interface(source_root)
78    remove_system_api_method(source_root, nodejs)
79    replace_sdk_api_dir(source_root)
80    regenerate_sdk_description_file(source_root, sdk_description_file, output_pub_sdk_desc_file)
81
82
83def main():
84    parser = argparse.ArgumentParser()
85    parser.add_argument('--sdk-description-file', required=True)
86    parser.add_argument('--root-build-dir', required=True)
87    parser.add_argument('--node-js', required=True)
88    parser.add_argument('--output-pub-sdk-desc-file', required=True)
89
90    options = parser.parse_args()
91    parse_step(options.sdk_description_file, options.root_build_dir,
92            options.node_js, options.output_pub_sdk_desc_file)
93
94
95if __name__ == '__main__':
96    sys.exit(main())
97