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 os 17import shutil 18import subprocess 19 20INTERFACE_PATH = "interface/sdk-js" 21API_PATH = "api" 22API_GEN_PATH = "build-tools/api" 23CONVERTER_DIR = "build-tools/permissions_converter" 24CONVERTER_TOOL = "build-tools/permissions_converter/convert.js" 25 26 27def copy_sdk_interface(source_root: str, project_path: str): 28 source = os.path.join(source_root, INTERFACE_PATH) 29 dest = os.path.join(source_root, project_path) 30 if os.path.exists(dest) is False: 31 shutil.copytree(source, dest) 32 33 34def copy_api(source_root: str, project_path: str): 35 source = os.path.join(source_root, project_path, API_PATH) 36 dest = os.path.join(source_root, project_path, API_GEN_PATH) 37 if os.path.exists(dest) is False: 38 shutil.copytree(source, dest) 39 40 41def convert_permission_method(source_root: str, project_path: str, file_name: str, nodejs: str): 42 permission_convert_dir = os.path.join(INTERFACE_PATH, CONVERTER_DIR) 43 permission_convert_tool = os.path.join(INTERFACE_PATH, CONVERTER_TOOL) 44 config_file = os.path.join("base/security/access_token/services/accesstokenmanager", "permission_definitions.json") 45 permission_gen_path = os.path.join(project_path, API_GEN_PATH, file_name) 46 47 tool = os.path.abspath(os.path.join(source_root, permission_convert_tool)) 48 nodejs = os.path.abspath(nodejs) 49 config = os.path.abspath(os.path.join(source_root, config_file)) 50 output_path = os.path.abspath(os.path.join(source_root, permission_gen_path)) 51 process = subprocess.Popen([nodejs, tool, config, output_path], shell=False, 52 cwd=os.path.abspath(os.path.join(source_root, permission_convert_dir)), 53 stdout=subprocess.PIPE) 54 process.wait() 55 56 57def replace_sdk_api_dir(source_root: str, project_path: str, file_name: str): 58 source = os.path.join(source_root, project_path, API_GEN_PATH, file_name) 59 dest = os.path.join(source_root, project_path, API_PATH, file_name) 60 if os.path.exists(dest): 61 os.remove(dest) 62 shutil.copyfile(source, dest) 63 64 65def parse_step(source_root: str, project_path: str, file_name: str, nodejs: str): 66 copy_sdk_interface(source_root, project_path) 67 copy_api(source_root, project_path) 68 convert_permission_method(source_root, project_path, file_name, nodejs) 69 replace_sdk_api_dir(source_root, project_path, file_name) 70 71 72def convert_permissions(root_build_dir: str, project_path: str, file_name: str, node_js: str): 73 parse_step(root_build_dir, project_path, file_name, node_js) 74 75