Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 69 additions & 0 deletions src/fosslight_util/convert_excel_to_yaml.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/fosslight_util/write_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added tests/FOSSLight-Report_sample.xlsx
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/test_convert_to_yaml.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down