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 sys
17import os
18import argparse
19import shutil
20
21WORK_SPACE = os.path.dirname(os.path.abspath(__file__))
22PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(WORK_SPACE)))
23
24sys.path.append(os.path.join(PROJECT_DIR, 'build'))
25sys.path.append(os.path.join(PROJECT_DIR, 'build/hb'))
26from hb.util import log_util  # noqa: E402
27from scripts.util import file_utils  # noqa: E402
28from scripts.util import build_utils  # noqa: E402
29
30INTERFACE_DIR = os.path.join(WORK_SPACE, 'ohos_interface')
31INTERFACE_INCLUDE_DIR = os.path.join(INTERFACE_DIR, 'include')
32INTERFACE_OHOS_GLUE_DIR = os.path.join(INTERFACE_DIR, 'ohos_glue')
33
34
35def copy_dir(src_dir: str, dst_dir: str):
36    log_util.LogUtil.hb_info("begin to copy dir from '{}' to '{}'".format(src_dir, dst_dir))
37    if os.path.isdir(dst_dir):
38        shutil.rmtree(dst_dir)
39
40    if os.path.isdir(src_dir) and os.listdir(src_dir):
41        shutil.copytree(src_dir, dst_dir)
42
43    source_files = []
44    for root, dirs, files in os.walk(src_dir):
45        for name in files:
46            source_files.append(os.path.join(root, name))
47    return source_files
48
49
50def copy_files(src_dir: str, dst_dir: str):
51    log_util.LogUtil.hb_info("begin to copy files from '{}' to '{}'".format(src_dir, dst_dir))
52    source_files = []
53    for item in os.listdir(src_dir):
54        src_file = os.path.join(src_dir, item)
55        dst_file = os.path.join(dst_dir, item)
56        if os.path.isfile(src_file):
57            source_files.append(src_file)
58            shutil.copy2(src_file, dst_file)
59    return source_files
60
61
62def copy_include():
63    log_util.LogUtil.hb_info("begin to copy include dir")
64    nweb_include = os.path.join('ohos_nweb', 'include')
65    include_source_files = copy_files(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_nweb'),
66            os.path.join(WORK_SPACE, nweb_include))
67
68    adapter_include = os.path.join('ohos_adapter', 'interfaces')
69    include_source_files += copy_dir(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_adapter'),
70            os.path.join(WORK_SPACE, adapter_include))
71    return include_source_files
72
73
74def copy_glue_base(glue_dir: str):
75    log_util.LogUtil.hb_info("begin to copy glue base dir")
76    base_dir = os.path.join(glue_dir, 'base')
77    base_source_files = copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'base'), base_dir)
78
79    script_dir = os.path.join(glue_dir, 'scripts')
80    base_source_files += copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'scripts'), script_dir)
81    return base_source_files
82
83
84def copy_glue_module(glue_dir: str, module_name: str):
85    dir_name = 'ohos_' + module_name;
86    log_util.LogUtil.hb_info("begin to copy glue '{}' dir".format(dir_name))
87    dst_dir = os.path.join(glue_dir, dir_name)
88    if os.path.isdir(dst_dir):
89        shutil.rmtree(dst_dir)
90
91    src_dir = os.path.join(INTERFACE_OHOS_GLUE_DIR, dir_name)
92    module_source_files = copy_dir(os.path.join(src_dir, 'include'), os.path.join(dst_dir, 'include'))
93    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'bridge'), 'webview'),
94            os.path.join(dst_dir, 'bridge'))
95    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'cpptoc'), 'webview'),
96            os.path.join(dst_dir, 'cpptoc'))
97    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'ctocpp'), 'webview'),
98            os.path.join(dst_dir, 'ctocpp'))
99    return module_source_files
100
101
102def main():
103    parser = argparse.ArgumentParser()
104    parser.add_argument('--command-type', required=True)
105    parser.add_argument('--ohos-glue-dir', required=True)
106    parser.add_argument('--outfile', required=True)
107    parser.add_argument('--depfile', required=False)
108    args = parser.parse_args()
109
110    if args.command_type == "include":
111        source_file_list = copy_include()
112    elif args.command_type == "base":
113        source_file_list = copy_glue_base(args.ohos_glue_dir)
114    else:
115        source_file_list = copy_glue_module(args.ohos_glue_dir, args.command_type)
116
117    file_utils.write_file(args.outfile, '\n'.join(source_file_list))
118
119    if args.depfile:
120        _dep_files = []
121        _dep_files.extend(source_file_list)
122        _dep_files.sort()
123        build_utils.write_depfile(args.depfile,
124                                  args.outfile,
125                                  _dep_files,
126                                  add_pydeps=False)
127
128
129if __name__ == '__main__':
130    sys.exit(main())
131
132