44import revengai
55from binaryninja import log_info , log_error , BinaryView
66from requests .exceptions import RequestException
7- from PySide6 .QtCore import QObject , Signal
7+ from PySide6 .QtCore import QObject
88from reai_toolkit .utils .core .sync import AnalysisSyncService
99
10+ _IN_PROGRESS_STATUSES = (
11+ revengai .StatusInput .UPLOADED ,
12+ revengai .StatusInput .QUEUED ,
13+ revengai .StatusInput .PROCESSING ,
14+ )
15+
16+
1017class PeriodicChecker (QObject ):
11- update_text_signal = Signal (object , str )
1218 sync_service : AnalysisSyncService
13-
19+
1420 def __init__ (self , config ):
1521 super ().__init__ ()
1622 self ._current_timer : Optional [Timer ] = None
1723 self .number_of_clicks = 0
18- self .update_text_signal .connect (self ._update_text_slot )
1924 self .config = config
2025 self .sync_service = AnalysisSyncService (config )
2126
22- def _update_text_slot (self , callback , text ):
23- """Slot that runs in the main thread to safely update UI"""
24- try :
25- if hasattr (callback , '__call__' ):
26- if hasattr (self , '_current_editor' ):
27- callback (self ._current_editor , text )
28- except Exception as ex :
29- log_error (f"RevEng.AI | Error updating UI: { str (ex )} " )
30-
3127 def stop (self ):
3228 if self ._current_timer :
3329 self ._current_timer .cancel ()
3430 self ._current_timer = None
3531 log_info ("RevEng.AI | Stopped periodic status check" )
36-
3732
38- def start_checking (self , binary_view : BinaryView , analysis_id : int , binary_id : int , callback , interval : float = 60 ) -> None :
33+ def start_checking (
34+ self ,
35+ binary_view : BinaryView ,
36+ analysis_id : int ,
37+ binary_id : int ,
38+ callback ,
39+ interval : float = 60 ,
40+ ) -> None :
3941 def _worker (bv : BinaryView , bid : int , aid : int ):
4042 try :
4143 with self .config .create_api_client () as api_client :
4244 api_instance = revengai .AnalysesCoreApi (api_client )
43- api_response = api_instance .get_analysis_status (aid )
45+ api_response = api_instance .get_analysis_status (aid )
4446 status = api_response .data .analysis_status
45- log_info (f"RevEng.AI | Current status for analysis [Binary ID: { bid } ] [Analysis ID: { aid } ]: { status } " )
47+ log_info (
48+ f"RevEng.AI | Current status for analysis [Binary ID: { bid } ] [Analysis ID: { aid } ]: { status } "
49+ )
4650
47- if status in ( "Queued" , "Processing" ) :
51+ if status in _IN_PROGRESS_STATUSES :
4852 if bv and bv .file and bv .file .filename :
4953 self ._current_timer = Timer (
50- interval ,
51- _worker ,
52- args = (bv , bid , aid )
54+ interval , _worker , args = (bv , bid , aid )
5355 )
5456 self ._current_timer .start ()
5557 log_info (
5658 f"RevEng.AI | Scheduled next status check for: { basename (bv .file .filename )} [Binary ID: { bid } ] [Analysis ID: { aid } ]"
5759 )
58- else :
59-
60+ elif status == revengai .StatusInput .COMPLETE :
6061 # Analysis is complete, fetch model_id and invoke callback
6162 with self .config .create_api_client () as api_client :
6263 api_instance = revengai .AnalysesCoreApi (api_client )
63- analysis_details : revengai .BaseResponseBasic = api_instance .get_analysis_basic_info (
64- analysis_id = analysis_id
64+ analysis_details : revengai .BaseResponseBasic = (
65+ api_instance .get_analysis_basic_info (
66+ analysis_id = analysis_id
67+ )
6568 )
6669 model_id = analysis_details .data .model_id
6770 callback (bid , aid , model_id )
6871
69- bv = self .sync_service .sync_analysis_data (analysis_id = aid , bv = bv )
72+ bv = self .sync_service .sync_analysis_data (
73+ analysis_id = aid , bv = bv
74+ )
7075
71- log_info (f"RevEng.AI | Analysis completed with status: { status } for Binary ID: { bid } | Analysis ID: { aid } | Model ID: { model_id } " )
76+ log_info (
77+ f"RevEng.AI | Analysis completed with status: { status } for Binary ID: { bid } | Analysis ID: { aid } | Model ID: { model_id } "
78+ )
79+ else :
80+ log_error (
81+ f"RevEng.AI | Analysis failed with status '{ status } ' "
82+ f"[Binary ID: { bid } ] [Analysis ID: { aid } ]. "
83+ "Check the analysis log in the RevEng.AI portal, then re-run the analysis."
84+ )
7285 except RequestException as ex :
73- log_error (f"RevEng.AI | Error getting binary analysis status: { str (ex )} " )
86+ log_error (
87+ f"RevEng.AI | Network error while monitoring analysis [Binary ID: { bid } ] [Analysis ID: { aid } ]: { ex } "
88+ )
7489 except Exception as ex :
75- log_error (f"RevEng.AI | Unexpected error during status check: { str (ex )} " )
90+ log_error (
91+ f"RevEng.AI | Unexpected error while monitoring analysis [Binary ID: { bid } ] [Analysis ID: { aid } ]: { ex } "
92+ )
7693
7794 self .stop ()
7895
79- self ._current_timer = Timer (30 , _worker , args = (binary_view , binary_id , analysis_id ))
96+ self ._current_timer = Timer (
97+ 30 , _worker , args = (binary_view , binary_id , analysis_id )
98+ )
8099 self ._current_timer .start ()
81100 log_info (
82101 f"RevEng.AI | Started periodic status check for: { basename (binary_view .file .filename )} [Binary ID: { binary_id } ] [Analysis ID: { analysis_id } ]"
83- )
102+ )
0 commit comments