1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021-2022 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 sys 18import optparse 19import json 20import re 21 22 23def copy_files(options): 24 ''' 25 根据remove_list.json文件判断 保留base中的文件\n 26 保留base\n 27 删除global_remove\n 28 编publicSDK时删除sdk_build_public_remove\n 29 ''' 30 with open(options.remove) as f: 31 remove_dict = json.load(f) 32 if options.name in remove_dict: 33 rm_name = remove_dict[options.name] 34 if 'base' in rm_name: 35 file_list = rm_name['base'] 36 else: 37 file_list = [] 38 for file in os.listdir(options.input): 39 src = os.path.join(options.input, file) 40 if os.path.isfile(src) and ( 41 'global_remove' not in rm_name or ( 42 'global_remove' in rm_name and ( 43 file not in rm_name['global_remove']))): 44 # 当前文件不在全局删除属性中 45 format_src = format_path(src) 46 if options.ispublic == 'true': 47 # publicSDK需要删除sdk_build_public_remove中的文件 48 if 'sdk_build_public_remove' not in rm_name: 49 file_list.append(format_src) 50 else: 51 if file not in rm_name['sdk_build_public_remove']: 52 file_list.append(format_src) 53 else: 54 file_list.append(format_src) 55 else: 56 file_list = [] 57 for file in os.listdir(options.input): 58 src = os.path.join(options.input, file) 59 if os.path.isfile(src): 60 format_src = format_path(src) 61 file_list.append(format_src) 62 return file_list 63 64 65def format_path(filepath): 66 '''删除api/前面所有内容,保留api/''' 67 return re.sub(r'.*(?=api/)', '', filepath) 68 69 70def parse_args(args): 71 '''解析命令行参数''' 72 parser = optparse.OptionParser() 73 parser.add_option('--input', help='d.ts document input path') 74 parser.add_option('--remove', help='d.ts to be remove path') 75 parser.add_option('--ispublic', help='whether or not sdk build public') 76 parser.add_option('--name', help='module label name') 77 options, _ = parser.parse_args(args) 78 return options 79 80 81def main(argv): 82 '''main函数入口''' 83 options = parse_args(argv) 84 result = copy_files(options) 85 print(json.dumps(result)) 86 return 0 87 88 89if __name__ == "__main__": 90 sys.exit(main(sys.argv)) 91