@@ -76,38 +76,51 @@ def main(args):
7676 output_cls = JSONOutput
7777
7878 with output_cls () as check_output :
79- # we need to consume the iterator once to get the total
80- # for the progress bar
81- check_output .enumerating_tasks_start ()
82- count = sum (1 for i in walk_lief_parsable_list (filepath_list , recursive ))
83- check_output .enumerating_tasks_stop (count )
84- with ProcessPoolExecutor (max_workers = workers ) as pool :
85- check_output .processing_tasks_start ()
86- future_to_checksec = {
87- pool .submit (checksec_file , filepath ): filepath
88- for filepath in walk_lief_parsable_list (filepath_list , recursive )
89- }
90- for future in as_completed (future_to_checksec ):
91- filepath = future_to_checksec [future ]
79+ try :
80+ # we need to consume the iterator once to get the total
81+ # for the progress bar
82+ check_output .enumerating_tasks_start ()
83+ count = sum (1 for i in walk_lief_parsable_list (filepath_list , recursive ))
84+ check_output .enumerating_tasks_stop (count )
85+ with ProcessPoolExecutor (max_workers = workers ) as pool :
9286 try :
93- data = future .result ()
94- except FileNotFoundError :
95- logging .debug ("%s does not exist" , filepath )
96- except ErrorParsingFailed :
97- logging .debug ("%s LIEF parsing failed" )
98- except NotImplementedError :
99- logging .debug ("%s: Not an ELF/PE. Skipping" , filepath )
100- else :
101- check_output .add_checksec_result (filepath , data )
102- finally :
103- check_output .checksec_result_end ()
104-
105- check_output .print ()
87+ check_output .processing_tasks_start ()
88+ future_to_checksec = {
89+ pool .submit (checksec_file , filepath ): filepath
90+ for filepath in walk_lief_parsable_list (filepath_list , recursive )
91+ }
92+ for future in as_completed (future_to_checksec ):
93+ filepath = future_to_checksec [future ]
94+ try :
95+ data = future .result ()
96+ except FileNotFoundError :
97+ logging .debug ("%s does not exist" , filepath )
98+ except ErrorParsingFailed :
99+ logging .debug ("%s LIEF parsing failed" )
100+ except NotImplementedError :
101+ logging .debug ("%s: Not an ELF/PE. Skipping" , filepath )
102+ else :
103+ check_output .add_checksec_result (filepath , data )
104+ finally :
105+ check_output .checksec_result_end ()
106+ except KeyboardInterrupt :
107+ # remove progress bars before waiting for ProcessPoolExecutor to shutdown
108+ check_output .__exit__ (None , None , None )
109+ logging .info ("Shutdown Process Pool ..." )
110+ pool .shutdown (wait = True )
111+ raise
112+ except KeyboardInterrupt :
113+ pass
114+ else :
115+ check_output .print ()
106116
107117
108118def entrypoint ():
109119 args = docopt (__doc__ )
110- main (args )
120+ try :
121+ main (args )
122+ except KeyboardInterrupt :
123+ pass
111124
112125
113126if __name__ == "__main__" :
0 commit comments