Skip to content

Commit da4cdd6

Browse files
committed
Add -v option and error msg in convert mode
1 parent 6fcc29e commit da4cdd6

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

src/fosslight_oss_pkg/_parsing_excel.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
# SPDX-License-Identifier: GPL-3.0-only
55
import logging
66
import os
7+
import sys
78
import yaml
89
from fosslight_util.constant import LOGGER_NAME
910
from fosslight_util.parsing_yaml import find_all_oss_pkg_files, parsing_yml
10-
from fosslight_util.write_excel import write_result_to_excel, write_result_to_csv
11+
from fosslight_util.write_excel import write_result_to_excel
1112
from fosslight_util.write_yaml import create_yaml_with_ossitem
1213
from fosslight_util.read_excel import read_oss_report
1314

@@ -21,8 +22,6 @@ def convert_yml_to_excel(oss_pkg_files, output_file, file_option_on, base_path,
2122

2223
if not file_option_on:
2324
oss_pkg_files = find_all_oss_pkg_files(base_path, oss_pkg_files)
24-
else:
25-
oss_pkg_files = [pkg_file for pkg_file in oss_pkg_files if pkg_file.endswith(('.yaml', '.yml'))]
2625

2726
for oss_pkg_file in oss_pkg_files:
2827
try:
@@ -37,20 +36,19 @@ def convert_yml_to_excel(oss_pkg_files, output_file, file_option_on, base_path,
3736
except Exception as ex:
3837
logger.error(f"Read yaml file: {ex}")
3938

40-
if not window:
41-
try:
42-
sheet_list["SRC_FL_Reuse"] = items_to_print
43-
write_result_to_csv(f"{output_file}.csv", sheet_list, True)
44-
logger.warning(f"Output: {output_file}_SRC_FL_Reuse.csv")
45-
except Exception as ex:
46-
logger.error(f"Write .xlsx file : {ex}")
4739
if items_to_print and len(items_to_print) > 0:
4840
try:
4941
sheet_list["SRC_FL_Reuse"] = items_to_print
50-
write_result_to_excel(f"{output_file}.xlsx", sheet_list)
51-
logger.warning(f"Output: {output_file}.xlsx")
42+
success = write_result_to_excel(f"{output_file}.xlsx", sheet_list)
43+
if success:
44+
logger.warning(f"Output: {output_file}.xlsx")
45+
else:
46+
logger.error(f"Can't write excel file : {output_file}")
47+
sys.exit(1)
5248
except Exception as ex:
53-
logger.error(f"Write .csv file : {ex}")
49+
logger.error(f"Error to write excel file : {ex}")
50+
else:
51+
logger.warning("There is no item to convert to Excel")
5452

5553

5654
def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
@@ -64,14 +62,26 @@ def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
6462
yaml_dict = create_yaml_with_ossitem(item, yaml_dict)
6563
if yaml_dict:
6664
output_file = output_file if output_file.endswith(_file_extension) else output_file + _file_extension
67-
write_yaml_file(output_file, yaml_dict)
68-
logger.warning(f"Output: {output_file}")
65+
success = write_yaml_file(output_file, yaml_dict)
66+
if success:
67+
logger.warning(f"Output: {output_file}")
68+
else:
69+
logger.error(f"Can't write yaml file : {output_file}")
70+
sys.exit(1)
6971
except Exception as error:
7072
logger.error(f"Convert yaml: {error}")
7173
else:
7274
logger.error(f"Can't find a file: {oss_report_to_read}")
7375

7476

7577
def write_yaml_file(output_file, json_output):
76-
with open(output_file, 'w') as f:
77-
yaml.dump(json_output, f, sort_keys=False)
78+
success = True
79+
error_msg = ""
80+
81+
try:
82+
with open(output_file, 'w') as f:
83+
yaml.dump(json_output, f, sort_keys=False)
84+
except Exception as ex:
85+
error_msg = str(ex)
86+
success = False
87+
return success, error_msg

src/fosslight_oss_pkg/fosslight_oss_pkg.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# SPDX-License-Identifier: GPL-3.0-only
55
import os
66
import sys
7+
import re
78
import logging
89
import platform
910
from datetime import datetime
@@ -31,8 +32,8 @@ def find_report_file(path_to_find):
3132
for root, dirs, files in os.walk(path_to_find):
3233
for file in files:
3334
file_name = file.lower()
34-
if (file_name.startswith("oss-report") or file_name.startswith("fosslight-report")) \
35-
and file_name.endswith(".xlsx"):
35+
p = re.compile(r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx", re.I)
36+
if p.search(file_name):
3637
return os.path.join(root, file)
3738
except Exception as error:
3839
logger.debug("Find report:"+str(error))
@@ -83,15 +84,22 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
8384
if output_extension == '.xlsx':
8485
logger.error("Format error - can make only .yaml file")
8586
sys.exit(1)
86-
convert_excel_mode = True
87-
report_to_read = base_path
88-
else:
87+
p = re.compile(r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx", re.I)
88+
if p.search(base_path):
89+
convert_excel_mode = True
90+
report_to_read = base_path
91+
elif base_path.endswith((".yaml", ".yml")):
8992
if output_extension == '.yaml':
9093
logger.error("Format error - can make only .xlsx file")
9194
sys.exit(1)
92-
oss_pkg_files = base_path.split(',')
93-
convert_yml_mode = True
94-
file_option_on = True
95+
p = re.compile(r"oss-pkg-info[\s\S]*.ya?ml", re.I)
96+
if p.search(base_path):
97+
oss_pkg_files = base_path.split(',')
98+
convert_yml_mode = True
99+
file_option_on = True
100+
else:
101+
logger.error("Not support file name or extension - only support for FOSSLight-Report*.xlsx or oss-pkg-info*.yaml file")
102+
sys.exit(1)
95103

96104
if not convert_yml_mode and not convert_excel_mode:
97105
if is_window:

src/fosslight_reuse/_help.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
2020
Options:
2121
-h\t\t\t Print help message
22+
-v\t\t\t Print FOSSLight Prechecker version
2223
-p <path>\t\t Path to check
2324
-f <format>\t\t Result format(yaml, xml, html)
2425
-o <file_name>\t Output file name

src/fosslight_reuse/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: GPL-3.0-only
55
import argparse
6-
from ._help import print_help_msg
7-
from ._fosslight_reuse import run_lint
6+
import sys
7+
from fosslight_util.help import print_package_version
8+
from fosslight_reuse._help import print_help_msg
9+
from fosslight_reuse._fosslight_reuse import run_lint, PKG_NAME
810
from fosslight_oss_pkg.fosslight_oss_pkg import convert_report
9-
from ._add import add_content
11+
from fosslight_reuse._add import add_content
1012

1113

1214
def main():
@@ -21,6 +23,7 @@ def main():
2123
parser.add_argument('--sheet', '-s', help='Sheet name to change to yaml', type=str, dest='sheet', default="")
2224
parser.add_argument('--help', '-h', help='Print help message', action='store_true', dest='help')
2325
parser.add_argument('--ignore', '-i', help='Do not write log to file', action='store_false', dest='log')
26+
parser.add_argument('--version', '-v', help='Print FOSSLight Prechecker version', action='store_true', dest='version')
2427
try:
2528
args = parser.parse_args()
2629
except SystemExit:
@@ -29,6 +32,10 @@ def main():
2932
if args.help:
3033
print_help_msg()
3134

35+
if args.version:
36+
print_package_version(PKG_NAME, "FOSSLight Prechecker Version")
37+
sys.exit(0)
38+
3239
if args.mode == "lint":
3340
run_lint(args.path, args.disable, args.output, args.format, args.log)
3441
elif args.mode == "convert":

0 commit comments

Comments
 (0)