15
15
from ._parsing_scancode_file_item import parsing_file_item , get_error_from_header
16
16
from fosslight_util .write_excel import write_excel_and_csv
17
17
from ._help import print_help_msg_convert
18
+ from ._license_matched import get_license_list_to_print
18
19
19
20
logger = logging .getLogger (constant .LOGGER_NAME )
20
21
_PKG_NAME = "fosslight_source"
21
22
22
23
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"
24
27
file_list = []
28
+ lic_list = {}
25
29
msg = ""
26
30
success = True
27
31
28
32
try :
29
33
sheet_list = {}
30
34
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 )
33
37
if len (file_list ) > 0 :
34
38
file_list = sorted (
35
39
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 )
37
43
elif os .path .isdir (scancode_json ):
38
44
for root , dirs , files in os .walk (scancode_json ):
39
45
for file in files :
40
46
if file .endswith (".json" ):
41
47
try :
42
48
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 )
45
51
if len (file_list ) > 0 :
46
52
file_name = os .path .basename (file )
47
53
file_list = sorted (
48
54
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_list [sheet_SRC_prefix + "_" + file_name ] = [scan_item .get_row_to_print () for scan_item in file_list ]
56
+ if need_license :
57
+ sheet_list [sheet_license_prefix + "_" + file_name ] = get_license_list_to_print (lic_list )
50
58
except Exception as ex :
51
59
logger .warning ("Error parsing " + file + ":" + str (ex ))
52
60
53
61
success_to_write , writing_msg = write_excel_and_csv (excel_name , sheet_list )
54
62
logger .info ("Writing excel :" + str (success_to_write ) + " " + writing_msg )
55
63
if success_to_write :
56
- _result_log ["FOSSLight Report" ] = excel_name + ".xlsx"
64
+ result_log ["FOSSLight Report" ] = excel_name + ".xlsx"
57
65
58
66
except Exception as ex :
59
67
success = False
60
68
logger .warning (str (ex ))
61
69
62
70
scan_result_msg = str (success ) if msg == "" else str (success ) + "," + msg
63
- _result_log ["Scan Result" ] = scan_result_msg
71
+ result_log ["Scan Result" ] = scan_result_msg
64
72
65
73
try :
66
- _str_final_result_log = yaml .safe_dump (_result_log , allow_unicode = True , sort_keys = True )
74
+ _str_final_result_log = yaml .safe_dump (result_log , allow_unicode = True , sort_keys = True )
67
75
logger .info (_str_final_result_log )
68
76
except Exception as ex :
69
77
logger .warning ("Failed to print result log.: " + str (ex ))
70
78
71
79
return file_list
72
80
73
81
74
- def get_detected_licenses_from_scancode (scancode_json_file ):
82
+ def get_detected_licenses_from_scancode (scancode_json_file , need_license ):
75
83
file_list = []
84
+ license_list = {}
76
85
try :
77
86
logger .info ("Start parsing " + scancode_json_file )
78
87
with open (scancode_json_file , "r" ) as st_json :
79
88
st_python = json .load (st_json )
80
89
has_error , str_error = get_error_from_header (st_python ["headers" ])
81
- rc , file_list , msg = parsing_file_item (st_python ["files" ], has_error )
90
+ rc , file_list , msg , license_list = parsing_file_item (st_python ["files" ], has_error , need_license )
82
91
logger .info ("|---" + msg )
83
92
if has_error :
84
93
logger .info ("|---Scan error:" + str_error )
85
94
except Exception as error :
86
95
logger .warning ("Parsing " + scancode_json_file + ":" + str (error ))
87
96
logger .info ("|---Number of files detected: " + str (len (file_list )))
88
- return file_list
97
+ return file_list , license_list
89
98
90
99
91
100
def main ():
@@ -95,16 +104,19 @@ def main():
95
104
path_to_find_bin = os .getcwd ()
96
105
start_time = datetime .now ().strftime ('%Y-%m-%d_%H-%M-%S' )
97
106
output_file_name = ""
107
+ print_matched_text = False
98
108
99
109
try :
100
- opts , args = getopt .getopt (argv , 'hp :o:' )
110
+ opts , args = getopt .getopt (argv , 'hmp :o:' )
101
111
for opt , arg in opts :
102
112
if opt == "-h" :
103
113
print_help_msg_convert ()
104
114
elif opt == "-p" :
105
115
path_to_find_bin = arg
106
116
elif opt == "-o" :
107
117
output_file_name = arg
118
+ elif opt == "-m" :
119
+ print_matched_text = True
108
120
except Exception :
109
121
print_help_msg_convert ()
110
122
@@ -117,7 +129,7 @@ def main():
117
129
118
130
logger , _result_log = init_log (os .path .join (output_dir , "fosslight_src_log_" + start_time + ".txt" ),
119
131
True , logging .INFO , logging .DEBUG , _PKG_NAME )
120
- convert_json_to_excel (path_to_find_bin , oss_report_name , _result_log )
132
+ convert_json_to_excel (path_to_find_bin , oss_report_name , _result_log , print_matched_text )
121
133
122
134
123
135
if __name__ == '__main__' :
0 commit comments