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