From da4cdd6b681262b6fc77d02810bb5f0615ecc788 Mon Sep 17 00:00:00 2001 From: Jaekwon Bang Date: Tue, 19 Jul 2022 09:55:07 +0900 Subject: [PATCH] Add -v option and error msg in convert mode --- src/fosslight_oss_pkg/_parsing_excel.py | 44 +++++++++++++--------- src/fosslight_oss_pkg/fosslight_oss_pkg.py | 24 ++++++++---- src/fosslight_reuse/_help.py | 1 + src/fosslight_reuse/cli.py | 13 +++++-- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/fosslight_oss_pkg/_parsing_excel.py b/src/fosslight_oss_pkg/_parsing_excel.py index 4a5b87d..f201265 100644 --- a/src/fosslight_oss_pkg/_parsing_excel.py +++ b/src/fosslight_oss_pkg/_parsing_excel.py @@ -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 @@ -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: @@ -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=""): @@ -64,8 +62,12 @@ 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: @@ -73,5 +75,13 @@ def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""): 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 diff --git a/src/fosslight_oss_pkg/fosslight_oss_pkg.py b/src/fosslight_oss_pkg/fosslight_oss_pkg.py index 83ebc08..8adb6e1 100755 --- a/src/fosslight_oss_pkg/fosslight_oss_pkg.py +++ b/src/fosslight_oss_pkg/fosslight_oss_pkg.py @@ -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 @@ -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)) @@ -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: diff --git a/src/fosslight_reuse/_help.py b/src/fosslight_reuse/_help.py index 0679da6..c3fb1a8 100644 --- a/src/fosslight_reuse/_help.py +++ b/src/fosslight_reuse/_help.py @@ -19,6 +19,7 @@ Options: -h\t\t\t Print help message + -v\t\t\t Print FOSSLight Prechecker version -p \t\t Path to check -f \t\t Result format(yaml, xml, html) -o \t Output file name diff --git a/src/fosslight_reuse/cli.py b/src/fosslight_reuse/cli.py index 9412ef4..ba5167b 100644 --- a/src/fosslight_reuse/cli.py +++ b/src/fosslight_reuse/cli.py @@ -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(): @@ -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: @@ -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":