Skip to content

Commit 101daf7

Browse files
authored
Merge pull request #98 from fosslight/temp_jk
Remove the �function to convert excel to yaml
2 parents 98ecad1 + 4d46bbb commit 101daf7

File tree

5 files changed

+14
-114
lines changed

5 files changed

+14
-114
lines changed

src/fosslight_oss_pkg/_convert.py

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,26 @@
44
# SPDX-License-Identifier: GPL-3.0-only
55
import os
66
import sys
7-
import re
87
import logging
9-
import platform
108
from datetime import datetime
119
from pathlib import Path
1210
from yaml import safe_dump
1311
from fosslight_util.constant import LOGGER_NAME
1412
from fosslight_util.set_log import init_log
1513
from fosslight_util.output_format import check_output_format
1614
from fosslight_util.parsing_yaml import find_sbom_yaml_files
17-
from ._parsing_excel import convert_excel_to_yaml, convert_yml_to_excel
15+
from ._parsing_excel import convert_yml_to_excel
1816

19-
CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'yaml': '.yaml', 'excel': '.xlsx'}
17+
CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'excel': '.xlsx'}
2018
_PKG_NAME = "fosslight_prechecker"
2119
logger = logging.getLogger(LOGGER_NAME)
2220

2321

24-
def find_report_file(path_to_find):
25-
file_to_find = ["FOSSLight-Report.xlsx", "OSS-Report.xlsx"]
26-
27-
try:
28-
for file in file_to_find:
29-
file_with_path = os.path.join(path_to_find, file)
30-
if os.path.isfile(file_with_path):
31-
return file
32-
for root, dirs, files in os.walk(path_to_find):
33-
for file in files:
34-
file_name = file.lower()
35-
p = re.compile(r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx", re.I)
36-
if p.search(file_name):
37-
return os.path.join(root, file)
38-
except Exception as error:
39-
logger.debug("Find report:"+str(error))
40-
return ""
41-
42-
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-
50-
def convert_report(base_path, output_name, format, need_log_file=True, sheet_names=""):
22+
def convert_report(base_path, output_name, format, need_log_file=True):
5123
oss_yaml_files = []
52-
oss_report_files = "" # TODO: Change to list type for multiple Report files
5324
file_option_on = False
54-
convert_yml_mode = False
55-
convert_excel_mode = False
5625
output_report = ""
57-
output_yaml = ""
5826
now = datetime.now().strftime('%Y%m%d_%H-%M-%S')
59-
is_window = platform.system() == "Windows"
6027

6128
success, msg, output_path, output_name, output_extension = check_output_format(output_name, format, CUSTOMIZED_FORMAT_FOR_PRECHECKER)
6229

@@ -72,49 +39,30 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
7239
pass
7340
if output_name != "":
7441
output_report = os.path.join(output_path, output_name)
75-
output_yaml = os.path.join(output_path, output_name)
7642
else:
7743
output_report = os.path.join(os.path.abspath(output_path), f"FOSSLight-Report_{now}")
78-
output_yaml = os.path.join(os.path.abspath(output_path), f"fosslight-sbom-info_{now}")
7944
else:
8045
logger.error(f"Format error - {msg}")
8146
sys.exit(1)
8247

8348
if os.path.isdir(base_path):
8449
oss_yaml_files = find_sbom_yaml_files(base_path)
85-
if oss_yaml_files:
86-
convert_yml_mode = True
8750
else:
8851
if base_path != "":
8952
files_to_convert = base_path.split(",")
53+
file_option_on = True
9054
for file in files_to_convert:
91-
check_extension_and_format(file, format)
92-
if file.endswith(".xlsx"):
93-
convert_excel_mode = True
94-
oss_report_files = file
95-
elif file.endswith((".yaml", ".yml")):
96-
convert_yml_mode = True
97-
file_option_on = True
55+
if file.endswith((".yaml", ".yml")):
9856
oss_yaml_files.append(file)
9957
else:
10058
logger.error("Not support file name or extension")
10159
sys.exit(1)
10260

103-
if not convert_yml_mode and not convert_excel_mode:
104-
if is_window:
105-
convert_yml_mode = True
106-
oss_report_files = find_report_file(base_path)
107-
if oss_report_files != "":
108-
convert_excel_mode = True
109-
else:
110-
logger.info("fosslight_prechecker: can't convert anything")
111-
logger.info("Try 'fosslight_prechecker -h for more information")
112-
113-
if convert_yml_mode:
61+
if oss_yaml_files:
11462
convert_yml_to_excel(oss_yaml_files, output_report, file_option_on, base_path)
115-
116-
if convert_excel_mode:
117-
convert_excel_to_yaml(oss_report_files, output_yaml, sheet_names)
63+
else:
64+
logger.info("fosslight_prechecker: can't convert anything")
65+
logger.info("Try 'fosslight_prechecker -h for more information")
11866

11967
try:
12068
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)

src/fosslight_oss_pkg/_parsing_excel.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
# SPDX-License-Identifier: GPL-3.0-only
55
import logging
66
import os
7-
import sys
8-
import yaml
97
from fosslight_util.constant import LOGGER_NAME
108
from fosslight_util.parsing_yaml import parsing_yml
119
from fosslight_util.output_format import write_output_file
12-
from fosslight_util.write_yaml import create_yaml_with_ossitem
13-
from fosslight_util.read_excel import read_oss_report
1410

1511
logger = logging.getLogger(LOGGER_NAME)
16-
IDX_CANNOT_FOUND = -1
1712

1813

1914
def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path):
@@ -35,8 +30,7 @@ def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path)
3530

3631
try:
3732
sheet_list["SRC_FL_Prechecker"] = items_to_print
38-
success, msg, result_file = write_output_file(output_file, '.xlsx',
39-
sheet_list)
33+
success, msg, result_file = write_output_file(output_file, '.xlsx', sheet_list)
4034
if success:
4135
if result_file:
4236
logger.warning(f"Output: {result_file}")
@@ -46,39 +40,3 @@ def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path)
4640
logger.error(f"Error to write excel file : {msg}")
4741
except Exception as ex:
4842
logger.error(f"Error to write excel file : {ex}")
49-
50-
51-
def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
52-
_file_extension = ".yaml"
53-
yaml_dict = {}
54-
55-
if os.path.isfile(oss_report_to_read):
56-
try:
57-
items = read_oss_report(oss_report_to_read, sheet_names)
58-
for item in items:
59-
create_yaml_with_ossitem(item, yaml_dict)
60-
if yaml_dict:
61-
output_file = output_file if output_file.endswith(_file_extension) else output_file + _file_extension
62-
success = write_yaml_file(output_file, yaml_dict)
63-
if success:
64-
logger.warning(f"Output: {output_file}")
65-
else:
66-
logger.error(f"Can't write yaml file : {output_file}")
67-
sys.exit(1)
68-
except Exception as error:
69-
logger.error(f"Convert yaml: {error}")
70-
else:
71-
logger.error(f"Can't find a file: {oss_report_to_read}")
72-
73-
74-
def write_yaml_file(output_file, json_output):
75-
success = True
76-
error_msg = ""
77-
78-
try:
79-
with open(output_file, 'w') as f:
80-
yaml.dump(json_output, f, sort_keys=False)
81-
except Exception as ex:
82-
error_msg = str(ex)
83-
success = False
84-
return success, error_msg

src/fosslight_prechecker/_help.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
Usage: fosslight_prechecker [Mode] [option1] <arg1> [option2] <arg2>...
1111
ex) fosslight_prechecker lint -p /home/test/src/
1212
fosslight_prechecker add -p /home/test/test.py -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"
13+
fosslight_prechecker convert -p /home/test/sbom_info.py
1314
1415
Parameters:
1516
Mode
1617
lint\t\t Check whether the copyright and license writing rules are complied with
17-
convert\t\t Convert oss_pkg_info.yaml <-> FOSSLight-Report.xlsx
18+
convert\t\t Convert sbom_info.yaml -> FOSSLight-Report.xlsx
1819
add\t\t\t Add missing license and copyright
1920
2021
Options:
@@ -28,10 +29,7 @@
2829
2930
Options for only 'add' mode
3031
-l <license>\t License name(SPDX format) to add
31-
-c <copyright>\t Copyright to add(ex, 2015-2021 LG Electronics Inc.)
32-
33-
Options for only 'convert' mode
34-
-s <sheet_names>\t Sheet name in excel to change to yaml(ex. SRC,BIN)"""
32+
-c <copyright>\t Copyright to add(ex, 2015-2021 LG Electronics Inc.)"""
3533

3634

3735
def print_help_msg(exitOpt=True):

src/fosslight_prechecker/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def main():
2121
parser.add_argument('--no', '-n', help='Disable automatic exclude mode', action='store_true', dest='disable')
2222
parser.add_argument('--license', '-l', help='License name to add', type=str, dest='license', default="")
2323
parser.add_argument('--copyright', '-c', help='Copyright to add', type=str, dest='copyright', default="")
24-
parser.add_argument('--sheet', '-s', help='Sheet name to change to yaml', type=str, dest='sheet', default="")
2524
parser.add_argument('--help', '-h', help='Print help message', action='store_true', dest='help')
2625
parser.add_argument('--ignore', '-i', help='Do not write log to file', action='store_false', dest='log')
2726
parser.add_argument('--version', '-v', help='Print FOSSLight Prechecker version', action='store_true', dest='version')
@@ -43,7 +42,7 @@ def main():
4342
if args.mode == "lint":
4443
run_lint(args.path, args.disable, args.output, args.format, args.log)
4544
elif args.mode == "convert":
46-
convert_report(args.path, args.output, args.format, args.log, args.sheet)
45+
convert_report(args.path, args.output, args.format, args.log)
4746
elif args.mode == "add":
4847
add_content(args.path, args.license, args.copyright, args.output, args.log)
4948
else:

tox.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ commands =
2525
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
2626
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
2727
fosslight_prechecker convert -p tests/convert
28-
fosslight_prechecker convert -p tests/convert/OSS-Report-Sample_1_BOM.xlsx -o test_convert/output.yaml
29-
cat test_convert/output.yaml
3028
rm -rf tests/add_result
3129
rm -rf tests/add/LICENSES
3230
rm -rf tests/add/LICENSE
@@ -42,7 +40,6 @@ commands =
4240
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
4341
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
4442
fosslight_prechecker convert -p tests/convert
45-
fosslight_prechecker convert -p tests/convert/OSS-Report-Sample_1_BOM.xlsx -o test_convert/output
4643
cp -r tests/add tests/add_result
4744
fosslight_prechecker add -p tests/add_result -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"
4845
fosslight_prechecker add -p tests/add -l EPL-1.0

0 commit comments

Comments
 (0)