|
| 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 logging |
| 8 | +import subprocess |
| 9 | +import re |
| 10 | +from bs4 import BeautifulSoup |
| 11 | +import urllib.request |
| 12 | +import fosslight_util.constant as constant |
| 13 | +import fosslight_dependency.constant as const |
| 14 | +from fosslight_dependency._package_manager import PackageManager |
| 15 | + |
| 16 | +logger = logging.getLogger(constant.LOGGER_NAME) |
| 17 | + |
| 18 | + |
| 19 | +class Go(PackageManager): |
| 20 | + package_manager_name = const.GO |
| 21 | + |
| 22 | + input_file_name = '' |
| 23 | + is_run_plugin = False |
| 24 | + dn_url = 'https://pkg.go.dev/' |
| 25 | + tmp_file_name = 'tmp_go_list.json' |
| 26 | + |
| 27 | + def __init__(self, input_dir, output_dir): |
| 28 | + super().__init__(self.package_manager_name, self.dn_url, input_dir, output_dir) |
| 29 | + |
| 30 | + def __del__(self): |
| 31 | + if os.path.isfile(self.tmp_file_name): |
| 32 | + os.remove(self.tmp_file_name) |
| 33 | + |
| 34 | + def run_plugin(self): |
| 35 | + ret = True |
| 36 | + |
| 37 | + logger.info("Execute 'go list -m all' to obtain package info.") |
| 38 | + cmd = "go list -m all > " + self.tmp_file_name |
| 39 | + |
| 40 | + ret_cmd = subprocess.call(cmd, shell=True) |
| 41 | + if ret_cmd != 0: |
| 42 | + logger.error("Failed to make the result: " + cmd) |
| 43 | + ret = False |
| 44 | + |
| 45 | + self.append_input_package_list_file(self.tmp_file_name) |
| 46 | + |
| 47 | + return ret |
| 48 | + |
| 49 | + def parse_oss_information(self, f_name): |
| 50 | + with open(f_name, 'r', encoding='utf8') as input_fp: |
| 51 | + sheet_list = [] |
| 52 | + for i, line in enumerate(input_fp.readlines()): |
| 53 | + |
| 54 | + re_result = re.findall(r'(\S+)\s?(\S*)', line) |
| 55 | + try: |
| 56 | + package_path = re_result[0][0] |
| 57 | + oss_name = self.package_manager_name + ":" + package_path |
| 58 | + oss_version = re_result[0][1] |
| 59 | + |
| 60 | + tmp_dn_loc = self.dn_url + package_path |
| 61 | + if oss_version: |
| 62 | + dn_loc = tmp_dn_loc + '@' + oss_version |
| 63 | + else: |
| 64 | + dn_loc = tmp_dn_loc |
| 65 | + |
| 66 | + license_name = '' |
| 67 | + homepage = '' |
| 68 | + comment = '' |
| 69 | + |
| 70 | + for dn_loc_i in [dn_loc, tmp_dn_loc]: |
| 71 | + try: |
| 72 | + res = urllib.request.urlopen(dn_loc_i) |
| 73 | + if res.getcode() == 200: |
| 74 | + urlopen_success = True |
| 75 | + if dn_loc_i == tmp_dn_loc: |
| 76 | + if oss_version: |
| 77 | + comment = 'Cannot connect ' \ |
| 78 | + + dn_loc \ |
| 79 | + + ', so use the latest version page to get the license, homepage.' |
| 80 | + dn_loc = tmp_dn_loc |
| 81 | + break |
| 82 | + except Exception: |
| 83 | + continue |
| 84 | + |
| 85 | + if urlopen_success: |
| 86 | + content = res.read().decode('utf8') |
| 87 | + bs_obj = BeautifulSoup(content, 'html.parser') |
| 88 | + |
| 89 | + license_data = bs_obj.find('a', {'data-test-id': 'UnitHeader-license'}) |
| 90 | + if license_data: |
| 91 | + license_name = license_data.text |
| 92 | + |
| 93 | + repository_data = bs_obj.find('div', {'class': 'UnitMeta-repo'}) |
| 94 | + if repository_data: |
| 95 | + homepage = repository_data.find('a')['href'] |
| 96 | + |
| 97 | + except Exception as e: |
| 98 | + logging.warning("Fail to parse " + line + ": " + str(e)) |
| 99 | + continue |
| 100 | + |
| 101 | + sheet_list.append([const.SUPPORT_PACKAE.get(self.package_manager_name), |
| 102 | + oss_name, oss_version, license_name, dn_loc, homepage, '', '', comment]) |
| 103 | + |
| 104 | + return sheet_list |
0 commit comments