|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2021 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import logging |
| 9 | +import platform |
| 10 | +import re |
| 11 | +import base64 |
| 12 | +import fosslight_util.constant as constant |
| 13 | +import fosslight_dependency.constant as const |
| 14 | + |
| 15 | +try: |
| 16 | + from github import Github |
| 17 | +except Exception: |
| 18 | + pass |
| 19 | + |
| 20 | +logger = logging.getLogger(constant.LOGGER_NAME) |
| 21 | + |
| 22 | +# binary url to check license text |
| 23 | +_license_scanner_linux = "third_party/nomos/nomossa" |
| 24 | +_license_scanner_macos = "third_party/askalono/askalono_macos" |
| 25 | +_license_scanner_windows = "third_party\\askalono\\askalono.exe" |
| 26 | + |
| 27 | + |
| 28 | +class PackageManager: |
| 29 | + input_package_list_file = [] |
| 30 | + |
| 31 | + def __init__(self, package_manager_name, dn_url, input_dir, output_dir): |
| 32 | + self.input_package_list_file = [] |
| 33 | + self.package_manager_name = package_manager_name |
| 34 | + self.input_dir = input_dir |
| 35 | + self.output_dir = output_dir |
| 36 | + self.dn_url = dn_url |
| 37 | + |
| 38 | + self.platform = platform.system() |
| 39 | + self.license_scanner_bin = check_license_scanner(self.platform) |
| 40 | + |
| 41 | + def run_plugin(self): |
| 42 | + logger.info('This package manager(' + self.package_manager_name + ') skips the step to run plugin.') |
| 43 | + return True |
| 44 | + |
| 45 | + def append_input_package_list_file(self, input_package_file): |
| 46 | + self.input_package_list_file.append(input_package_file) |
| 47 | + |
| 48 | + |
| 49 | +def version_refine(oss_version): |
| 50 | + version_cmp = oss_version.upper() |
| 51 | + |
| 52 | + if version_cmp.find(".RELEASE") != -1: |
| 53 | + oss_version = version_cmp.rstrip(".RELEASE") |
| 54 | + elif version_cmp.find(".FINAL") != -1: |
| 55 | + oss_version = version_cmp.rstrip(".FINAL") |
| 56 | + |
| 57 | + return oss_version |
| 58 | + |
| 59 | + |
| 60 | +def connect_github(github_token): |
| 61 | + if github_token is not None: |
| 62 | + g = Github(github_token) |
| 63 | + else: |
| 64 | + g = Github() |
| 65 | + |
| 66 | + return g |
| 67 | + |
| 68 | + |
| 69 | +def get_github_license(g, github_repo, platform, license_scanner_bin): |
| 70 | + license_name = '' |
| 71 | + tmp_license_txt_file_name = 'tmp_license.txt' |
| 72 | + |
| 73 | + try: |
| 74 | + repository = g.get_repo(github_repo) |
| 75 | + except Exception: |
| 76 | + logger.error("It cannot find the license name. Please use '-t' option with github token.") |
| 77 | + logger.error("{0}{1}".format("refer:https://docs.github.com/en/github/authenticating-to-github/", |
| 78 | + "keeping-your-account-and-data-secure/creating-a-personal-access-token")) |
| 79 | + repository = '' |
| 80 | + |
| 81 | + if repository is not None: |
| 82 | + try: |
| 83 | + license_name = repository.get_license().license.spdx_id |
| 84 | + except Exception: |
| 85 | + logger.info("Cannot find the license name with github api.") |
| 86 | + |
| 87 | + if license_name == "" or license_name == "NOASSERTION": |
| 88 | + try: |
| 89 | + license_txt_data = base64.b64decode(repository.get_license().content).decode('utf-8') |
| 90 | + tmp_license_txt = open(tmp_license_txt_file_name, 'w', encoding='utf-8') |
| 91 | + tmp_license_txt.write(license_txt_data) |
| 92 | + tmp_license_txt.close() |
| 93 | + license_name = check_and_run_license_scanner(platform, license_scanner_bin, tmp_license_txt_file_name) |
| 94 | + except Exception: |
| 95 | + logger.info("Cannot find the license name with license scanner binary.") |
| 96 | + |
| 97 | + if os.path.isfile(tmp_license_txt_file_name): |
| 98 | + os.remove(tmp_license_txt_file_name) |
| 99 | + |
| 100 | + return license_name |
| 101 | + |
| 102 | + |
| 103 | +def check_license_scanner(platform): |
| 104 | + license_scanner_bin = '' |
| 105 | + |
| 106 | + if platform == const.LINUX: |
| 107 | + license_scanner = _license_scanner_linux |
| 108 | + elif platform == const.MACOS: |
| 109 | + license_scanner = _license_scanner_macos |
| 110 | + elif platform == const.WINDOWS: |
| 111 | + license_scanner = _license_scanner_windows |
| 112 | + else: |
| 113 | + logger.debug("Not supported OS to analyze license text with binary.") |
| 114 | + |
| 115 | + if license_scanner: |
| 116 | + try: |
| 117 | + base_path = sys._MEIPASS |
| 118 | + except Exception: |
| 119 | + base_path = os.path.dirname(__file__) |
| 120 | + |
| 121 | + data_path = os.path.join(base_path, license_scanner) |
| 122 | + license_scanner_bin = data_path |
| 123 | + |
| 124 | + return license_scanner_bin |
| 125 | + |
| 126 | + |
| 127 | +def check_and_run_license_scanner(platform, license_scanner_bin, file_dir): |
| 128 | + license_name = '' |
| 129 | + |
| 130 | + if not license_scanner_bin: |
| 131 | + logger.error('Not supported OS for license scanner binary.') |
| 132 | + |
| 133 | + try: |
| 134 | + tmp_output_file_name = "tmp_license_scanner_output.txt" |
| 135 | + |
| 136 | + if file_dir == "UNKNOWN": |
| 137 | + license_name = "" |
| 138 | + else: |
| 139 | + if platform == const.LINUX: |
| 140 | + run_license_scanner = license_scanner_bin + " " + file_dir + " > " + tmp_output_file_name |
| 141 | + elif platform == const.MACOS: |
| 142 | + run_license_scanner = license_scanner_bin + " identify " + file_dir + " > " + tmp_output_file_name |
| 143 | + elif platform == const.WINDOWS: |
| 144 | + run_license_scanner = license_scanner_bin + " identify " + file_dir + " > " + tmp_output_file_name |
| 145 | + else: |
| 146 | + run_license_scanner = '' |
| 147 | + |
| 148 | + if run_license_scanner is None: |
| 149 | + license_name = "" |
| 150 | + return license_name |
| 151 | + else: |
| 152 | + ret = os.system(run_license_scanner) |
| 153 | + if ret != 0: |
| 154 | + logger.info("Cannot find the license name with scanner bin.") |
| 155 | + return "" |
| 156 | + |
| 157 | + fp = open(tmp_output_file_name, "r", encoding='utf8') |
| 158 | + license_output = fp.read() |
| 159 | + fp.close() |
| 160 | + os.remove(tmp_output_file_name) |
| 161 | + |
| 162 | + if platform == const.LINUX: |
| 163 | + license_output_re = re.findall(r'.*contains license\(s\)\s(.*)', license_output) |
| 164 | + else: |
| 165 | + license_output_re = re.findall(r"License:\s{1}(\S*)\s{1}", license_output) |
| 166 | + |
| 167 | + if len(license_output_re) == 1: |
| 168 | + license_name = license_output_re[0] |
| 169 | + if license_name == "No_license_found": |
| 170 | + license_name = "" |
| 171 | + else: |
| 172 | + license_name = "" |
| 173 | + |
| 174 | + except Exception as ex: |
| 175 | + logger.error("Failed to run license scan binary." + str(ex)) |
| 176 | + license_name = "" |
| 177 | + |
| 178 | + return license_name |
0 commit comments