Skip to content

Commit

Permalink
Merge pull request #85 from fosslight/develop
Browse files Browse the repository at this point in the history
Add -v option and error handling in convert mode
  • Loading branch information
bjk7119 authored Jul 20, 2022
2 parents fd133f1 + da4cdd6 commit b355198
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
44 changes: 27 additions & 17 deletions src/fosslight_oss_pkg/_parsing_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# SPDX-License-Identifier: GPL-3.0-only
import logging
import os
import sys
import yaml
from fosslight_util.constant import LOGGER_NAME
from fosslight_util.parsing_yaml import find_all_oss_pkg_files, parsing_yml
from fosslight_util.write_excel import write_result_to_excel, write_result_to_csv
from fosslight_util.write_excel import write_result_to_excel
from fosslight_util.write_yaml import create_yaml_with_ossitem
from fosslight_util.read_excel import read_oss_report

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

if not file_option_on:
oss_pkg_files = find_all_oss_pkg_files(base_path, oss_pkg_files)
else:
oss_pkg_files = [pkg_file for pkg_file in oss_pkg_files if pkg_file.endswith(('.yaml', '.yml'))]

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

if not window:
try:
sheet_list["SRC_FL_Reuse"] = items_to_print
write_result_to_csv(f"{output_file}.csv", sheet_list, True)
logger.warning(f"Output: {output_file}_SRC_FL_Reuse.csv")
except Exception as ex:
logger.error(f"Write .xlsx file : {ex}")
if items_to_print and len(items_to_print) > 0:
try:
sheet_list["SRC_FL_Reuse"] = items_to_print
write_result_to_excel(f"{output_file}.xlsx", sheet_list)
logger.warning(f"Output: {output_file}.xlsx")
success = write_result_to_excel(f"{output_file}.xlsx", sheet_list)
if success:
logger.warning(f"Output: {output_file}.xlsx")
else:
logger.error(f"Can't write excel file : {output_file}")
sys.exit(1)
except Exception as ex:
logger.error(f"Write .csv file : {ex}")
logger.error(f"Error to write excel file : {ex}")
else:
logger.warning("There is no item to convert to Excel")


def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
Expand All @@ -64,14 +62,26 @@ def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
yaml_dict = 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
write_yaml_file(output_file, yaml_dict)
logger.warning(f"Output: {output_file}")
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):
with open(output_file, 'w') as f:
yaml.dump(json_output, f, sort_keys=False)
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
24 changes: 16 additions & 8 deletions src/fosslight_oss_pkg/fosslight_oss_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: GPL-3.0-only
import os
import sys
import re
import logging
import platform
from datetime import datetime
Expand Down Expand Up @@ -31,8 +32,8 @@ def find_report_file(path_to_find):
for root, dirs, files in os.walk(path_to_find):
for file in files:
file_name = file.lower()
if (file_name.startswith("oss-report") or file_name.startswith("fosslight-report")) \
and file_name.endswith(".xlsx"):
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))
Expand Down Expand Up @@ -83,15 +84,22 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
if output_extension == '.xlsx':
logger.error("Format error - can make only .yaml file")
sys.exit(1)
convert_excel_mode = True
report_to_read = base_path
else:
p = re.compile(r"[\s\S]*OSS[\s\S]*-Report[\s\S]*.xlsx", re.I)
if p.search(base_path):
convert_excel_mode = True
report_to_read = base_path
elif base_path.endswith((".yaml", ".yml")):
if output_extension == '.yaml':
logger.error("Format error - can make only .xlsx file")
sys.exit(1)
oss_pkg_files = base_path.split(',')
convert_yml_mode = True
file_option_on = True
p = re.compile(r"oss-pkg-info[\s\S]*.ya?ml", re.I)
if p.search(base_path):
oss_pkg_files = base_path.split(',')
convert_yml_mode = True
file_option_on = True
else:
logger.error("Not support file name or extension - only support for FOSSLight-Report*.xlsx or oss-pkg-info*.yaml file")
sys.exit(1)

if not convert_yml_mode and not convert_excel_mode:
if is_window:
Expand Down
1 change: 1 addition & 0 deletions src/fosslight_reuse/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Options:
-h\t\t\t Print help message
-v\t\t\t Print FOSSLight Prechecker version
-p <path>\t\t Path to check
-f <format>\t\t Result format(yaml, xml, html)
-o <file_name>\t Output file name
Expand Down
13 changes: 10 additions & 3 deletions src/fosslight_reuse/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: GPL-3.0-only
import argparse
from ._help import print_help_msg
from ._fosslight_reuse import run_lint
import sys
from fosslight_util.help import print_package_version
from fosslight_reuse._help import print_help_msg
from fosslight_reuse._fosslight_reuse import run_lint, PKG_NAME
from fosslight_oss_pkg.fosslight_oss_pkg import convert_report
from ._add import add_content
from fosslight_reuse._add import add_content


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

if args.version:
print_package_version(PKG_NAME, "FOSSLight Prechecker Version")
sys.exit(0)

if args.mode == "lint":
run_lint(args.path, args.disable, args.output, args.format, args.log)
elif args.mode == "convert":
Expand Down

0 comments on commit b355198

Please sign in to comment.