1010import re
1111import json
1212import logging
13+ import zipfile
14+ import shutil
1315# Parsing NOTICE
1416from bs4 import BeautifulSoup
1517import subprocess
@@ -336,30 +338,34 @@ def get_result_of_notice_html(found_on_html, notice_file_found):
336338 return "nok"
337339
338340
339- def find_notice_value ():
341+ def find_notice_value (notice_zip_dest_file = "" ):
340342 global notice_file_list , final_bin_info
341- str_notice_files = ""
343+ notice_file_comment = "Notice file not found. "
342344
343345 try :
344346 notice_file_list , notice_files = read_notice_file (os .path .abspath (build_out_notice_file_path ), NOTICE_HTML_FILE_NAME )
345- if not notice_file_list :
346- logger .info (f"Notice file is empty:{ notice_files } " )
347- return
348- if notice_files :
349- for notice_file in notice_files :
350- if "NOTICE.txt" in notice_file :
351- logger .debug (f"NOTICE.txt: { notice_file } " )
352- notice_files .remove (notice_file )
353- str_notice_files = "," .join (notice_files )
354- logger .info (f"Notice files:{ str_notice_files } " )
355- else :
356- logger .debug ("Can't find a notice file" )
357- return_list = do_multi_process (find_notice_html , final_bin_info )
358- final_bin_info = return_list [:]
359-
360- except IOError as error : # 'CANNOT_FIND_NOTICE_HTML'
347+ if notice_file_list :
348+ if notice_files :
349+ for notice_file in notice_files :
350+ if "NOTICE.txt" in notice_file :
351+ logger .debug (f"NOTICE.txt: { notice_file } " )
352+ notice_files .remove (notice_file )
353+ str_notice_files = "," .join (notice_files )
354+ logger .debug (f"Notice files:{ str_notice_files } " )
355+ if notice_zip_dest_file :
356+ final_notice_zip = create_and_copy_notice_zip (notice_files , notice_zip_dest_file )
357+ if final_notice_zip :
358+ notice_file_comment = f"Notice file to upload FOSSLight Hub: { final_notice_zip } "
359+ else :
360+ notice_file_comment = "Failed to compress the Notice file."
361+ else :
362+ logger .debug ("Can't find a notice file to read." )
363+ return_list = do_multi_process (find_notice_html , final_bin_info )
364+ final_bin_info = return_list [:]
365+ except Exception as error :
361366 logger .debug (f"find_notice_value:{ error } " )
362- return str_notice_files
367+ logger .info (notice_file_comment )
368+ return notice_file_comment
363369
364370
365371def find_notice_html (bin_info , return_list ):
@@ -767,6 +773,24 @@ def find_meta_lic_files():
767773 if lic_list :
768774 lic = ',' .join (lic_list )
769775 meta_lic_files [key ] = lic
776+
777+
778+ def create_and_copy_notice_zip (notice_files_list , zip_file_path ):
779+ final_destination_file_name = ""
780+
781+ if len (notice_files_list ) == 1 :
782+ single_file_path = notice_files_list [0 ]
783+ destination_path = os .path .join (os .path .dirname (zip_file_path ), os .path .basename (single_file_path ))
784+ shutil .copy (single_file_path , destination_path )
785+ final_destination_file_name = destination_path
786+ logger .debug (f"Notice file is copied to '{ destination_path } '." )
787+ else :
788+ with zipfile .ZipFile (zip_file_path , 'w' ) as zipf :
789+ for single_file_path in notice_files_list :
790+ zipf .write (single_file_path , arcname = os .path .basename (single_file_path ))
791+ final_destination_file_name = zip_file_path
792+
793+ return final_destination_file_name
770794
771795
772796def main ():
@@ -790,6 +814,7 @@ def main():
790814 now = datetime .now ().strftime ('%y%m%d_%H%M' )
791815 log_txt_file = os .path .join (python_script_dir , f"fosslight_log_android_{ now } .txt" )
792816 result_excel_file_name = os .path .join (python_script_dir , f"fosslight_report_android_{ now } " )
817+ result_notice_zip_file_name = os .path .join (python_script_dir , f"notice_to_fosslight-hub_{ now } .zip" )
793818 remove_list_file = ""
794819
795820 parser = argparse .ArgumentParser (description = 'FOSSLight Android' , prog = 'fosslight_android' , add_help = False )
@@ -861,7 +886,7 @@ def main():
861886
862887 map_binary_module_name_and_path (find_binaries_from_out_dir ())
863888
864- notice_files = find_notice_value ()
889+ notice_result_comment = find_notice_value (result_notice_zip_file_name )
865890 find_bin_license_info ()
866891
867892 set_mk_file_path () # Mk file path and local path, location of NOTICE file, can be different
@@ -879,7 +904,8 @@ def main():
879904 scan_item .set_cover_pathinfo (android_src_path , "" )
880905
881906 scan_item .set_cover_comment (f"Total number of binaries: { len (final_bin_info )} " )
882- scan_item .set_cover_comment (f"\n Notice: { notice_files } " )
907+ scan_item .set_cover_comment (notice_result_comment )
908+
883909 scan_item .append_file_items (final_bin_info , PKG_NAME )
884910 success , msg , result_file = write_output_file (result_excel_file_name , RESULT_FILE_EXTENSION ,
885911 scan_item , HEADER , HIDDEN_HEADER )
0 commit comments