• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 shutil
19import optparse
20import json
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        file_list = []
33        if options.name in remove_dict:
34            rm_name = remove_dict[options.name]
35            if 'base' in rm_name:
36                for i, base_path in enumerate(rm_name['base']):
37                    base_full_path = os.path.join(
38                        options.project_dir, base_path)
39                    format_src = format_path(base_full_path, options.base_dir)
40                    file_list.append(format_src)
41            for file in os.listdir(options.input):
42                src = os.path.join(options.input, file)
43                if os.path.isfile(src) and (
44                    'global_remove' not in rm_name or (
45                        'global_remove' in rm_name and (
46                            file not in rm_name['global_remove']))):
47                    # 当前文件不在全局删除属性中
48                    format_src = format_path(src, options.base_dir)
49                    if options.ispublic == 'true':
50                        # publicSDK需要删除sdk_build_public_remove中的文件
51                        if 'sdk_build_public_remove' not in rm_name:
52                            file_list.append(format_src)
53                        else:
54                            if file not in rm_name['sdk_build_public_remove']:
55                                file_list.append(format_src)
56                    else:
57                        file_list.append(format_src)
58        else:
59            for file in os.listdir(options.input):
60                src = os.path.join(options.input, file)
61                if os.path.isfile(src):
62                    format_src = format_path(src, options.base_dir)
63                    file_list.append(format_src)
64        return file_list
65
66
67def format_path(filepath, base_dir):
68    return os.path.abspath(filepath)
69
70
71def parse_args(args):
72    '''解析命令行参数'''
73    parser = optparse.OptionParser()
74    parser.add_option('--input', help='d.ts document input path')
75    parser.add_option('--remove', help='d.ts to be remove path')
76    parser.add_option('--base-dir', help='d.ts document base dir path')
77    parser.add_option('--project-dir', help='current project dir path')
78    parser.add_option('--ispublic', help='whether or not sdk build public')
79    parser.add_option('--name', help='module label name')
80    parser.add_option('--output', help='output path')
81    options, _ = parser.parse_args(args)
82    options.input = os.path.realpath(options.input)
83    return options
84
85
86def main(argv):
87    '''main函数入口'''
88    options = parse_args(argv)
89    if not os.path.exists(options.output):
90        os.makedirs(options.output)
91    result = copy_files(options)
92    for file_path in result:
93        shutil.copy(file_path, options.output)
94    return 0
95
96
97if __name__ == "__main__":
98    sys.exit(main(sys.argv))
99