|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# FOSSLight Scanner |
| 4 | +# Copyright (c) 2022 LG Electronics Inc. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import json |
| 9 | +import yaml |
| 10 | +import logging |
| 11 | +import xlsxwriter |
| 12 | +import codecs |
| 13 | +from pathlib import Path |
| 14 | +from bs4 import BeautifulSoup |
| 15 | +import fosslight_util.constant as constant |
| 16 | +from fosslight_util.compare_yaml import compare_yaml |
| 17 | + |
| 18 | +logger = logging.getLogger(constant.LOGGER_NAME) |
| 19 | +ADD = "add" |
| 20 | +DELETE = "delete" |
| 21 | +CHANGE = "change" |
| 22 | + |
| 23 | + |
| 24 | +def write_result_json_yaml(output_file, compared_result, file_ext): |
| 25 | + ret = True |
| 26 | + try: |
| 27 | + with open(output_file, 'w') as f: |
| 28 | + if file_ext == '.json': |
| 29 | + json.dump(compared_result, f, indent=4, sort_keys=True) |
| 30 | + elif file_ext == '.yaml': |
| 31 | + yaml.dump(compared_result, f, sort_keys=True) |
| 32 | + except Exception: |
| 33 | + ret = False |
| 34 | + return ret |
| 35 | + |
| 36 | + |
| 37 | +def parse_result_for_table(oi, status): |
| 38 | + compared_row = [] |
| 39 | + if status == ADD: |
| 40 | + oss_after = f"{oi['name']} ({oi['version']})" |
| 41 | + license_after = f"{', '.join(oi['license'])}" |
| 42 | + compared_row = [status, '', '', oss_after, license_after] |
| 43 | + elif status == DELETE: |
| 44 | + oss_before = f"{oi['name']} ({oi['version']})" |
| 45 | + license_before = f"{', '.join(oi['license'])}" |
| 46 | + compared_row = [status, oss_before, license_before, '', ''] |
| 47 | + elif status == CHANGE: |
| 48 | + oss_before, oss_after, license_before, license_after = [], [], [], [] |
| 49 | + for prev_i in oi['prev']: |
| 50 | + oss_before.append(f"{oi['name']} ({prev_i['version']})") |
| 51 | + license_before.append(f"{', '.join(prev_i['license'])}") |
| 52 | + for now_i in oi['now']: |
| 53 | + oss_after.append(f"{oi['name']} ({now_i['version']})") |
| 54 | + license_after.append(f"{', '.join(now_i['license'])}") |
| 55 | + compared_row = [status, ' / '.join(oss_before), ' / '.join(license_before), |
| 56 | + ' / '.join(oss_after), ' / '.join(license_after)] |
| 57 | + else: |
| 58 | + logger.error(f"Not supported compared status: {status}") |
| 59 | + |
| 60 | + return compared_row |
| 61 | + |
| 62 | + |
| 63 | +def get_sample_html(): |
| 64 | + RESOURCES_DIR = 'resources' |
| 65 | + SAMPLE_HTML = 'bom_compare.html' |
| 66 | + html_file = os.path.join(RESOURCES_DIR, SAMPLE_HTML) |
| 67 | + |
| 68 | + try: |
| 69 | + base_dir = sys._MEIPASS |
| 70 | + except Exception: |
| 71 | + base_dir = os.path.dirname(__file__) |
| 72 | + |
| 73 | + file_withpath = os.path.join(base_dir, html_file) |
| 74 | + try: |
| 75 | + html_f = codecs.open(file_withpath, 'r', 'utf-8') |
| 76 | + except Exception as ex: |
| 77 | + logger.error(f"Error to get sample html file : {ex}") |
| 78 | + |
| 79 | + return html_f |
| 80 | + |
| 81 | + |
| 82 | +def write_result_html(output_file, compared_result, before_yaml, after_yaml): |
| 83 | + ret = True |
| 84 | + |
| 85 | + f = BeautifulSoup(get_sample_html().read(), 'html.parser') |
| 86 | + f.find("li", {"class": "before_f"}).append(before_yaml) |
| 87 | + f.find("li", {"class": "after_f"}).append(after_yaml) |
| 88 | + |
| 89 | + table_html = f.find("table", {"id": "comp_result"}) |
| 90 | + |
| 91 | + status = [ADD, DELETE, CHANGE] |
| 92 | + row = 2 |
| 93 | + for st in status: |
| 94 | + for oi in compared_result[st]: |
| 95 | + compared_row = parse_result_for_table(oi, st) |
| 96 | + tr = f.new_tag('tr') |
| 97 | + for i, ci in enumerate(compared_row): |
| 98 | + td = f.new_tag('td') |
| 99 | + td.string = ci |
| 100 | + td.attrs = {"style": "padding:5px;"} |
| 101 | + tr.insert(i, td) |
| 102 | + table_html.insert(row, tr) |
| 103 | + row += 1 |
| 104 | + |
| 105 | + with open(output_file, "wb") as f_out: |
| 106 | + f_out.write(f.prettify("utf-8")) |
| 107 | + return ret |
| 108 | + |
| 109 | + |
| 110 | +def write_result_xlsx(output_file, compared_result): |
| 111 | + HEADER = ['Status', 'OSS_Before', 'License_Before', 'OSS_After', 'License_After'] |
| 112 | + ret = True |
| 113 | + |
| 114 | + try: |
| 115 | + output_dir = os.path.dirname(output_file) |
| 116 | + Path(output_dir).mkdir(parents=True, exist_ok=True) |
| 117 | + |
| 118 | + workbook = xlsxwriter.Workbook(os.path.basename(output_file)) |
| 119 | + worksheet = workbook.add_worksheet('BOM_compare') |
| 120 | + bold = workbook.add_format({'bold': True}) |
| 121 | + worksheet.write_row(0, 0, HEADER, bold) |
| 122 | + |
| 123 | + row = 1 |
| 124 | + status = [ADD, DELETE, CHANGE] |
| 125 | + for st in status: |
| 126 | + for oi in compared_result[st]: |
| 127 | + compared_row = parse_result_for_table(oi, st) |
| 128 | + worksheet.write_row(row, 0, compared_row) |
| 129 | + row += 1 |
| 130 | + workbook.close() |
| 131 | + except Exception: |
| 132 | + ret = False |
| 133 | + |
| 134 | + return ret |
| 135 | + |
| 136 | + |
| 137 | +def write_compared_result(output_file, compared_result, file_ext, before_yaml='', after_yaml=''): |
| 138 | + success = False |
| 139 | + if file_ext == "" or file_ext == ".xlsx": |
| 140 | + success = write_result_xlsx(output_file, compared_result) |
| 141 | + elif file_ext == ".html": |
| 142 | + success = write_result_html(output_file, compared_result, before_yaml, after_yaml) |
| 143 | + elif file_ext == ".json": |
| 144 | + success = write_result_json_yaml(output_file, compared_result, file_ext) |
| 145 | + elif file_ext == ".yaml": |
| 146 | + success = write_result_json_yaml(output_file, compared_result, file_ext) |
| 147 | + else: |
| 148 | + logger.info("Not supported file extension") |
| 149 | + |
| 150 | + return success |
| 151 | + |
| 152 | + |
| 153 | +def run_compare(before_yaml, after_yaml, output_file, file_ext): |
| 154 | + ret = False |
| 155 | + logger.info("Start compare mode") |
| 156 | + |
| 157 | + compared_result = compare_yaml(before_yaml, after_yaml) |
| 158 | + if compared_result != '': |
| 159 | + ret = write_compared_result(output_file, compared_result, file_ext, before_yaml, after_yaml) |
| 160 | + if ret: |
| 161 | + logger.info(f"Success to write compared result: {output_file}") |
| 162 | + else: |
| 163 | + logger.error("Fail to write compared result file.") |
| 164 | + |
| 165 | + return ret |
0 commit comments