Skip to content

Commit 68cc9fb

Browse files
committed
Modify to convert *sbom-info*.yaml
1 parent 1b51976 commit 68cc9fb

File tree

4 files changed

+48
-44
lines changed

4 files changed

+48
-44
lines changed

src/fosslight_oss_pkg/_convert.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from fosslight_util.constant import LOGGER_NAME
1414
from fosslight_util.set_log import init_log
1515
from fosslight_util.output_format import check_output_format
16+
from fosslight_util.parsing_yaml import find_sbom_yaml_files
1617
from ._parsing_excel import convert_excel_to_yaml, convert_yml_to_excel
1718

1819
CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'yaml': '.yaml', 'excel': '.xlsx'}
@@ -39,12 +40,19 @@ def find_report_file(path_to_find):
3940
return ""
4041

4142

43+
def check_extension_and_format(file, format):
44+
if (file.endswith((".yaml", ".yml")) and format == "yaml") or \
45+
(file.endswith(".xlsx") and format == "excel"):
46+
logger.error(f"File extension is not matched with input format({format})")
47+
sys.exit(1)
48+
49+
4250
def convert_report(base_path, output_name, format, need_log_file=True, sheet_names=""):
43-
oss_pkg_files = ["oss-pkg-info.yml", "oss-pkg-info.yaml"]
51+
oss_yaml_files = []
52+
oss_report_files = "" # TODO: Change to list type for multiple Report files
4453
file_option_on = False
4554
convert_yml_mode = False
4655
convert_excel_mode = False
47-
report_to_read = ""
4856
output_report = ""
4957
output_yaml = ""
5058
now = datetime.now().strftime('%Y%m%d_%H-%M-%S')
@@ -72,46 +80,45 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
7280
logger.error(f"Format error - {msg}")
7381
sys.exit(1)
7482

83+
if base_path == "":
84+
base_path = output_path
85+
7586
if os.path.isdir(base_path):
76-
if output_extension == ".yaml":
77-
logger.error("Format error - can make only .xlsx file")
78-
sys.exit(1)
79-
convert_yml_mode = True
87+
oss_yaml_files = find_sbom_yaml_files(base_path)
88+
if oss_yaml_files:
89+
convert_yml_mode = True
8090
else:
8191
if base_path != "":
82-
if base_path.endswith(".xlsx"):
83-
if output_extension == '.xlsx':
84-
logger.error("Format error - can make only .yaml file")
85-
sys.exit(1)
86-
convert_excel_mode = True
87-
report_to_read = base_path
88-
elif base_path.endswith((".yaml", ".yml")):
89-
if output_extension == '.yaml':
90-
logger.error("Format error - can make only .xlsx file")
92+
files_to_convert = base_path.split(",")
93+
for file in files_to_convert:
94+
check_extension_and_format(file, format)
95+
if base_path.endswith(".xlsx"):
96+
convert_excel_mode = True
97+
oss_report_files = base_path
98+
elif base_path.endswith((".yaml", ".yml")):
99+
convert_yml_mode = True
100+
file_option_on = True
101+
oss_yaml_files.append(file)
102+
else:
103+
logger.error("Not support file name or extension")
91104
sys.exit(1)
92-
oss_pkg_files = base_path.split(',')
93-
convert_yml_mode = True
94-
file_option_on = True
95-
else:
96-
logger.error("Not support file name or extension")
97-
sys.exit(1)
98105

99106
if not convert_yml_mode and not convert_excel_mode:
100107
if is_window:
101108
convert_yml_mode = True
102109
base_path = os.getcwd()
103-
report_to_read = find_report_file(base_path)
104-
if report_to_read != "":
110+
oss_report_files = find_report_file(base_path)
111+
if oss_report_files != "":
105112
convert_excel_mode = True
106113
else:
107114
logger.info("fosslight_prechecker: can't convert anything")
108115
logger.info("Try 'fosslight_prechecker -h for more information")
109116

110117
if convert_yml_mode:
111-
convert_yml_to_excel(oss_pkg_files, output_report, file_option_on, base_path, is_window)
118+
convert_yml_to_excel(oss_yaml_files, output_report, file_option_on, base_path)
112119

113120
if convert_excel_mode:
114-
convert_excel_to_yaml(report_to_read, output_yaml, sheet_names)
121+
convert_excel_to_yaml(oss_report_files, output_yaml, sheet_names)
115122

116123
try:
117124
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)

src/fosslight_oss_pkg/_parsing_excel.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
import yaml
99
from fosslight_util.constant import LOGGER_NAME
10-
from fosslight_util.parsing_yaml import find_all_oss_pkg_files, parsing_yml
10+
from fosslight_util.parsing_yaml import parsing_yml
1111
from fosslight_util.output_format import write_output_file
1212
from fosslight_util.write_yaml import create_yaml_with_ossitem
1313
from fosslight_util.read_excel import read_oss_report
@@ -16,21 +16,18 @@
1616
IDX_CANNOT_FOUND = -1
1717

1818

19-
def convert_yml_to_excel(oss_pkg_files, output_file, file_option_on, base_path, window):
19+
def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path):
2020
items_to_print = []
2121
sheet_list = {}
2222

23-
if not file_option_on:
24-
oss_pkg_files = find_all_oss_pkg_files(base_path, oss_pkg_files)
25-
26-
for oss_pkg_file in oss_pkg_files:
23+
for yaml_file in oss_yaml_files:
2724
try:
28-
if os.path.isfile(oss_pkg_file):
29-
logger.warning(f"Read data from : {oss_pkg_file}")
25+
if os.path.isfile(yaml_file):
26+
logger.warning(f"Read data from : {yaml_file}")
3027

3128
if file_option_on:
32-
base_path = os.path.dirname(oss_pkg_file)
33-
oss_items, _ = parsing_yml(oss_pkg_file, base_path)
29+
base_path = os.path.dirname(yaml_file)
30+
oss_items, _ = parsing_yml(yaml_file, base_path)
3431
for item in oss_items:
3532
items_to_print.extend(item.get_print_array())
3633
except Exception as ex:

src/fosslight_prechecker/_add.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from yaml import safe_dump
1313
from fosslight_util.set_log import init_log
1414
from fosslight_util.spdx_licenses import get_spdx_licenses_json
15-
from fosslight_util.parsing_yaml import find_all_oss_pkg_files, parsing_yml
15+
from fosslight_util.parsing_yaml import find_sbom_yaml_files, parsing_yml
1616
from fosslight_util.output_format import check_output_format
1717
from datetime import datetime
1818
from fosslight_prechecker._precheck import precheck_for_project, precheck_for_files, dump_error_msg, get_path_to_find
@@ -298,24 +298,24 @@ def is_exclude_dir(dir_path):
298298

299299

300300
def download_oss_info_license(base_path, input_license=""):
301-
oss_pkg_files = ["oss-pkg-info.yml", "oss-pkg-info.yaml"]
302301
license_list = []
303302
converted_lic_list = []
303+
oss_yaml_files = []
304304
main_parser = reuse_arg_parser()
305305
prj = Project(base_path)
306306

307-
found_oss_pkg_files = find_all_oss_pkg_files(base_path, oss_pkg_files)
307+
oss_yaml_files = find_sbom_yaml_files(base_path)
308308

309309
if input_license != "":
310310
license_list.append(input_license)
311311

312-
if found_oss_pkg_files is None or len(found_oss_pkg_files) == 0:
312+
if oss_yaml_files is None or len(oss_yaml_files) == 0:
313313
logger.info("\n # There is no OSS package Info file in this path\n")
314314
return
315315
else:
316-
logger.info(f"\n # There is OSS Package Info file(s) : {found_oss_pkg_files}\n")
316+
logger.info(f"\n # There is OSS Package Info file(s) : {oss_yaml_files}\n")
317317

318-
for oss_pkg_file in found_oss_pkg_files:
318+
for oss_pkg_file in oss_yaml_files:
319319
_, license_list = parsing_yml(oss_pkg_file, base_path)
320320

321321
for lic in license_list:

src/fosslight_prechecker/_result.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pathlib import Path
1414
from reuse.project import Project
1515
from fosslight_prechecker._result_html import result_for_html
16-
from fosslight_util.parsing_yaml import find_all_oss_pkg_files, parsing_yml
16+
from fosslight_util.parsing_yaml import find_sbom_yaml_files, parsing_yml
1717
from fosslight_util.output_format import check_output_format
1818
import re
1919

@@ -286,8 +286,8 @@ def result_for_summary(path_to_find, oss_pkg_info_files, license_missing_files,
286286
detected_lic.append(lic)
287287

288288
if oss_pkg_info_files:
289-
pkg_info_yaml_files = find_all_oss_pkg_files(path_to_find, oss_pkg_info_files)
290-
yaml_file = get_only_pkg_info_yaml_file(pkg_info_yaml_files)
289+
oss_yaml_files = find_sbom_yaml_files(path_to_find)
290+
yaml_file = get_only_pkg_info_yaml_file(oss_yaml_files)
291291
# Exclude files in yaml
292292
license_missing_files, copyright_missing_files = exclude_file_in_yaml(path_to_find, yaml_file,
293293
set(license_missing_files) - set(oss_pkg_info_files),

0 commit comments

Comments
 (0)