44# SPDX-License-Identifier: GPL-3.0-only
55import os
66import sys
7- import re
87import logging
9- import platform
108from datetime import datetime
119from pathlib import Path
1210from yaml import safe_dump
1311from fosslight_util .constant import LOGGER_NAME
1412from fosslight_util .set_log import init_log
1513from fosslight_util .output_format import check_output_format
1614from 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"
2119logger = 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 )
0 commit comments