diff --git a/.reuse/dep5 b/.reuse/dep5 index 4966105..d313177 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -32,4 +32,8 @@ License: CC-BY-3.0 Files: tests/*.yaml Copyright: 2022 LG Electronics +License: Apache-2.0 + +Files: tests/*.xlsx +Copyright: 2022 LG Electronics License: Apache-2.0 \ No newline at end of file diff --git a/src/fosslight_util/convert_excel_to_yaml.py b/src/fosslight_util/convert_excel_to_yaml.py new file mode 100644 index 0000000..74dfce5 --- /dev/null +++ b/src/fosslight_util/convert_excel_to_yaml.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2022 LG Electronics Inc. +# SPDX-License-Identifier: Apache-2.0 +import os +import sys +import logging +import yaml +import re +from fosslight_util.constant import LOGGER_NAME +from fosslight_util.write_yaml import create_yaml_with_ossitem +from fosslight_util.read_excel import read_oss_report + +logger = logging.getLogger(LOGGER_NAME) + + +def find_report_file(path_to_find): + file_to_find = ["FOSSLight-Report.xlsx", "OSS-Report.xlsx"] + + try: + for file in file_to_find: + file_with_path = os.path.join(path_to_find, file) + if os.path.isfile(file_with_path): + return file + for root, dirs, files in os.walk(path_to_find): + for file in files: + file_name = file.lower() + p = re.compile(r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx", re.I) + if p.search(file_name): + return os.path.join(root, file) + except Exception as error: + logger.debug("Find report:"+str(error)) + return "" + + +def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""): + _file_extension = ".yaml" + yaml_dict = {} + + if os.path.isfile(oss_report_to_read): + try: + items = read_oss_report(oss_report_to_read, sheet_names) + for item in items: + create_yaml_with_ossitem(item, yaml_dict) + if yaml_dict: + output_file = output_file if output_file.endswith(_file_extension) else output_file + _file_extension + success = write_yaml_file(output_file, yaml_dict) + if success: + logger.warning(f"Output: {output_file}") + else: + logger.error(f"Can't write yaml file : {output_file}") + sys.exit(1) + except Exception as error: + logger.error(f"Convert yaml: {error}") + else: + logger.error(f"Can't find a file: {oss_report_to_read}") + + +def write_yaml_file(output_file, json_output): + success = True + error_msg = "" + + try: + with open(output_file, 'w') as f: + yaml.dump(json_output, f, sort_keys=False) + except Exception as ex: + error_msg = str(ex) + success = False + return success, error_msg diff --git a/src/fosslight_util/write_yaml.py b/src/fosslight_util/write_yaml.py index a80ee27..591538c 100644 --- a/src/fosslight_util/write_yaml.py +++ b/src/fosslight_util/write_yaml.py @@ -76,8 +76,8 @@ def convert_sheet_to_yaml(sheet_contents, output_file): def create_yaml_with_ossitem(item, yaml_dict): item_json = item.get_print_json() - item_name = item_json.pop("name") + if item_name not in yaml_dict.keys(): yaml_dict[item_name] = [] merged = False diff --git a/tests/FOSSLight-Report_sample.xlsx b/tests/FOSSLight-Report_sample.xlsx new file mode 100644 index 0000000..65d46b9 Binary files /dev/null and b/tests/FOSSLight-Report_sample.xlsx differ diff --git a/tests/test_convert_to_yaml.py b/tests/test_convert_to_yaml.py new file mode 100755 index 0000000..574023d --- /dev/null +++ b/tests/test_convert_to_yaml.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2022 LG Electronics Inc. +# SPDX-License-Identifier: Apache-2.0 +import os +from fosslight_util.set_log import init_log +from fosslight_util.convert_excel_to_yaml import convert_excel_to_yaml + + +def main(): + output_dir = "test_result/convert" + logger, _result_log = init_log(os.path.join(output_dir, "convert_result.txt")) + logger.warning("TESTING - converting excel to yaml") + output_yaml = os.path.join(os.path.abspath(output_dir), "fosslight-sbom-info") + logger.warning(f"output_yaml - {output_yaml}") + convert_excel_to_yaml("tests/FOSSLight-Report_sample.xlsx", output_yaml) + + +if __name__ == '__main__': + main() diff --git a/tox.ini b/tox.ini index ff7578e..7640bee 100644 --- a/tox.ini +++ b/tox.ini @@ -47,6 +47,9 @@ commands = python tests/test_output_format.py # Test - get spdx licenses python tests/test_spdx_licenses.py + # Test - convert excel to yaml + python tests/test_convert_to_yaml.py + cat test_result/convert/fosslight-sbom-info.yaml [testenv:release] deps =