Skip to content

Commit

Permalink
Remove convert_excel_to_yaml code
Browse files Browse the repository at this point in the history
  • Loading branch information
bjk7119 committed Aug 10, 2022
1 parent 6d566a8 commit f95cb2c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 94 deletions.
47 changes: 6 additions & 41 deletions src/fosslight_oss_pkg/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# SPDX-License-Identifier: GPL-3.0-only
import os
import sys
import re
import logging
import platform
from datetime import datetime
Expand All @@ -14,47 +13,24 @@
from fosslight_util.set_log import init_log
from fosslight_util.output_format import check_output_format
from fosslight_util.parsing_yaml import find_sbom_yaml_files
from ._parsing_excel import convert_excel_to_yaml, convert_yml_to_excel
from ._parsing_excel import convert_yml_to_excel

CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'yaml': '.yaml', 'excel': '.xlsx'}
CUSTOMIZED_FORMAT_FOR_PRECHECKER = {'excel': '.xlsx'}
_PKG_NAME = "fosslight_prechecker"
logger = logging.getLogger(LOGGER_NAME)


def find_report_file(path_to_find):
file_to_find = ["FOSSLight-Report.xlsx", "OSS-Report.xlsx"]

try:
for file in file_to_find:
file_with_path = os.path.join(path_to_find, file)
if os.path.isfile(file_with_path):
return file
for root, dirs, files in os.walk(path_to_find):
for file in files:
file_name = file.lower()
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))
return ""


def check_extension_and_format(file, format):
if (file.endswith((".yaml", ".yml")) and format == "yaml") or \
(file.endswith(".xlsx") and format == "excel"):
if (file.endswith((".yaml", ".yml")) and format == "yaml"):
logger.error(f"File extension is not matched with input format({format})")
sys.exit(1)


def convert_report(base_path, output_name, format, need_log_file=True, sheet_names=""):
def convert_report(base_path, output_name, format, need_log_file=True):
oss_yaml_files = []
oss_report_files = "" # TODO: Change to list type for multiple Report files
file_option_on = False
convert_yml_mode = False
convert_excel_mode = False
output_report = ""
output_yaml = ""
now = datetime.now().strftime('%Y%m%d_%H-%M-%S')
is_window = platform.system() == "Windows"

Expand All @@ -72,10 +48,8 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
pass
if output_name != "":
output_report = os.path.join(output_path, output_name)
output_yaml = os.path.join(output_path, output_name)
else:
output_report = os.path.join(os.path.abspath(output_path), f"FOSSLight-Report_{now}")
output_yaml = os.path.join(os.path.abspath(output_path), f"fosslight-sbom-info_{now}")
else:
logger.error(f"Format error - {msg}")
sys.exit(1)
Expand All @@ -89,33 +63,24 @@ def convert_report(base_path, output_name, format, need_log_file=True, sheet_nam
files_to_convert = base_path.split(",")
for file in files_to_convert:
check_extension_and_format(file, format)
if file.endswith(".xlsx"):
convert_excel_mode = True
oss_report_files = file
elif file.endswith((".yaml", ".yml")):
if file.endswith((".yaml", ".yml")):
convert_yml_mode = True
file_option_on = True
oss_yaml_files.append(file)
else:
logger.error("Not support file name or extension")
sys.exit(1)

if not convert_yml_mode and not convert_excel_mode:
if not convert_yml_mode:
if is_window:
convert_yml_mode = True
oss_report_files = find_report_file(base_path)
if oss_report_files != "":
convert_excel_mode = True
else:
logger.info("fosslight_prechecker: can't convert anything")
logger.info("Try 'fosslight_prechecker -h for more information")

if convert_yml_mode:
convert_yml_to_excel(oss_yaml_files, output_report, file_option_on, base_path)

if convert_excel_mode:
convert_excel_to_yaml(oss_report_files, output_yaml, sheet_names)

try:
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
logger.info(_str_final_result_log)
Expand Down
44 changes: 1 addition & 43 deletions src/fosslight_oss_pkg/_parsing_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +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 parsing_yml
from fosslight_util.output_format import write_output_file
from fosslight_util.write_yaml import create_yaml_with_ossitem
from fosslight_util.read_excel import read_oss_report

logger = logging.getLogger(LOGGER_NAME)
IDX_CANNOT_FOUND = -1


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

try:
sheet_list["SRC_FL_Prechecker"] = items_to_print
success, msg, result_file = write_output_file(output_file, '.xlsx',
sheet_list)
success, msg, result_file = write_output_file(output_file, '.xlsx', sheet_list)
if success:
if result_file:
logger.warning(f"Output: {result_file}")
Expand All @@ -46,39 +40,3 @@ def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path)
logger.error(f"Error to write excel file : {msg}")
except Exception as ex:
logger.error(f"Error to write excel file : {ex}")


def convert_excel_to_yaml(oss_report_to_read, output_file, sheet_names=""):
_file_extension = ".yaml"
yaml_dict = {}

if os.path.isfile(oss_report_to_read):
try:
items = read_oss_report(oss_report_to_read, sheet_names)
for item in items:
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
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):
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
8 changes: 3 additions & 5 deletions src/fosslight_prechecker/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
Usage: fosslight_prechecker [Mode] [option1] <arg1> [option2] <arg2>...
ex) fosslight_prechecker lint -p /home/test/src/
fosslight_prechecker add -p /home/test/test.py -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"
fosslight_prechecker convert -p /home/test/sbom_info.py
Parameters:
Mode
lint\t\t Check whether the copyright and license writing rules are complied with
convert\t\t Convert oss_pkg_info.yaml <-> FOSSLight-Report.xlsx
convert\t\t Convert sbom_info.yaml -> FOSSLight-Report.xlsx
add\t\t\t Add missing license and copyright
Options:
Expand All @@ -28,10 +29,7 @@
Options for only 'add' mode
-l <license>\t License name(SPDX format) to add
-c <copyright>\t Copyright to add(ex, 2015-2021 LG Electronics Inc.)
Options for only 'convert' mode
-s <sheet_names>\t Sheet name in excel to change to yaml(ex. SRC,BIN)"""
-c <copyright>\t Copyright to add(ex, 2015-2021 LG Electronics Inc.)"""


def print_help_msg(exitOpt=True):
Expand Down
3 changes: 1 addition & 2 deletions src/fosslight_prechecker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def main():
parser.add_argument('--no', '-n', help='Disable automatic exclude mode', action='store_true', dest='disable')
parser.add_argument('--license', '-l', help='License name to add', type=str, dest='license', default="")
parser.add_argument('--copyright', '-c', help='Copyright to add', type=str, dest='copyright', default="")
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')
Expand All @@ -43,7 +42,7 @@ def main():
if args.mode == "lint":
run_lint(args.path, args.disable, args.output, args.format, args.log)
elif args.mode == "convert":
convert_report(args.path, args.output, args.format, args.log, args.sheet)
convert_report(args.path, args.output, args.format, args.log)
elif args.mode == "add":
add_content(args.path, args.license, args.copyright, args.output, args.log)
else:
Expand Down
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ commands =
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
fosslight_prechecker convert -p tests/convert
fosslight_prechecker convert -p tests/convert/OSS-Report-Sample_1_BOM.xlsx -o test_convert/output.yaml
cat test_convert/output.yaml
rm -rf tests/add_result
rm -rf tests/add/LICENSES
rm -rf tests/add/LICENSE
Expand All @@ -42,7 +40,6 @@ commands =
fosslight_prechecker lint -p src/ -o "test_result/prechecker_result.yaml"
fosslight_prechecker lint -p src/ -f yaml -o "test_result2/prechecker_result.yaml"
fosslight_prechecker convert -p tests/convert
fosslight_prechecker convert -p tests/convert/OSS-Report-Sample_1_BOM.xlsx -o test_convert/output
cp -r tests/add tests/add_result
fosslight_prechecker add -p tests/add_result -c "2019-2021 LG Electronics Inc." -l "GPL-3.0-only"
fosslight_prechecker add -p tests/add -l EPL-1.0
Expand Down

0 comments on commit f95cb2c

Please sign in to comment.