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