1515from ._parsing_scancode_file_item import parsing_file_item , get_error_from_header
1616from fosslight_util .write_excel import write_excel_and_csv
1717from ._help import print_help_msg_convert
18+ from ._license_matched import get_license_list_to_print
1819
1920logger = logging .getLogger (constant .LOGGER_NAME )
2021_PKG_NAME = "fosslight_source"
2122
2223
23- def convert_json_to_excel (scancode_json , excel_name , _result_log ):
24+ def convert_json_to_excel (scancode_json , excel_name , result_log , need_license = False ):
25+ sheet_license_prefix = "matched_text"
26+ sheet_SRC_prefix = "SRC"
2427 file_list = []
28+ lic_list = {}
2529 msg = ""
2630 success = True
2731
2832 try :
2933 sheet_list = {}
3034 if os .path .isfile (scancode_json ):
31- file_list = get_detected_licenses_from_scancode (
32- scancode_json )
35+ file_list , lic_list = get_detected_licenses_from_scancode (
36+ scancode_json , need_license )
3337 if len (file_list ) > 0 :
3438 file_list = sorted (
3539 file_list , key = lambda row : ('' .join (row .licenses )))
36- sheet_list ["SRC" ] = [scan_item .get_row_to_print () for scan_item in file_list ]
40+ sheet_list [sheet_SRC_prefix ] = [scan_item .get_row_to_print () for scan_item in file_list ]
41+ if need_license :
42+ sheet_list [sheet_license_prefix ] = get_license_list_to_print (lic_list )
3743 elif os .path .isdir (scancode_json ):
3844 for root , dirs , files in os .walk (scancode_json ):
3945 for file in files :
4046 if file .endswith (".json" ):
4147 try :
4248 result_file = os .path .join (root , file )
43- file_list = get_detected_licenses_from_scancode (
44- result_file )
49+ file_list , lic_list = get_detected_licenses_from_scancode (
50+ result_file , need_license )
4551 if len (file_list ) > 0 :
4652 file_name = os .path .basename (file )
4753 file_list = sorted (
4854 file_list , key = lambda row : ('' .join (row .licenses )))
49- sheet_list ["SRC_" + file_name ] = [scan_item .get_row_to_print () for scan_item in file_list ]
55+ sheet_name = sheet_SRC_prefix + "_" + file_name
56+ sheet_list [sheet_name ] = [scan_item .get_row_to_print () for scan_item in file_list ]
57+ if need_license :
58+ lic_sheet_name = sheet_license_prefix + "_" + file_name
59+ sheet_list [lic_sheet_name ] = get_license_list_to_print (lic_list )
5060 except Exception as ex :
5161 logger .warning ("Error parsing " + file + ":" + str (ex ))
5262
5363 success_to_write , writing_msg = write_excel_and_csv (excel_name , sheet_list )
5464 logger .info ("Writing excel :" + str (success_to_write ) + " " + writing_msg )
5565 if success_to_write :
56- _result_log ["FOSSLight Report" ] = excel_name + ".xlsx"
66+ result_log ["FOSSLight Report" ] = excel_name + ".xlsx"
5767
5868 except Exception as ex :
5969 success = False
6070 logger .warning (str (ex ))
6171
6272 scan_result_msg = str (success ) if msg == "" else str (success ) + "," + msg
63- _result_log ["Scan Result" ] = scan_result_msg
73+ result_log ["Scan Result" ] = scan_result_msg
6474
6575 try :
66- _str_final_result_log = yaml .safe_dump (_result_log , allow_unicode = True , sort_keys = True )
76+ _str_final_result_log = yaml .safe_dump (result_log , allow_unicode = True , sort_keys = True )
6777 logger .info (_str_final_result_log )
6878 except Exception as ex :
6979 logger .warning ("Failed to print result log.: " + str (ex ))
7080
7181 return file_list
7282
7383
74- def get_detected_licenses_from_scancode (scancode_json_file ):
84+ def get_detected_licenses_from_scancode (scancode_json_file , need_license ):
7585 file_list = []
86+ license_list = {}
7687 try :
7788 logger .info ("Start parsing " + scancode_json_file )
7889 with open (scancode_json_file , "r" ) as st_json :
7990 st_python = json .load (st_json )
8091 has_error , str_error = get_error_from_header (st_python ["headers" ])
81- rc , file_list , msg = parsing_file_item (st_python ["files" ], has_error )
92+ rc , file_list , msg , license_list = parsing_file_item (st_python ["files" ], has_error , need_license )
8293 logger .info ("|---" + msg )
8394 if has_error :
8495 logger .info ("|---Scan error:" + str_error )
8596 except Exception as error :
8697 logger .warning ("Parsing " + scancode_json_file + ":" + str (error ))
8798 logger .info ("|---Number of files detected: " + str (len (file_list )))
88- return file_list
99+ return file_list , license_list
89100
90101
91102def main ():
@@ -95,16 +106,19 @@ def main():
95106 path_to_find_bin = os .getcwd ()
96107 start_time = datetime .now ().strftime ('%Y-%m-%d_%H-%M-%S' )
97108 output_file_name = ""
109+ print_matched_text = False
98110
99111 try :
100- opts , args = getopt .getopt (argv , 'hp :o:' )
112+ opts , args = getopt .getopt (argv , 'hmp :o:' )
101113 for opt , arg in opts :
102114 if opt == "-h" :
103115 print_help_msg_convert ()
104116 elif opt == "-p" :
105117 path_to_find_bin = arg
106118 elif opt == "-o" :
107119 output_file_name = arg
120+ elif opt == "-m" :
121+ print_matched_text = True
108122 except Exception :
109123 print_help_msg_convert ()
110124
@@ -117,7 +131,7 @@ def main():
117131
118132 logger , _result_log = init_log (os .path .join (output_dir , "fosslight_src_log_" + start_time + ".txt" ),
119133 True , logging .INFO , logging .DEBUG , _PKG_NAME )
120- convert_json_to_excel (path_to_find_bin , oss_report_name , _result_log )
134+ convert_json_to_excel (path_to_find_bin , oss_report_name , _result_log , print_matched_text )
121135
122136
123137if __name__ == '__main__' :
0 commit comments