• 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 os
18import shutil
19import tarfile
20sys.path.append(os.path.abspath(os.path.dirname(
21    os.path.abspath(os.path.dirname(__file__)))))
22from scripts.util.file_utils import read_json_file  # noqa: E402
23
24RELEASE_FILENAME = 'README.OpenSource'
25scan_dir_list = ['third_party']
26
27
28def get_source_top_dir():
29    top_dir = os.path.abspath(os.path.dirname(
30        os.path.abspath(os.path.dirname(
31            os.path.abspath(os.path.dirname(__file__))))))
32    return top_dir
33
34
35def get_package_dir():
36    top_dir = get_source_top_dir()
37    package_dir = os.path.join(top_dir, 'out', 'Code_Opensource')
38    return package_dir
39
40
41def copy_opensource_file(opensource_config_file):
42    if not os.path.exists(opensource_config_file):
43        print("Warning, the opensource config file is not exists.")
44        return False
45
46    top_dir = get_source_top_dir()
47    package_dir = get_package_dir()
48    src_dir = os.path.dirname(opensource_config_file)
49    dst_dir = os.path.join(package_dir, os.path.relpath(src_dir, top_dir))
50
51    # copy opensource folder to out dir
52    if os.path.exists(dst_dir):
53        shutil.rmtree(dst_dir)
54    shutil.copytree(src_dir, dst_dir, symlinks=True,
55                    ignore=shutil.ignore_patterns('*.pyc', 'tmp*', '.git*'))
56
57    # delete the README.OpenSource file
58    release_file = os.path.join(dst_dir, RELEASE_FILENAME)
59    os.remove(release_file)
60    return True
61
62
63def parse_opensource_file(opensource_config_file):
64    if not os.path.exists(opensource_config_file):
65        print("Warning, the opensource config file is not exists.")
66        return False
67
68    opensource_config = read_json_file(opensource_config_file)
69    if opensource_config is None:
70        raise Exception("read opensource config file [{}] failed.".format(
71            opensource_config_file))
72
73    result = False
74    for info in opensource_config:
75        license = info.get('License')
76        if license.count('GPL') > 0 or license.count('LGPL') > 0:
77            result = copy_opensource_file(opensource_config_file)
78
79    return result
80
81
82def scan_and_package_code_release(scan_dir):
83    file_dir_names = os.listdir(scan_dir)
84    for file_dir_name in file_dir_names:
85        file_dir_path = os.path.join(scan_dir, file_dir_name)
86        if os.path.isdir(file_dir_path):
87            scan_and_package_code_release(file_dir_path)
88        elif file_dir_path == os.path.join(scan_dir, RELEASE_FILENAME):
89            parse_opensource_file(file_dir_path)
90
91
92def scan_opensource_dir_list(scan_list):
93    for scan_dir in scan_list:
94        scan_and_package_code_release(scan_dir)
95
96
97def tar_opensource_package_file():
98    package_dir = get_package_dir()
99    top_dir = get_source_top_dir()
100    result = -1
101    if os.path.exists(package_dir):
102        package_filename = os.path.join(
103            top_dir, 'out', 'Code_Opensource.tar.gz')
104        try:
105            with tarfile.open(package_filename, "w:gz") as tar:
106                tar.add(package_dir, arcname=os.path.basename(package_dir))
107                result = 0
108        except IOError as err:
109            raise err
110    return result
111
112
113def main():
114    # get the source top directory to be scan
115    top_dir = get_source_top_dir()
116
117    # generate base_dir/out/Code_Opensource dir
118    package_dir = get_package_dir()
119    if os.path.exists(package_dir):
120        shutil.rmtree(package_dir)
121    os.makedirs(package_dir)
122
123    # scan the target dir and copy release code to out/opensource dir
124    dir_list = [os.path.join(top_dir, dir) for dir in scan_dir_list]
125    print(dir_list)
126    scan_opensource_dir_list(dir_list)
127
128    # package the opensource to Code_Opensource.tar.gz
129    if tar_opensource_package_file() == 0:
130        print('Generate the opensource package successfully.')
131    else:
132        print('Generate the opensource package failed.')
133
134    return 0
135
136
137if __name__ == '__main__':
138    sys.exit(main())
139