• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 argparse
18import os
19import shutil
20import json
21import glob
22sys.path.append(
23    os.path.dirname(os.path.dirname(os.path.dirname(
24        os.path.abspath(__file__)))))
25from scripts.util import build_utils  # noqa: E402
26
27
28def collect_notice_files(options, dest_dir: str, depfiles: str):
29    subsystem_info_files = []
30    with open(options.install_info_file, 'r') as file:
31        install_info = json.load(file)
32        for item in install_info:
33            subsystem_info_files.append(item['part_info_file'])
34    depfiles.extend(subsystem_info_files)
35
36    module_info_files = []
37    for subsystem_info in subsystem_info_files:
38        with open(subsystem_info, 'r') as file:
39            subsystem_info = json.load(file)
40            for item in subsystem_info:
41                module_info_files.append(item['module_info_file'])
42    depfiles.extend(module_info_files)
43
44    for module_info in module_info_files:
45        with open(module_info, 'r') as file:
46            module_info = json.load(file)
47            if 'notice' in module_info and module_info[
48                'type'] != "java_library":
49                notice_file = module_info['notice']
50                if os.path.exists(notice_file) is False or os.stat(
51                        notice_file).st_size == 0:
52                    continue
53                dest = os.path.join(dest_dir,
54                                    "{}.txt".format(module_info['dest'][0]))
55                os.makedirs(os.path.dirname(dest), exist_ok=True)
56                shutil.copyfile(module_info['notice'], dest)
57                depfiles.append(module_info['notice'])
58                if os.path.isfile("{}.json".format(module_info['notice'])):
59                    os.makedirs(os.path.dirname("{}.json".format(dest)), exist_ok=True)
60                    shutil.copyfile("{}.json".format(module_info['notice']), "{}.json".format(dest))
61
62    notice_files = build_utils.get_all_files(options.notice_root_dir)
63    depfiles.extend(notice_files)
64    for file in notice_files:
65        dest = os.path.join(dest_dir,
66                            os.path.relpath(file, options.notice_root_dir))
67        os.makedirs(os.path.dirname(dest), exist_ok=True)
68        shutil.copyfile(file, dest)
69        if os.path.isfile("{}.json".format(file)):
70            os.makedirs(os.path.dirname("{}.json".format(dest)), exist_ok=True)
71            shutil.copyfile("{}.json".format(file), "{}.json".format(dest))
72
73
74def main():
75    parser = argparse.ArgumentParser()
76    parser.add_argument('--install-info-file', required=True)
77    parser.add_argument('--notice-root-dir', required=True)
78    parser.add_argument('--output-file', required=True)
79    parser.add_argument('--depfile', required=False)
80    args = parser.parse_args()
81
82    depfiles = [args.install_info_file]
83    with build_utils.temp_dir() as tmp:
84        collect_notice_files(args, tmp, depfiles)
85        build_utils.zip_dir(args.output_file, tmp)
86
87    if args.depfile:
88        build_utils.write_depfile(args.depfile,
89                                  args.output_file,
90                                  sorted(depfiles),
91                                  add_pydeps=False)
92    return 0
93
94
95if __name__ == '__main__':
96    sys.exit(main())
97