|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2024 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import os |
| 7 | +import logging |
| 8 | +import re |
| 9 | +import yaml |
| 10 | +import fosslight_util.constant as constant |
| 11 | +import fosslight_dependency.constant as const |
| 12 | +from fosslight_dependency._package_manager import PackageManager |
| 13 | +from fosslight_dependency._package_manager import check_and_run_license_scanner, get_url_to_purl |
| 14 | + |
| 15 | +logger = logging.getLogger(constant.LOGGER_NAME) |
| 16 | +proprietary_license = 'Proprietary License' |
| 17 | +unclassifed_license = 'UnclassifiedLicense' |
| 18 | +license_md = 'LICENSE.md' |
| 19 | +third_party_md = 'Third Party Notices.md' |
| 20 | + |
| 21 | + |
| 22 | +class Unity(PackageManager): |
| 23 | + package_manager_name = const.UNITY |
| 24 | + |
| 25 | + input_file_name = const.SUPPORT_PACKAE.get(package_manager_name) |
| 26 | + packageCache_dir = os.path.join('Library', 'PackageCache') |
| 27 | + mirror_url = 'https://github.com/needle-mirror/' |
| 28 | + unity_internal_url = 'https://github.cds.internal.unity3d.com' |
| 29 | + third_notice_txt = 'third_party_notice.txt' |
| 30 | + |
| 31 | + def __init__(self, input_dir, output_dir): |
| 32 | + super().__init__(self.package_manager_name, '', input_dir, output_dir) |
| 33 | + self.append_input_package_list_file(self.input_file_name) |
| 34 | + |
| 35 | + def parse_oss_information(self, f_name): |
| 36 | + comment = '' |
| 37 | + |
| 38 | + with open(f_name, 'r', encoding='utf8') as f: |
| 39 | + f_yml = yaml.safe_load(f) |
| 40 | + resolvedPkg = f_yml['m_ResolvedPackages'] |
| 41 | + |
| 42 | + try: |
| 43 | + sheet_list = [] |
| 44 | + |
| 45 | + for pkg_data in resolvedPkg: |
| 46 | + oss_name = pkg_data['name'] |
| 47 | + oss_version = pkg_data['version'] |
| 48 | + |
| 49 | + oss_packagecache_dir = os.path.join(self.packageCache_dir, f'{oss_name}@{oss_version}') |
| 50 | + license_f = os.path.join(oss_packagecache_dir, license_md) |
| 51 | + if os.path.isfile(license_f): |
| 52 | + license_name = check_and_run_license_scanner(self.platform, |
| 53 | + self.license_scanner_bin, |
| 54 | + license_f) |
| 55 | + if license_name == unclassifed_license or license_name == '': |
| 56 | + with open(license_f, 'r', encoding='utf-8') as f: |
| 57 | + for line in f: |
| 58 | + matched_l = re.search(r'Unity\s[\s\w]*\sLicense', line) |
| 59 | + if matched_l: |
| 60 | + license_name = matched_l[0] |
| 61 | + break |
| 62 | + else: |
| 63 | + license_name = proprietary_license |
| 64 | + |
| 65 | + third_f = os.path.join(oss_packagecache_dir, third_party_md) |
| 66 | + if os.path.isfile(third_f): |
| 67 | + with open(third_f, 'r', encoding='utf-8') as f: |
| 68 | + third_notice = f.readlines() |
| 69 | + with open(self.third_notice_txt, 'a+', encoding='utf-8') as tf: |
| 70 | + for line in third_notice: |
| 71 | + tf.write(line) |
| 72 | + tf.flush() |
| 73 | + |
| 74 | + homepage = pkg_data['repository']['url'] |
| 75 | + if homepage and homepage.startswith('git@'): |
| 76 | + homepage = homepage.replace('git@', 'https://') |
| 77 | + if homepage is None or homepage.startswith(self.unity_internal_url): |
| 78 | + if license_name != proprietary_license: |
| 79 | + homepage = f'{self.mirror_url}{oss_name}' |
| 80 | + if homepage is None: |
| 81 | + homepage = '' |
| 82 | + |
| 83 | + dn_loc = homepage |
| 84 | + purl = get_url_to_purl(dn_loc, self.package_manager_name) |
| 85 | + if purl == 'None': |
| 86 | + purl = '' |
| 87 | + |
| 88 | + comment_list = [] |
| 89 | + if self.direct_dep: |
| 90 | + if pkg_data['isDirectDependency']: |
| 91 | + comment_list.append('direct') |
| 92 | + else: |
| 93 | + comment_list.append('transitive') |
| 94 | + |
| 95 | + comment = ','.join(comment_list) |
| 96 | + sheet_list.append([purl, oss_name, oss_version, license_name, dn_loc, homepage, |
| 97 | + '', '', comment, '']) |
| 98 | + except Exception as e: |
| 99 | + logger.error(f"Fail to parse unity oss information: {e}") |
| 100 | + |
| 101 | + return sheet_list |
0 commit comments