55import copy
66import re
77import shutil
8- import subprocess
8+ from subprocess import ( # nosec
9+ STDOUT ,
10+ CalledProcessError ,
11+ Popen ,
12+ PIPE ,
13+ check_output ,
14+ )
915from ast import Str
1016from os import F_OK , PathLike , access , mkdir
1117from xmlrpc .client import Boolean
@@ -70,7 +76,7 @@ def descriptor_to_androguard_format(descriptor):
7076 return new_descriptor
7177
7278
73- def execute_command (command , stderr = subprocess . PIPE , cwd = None ):
79+ def _execute_command (command , stderr = PIPE , cwd = None ):
7480 """
7581 Execute a given command and yield the messages from the standard output.
7682
@@ -81,10 +87,10 @@ def execute_command(command, stderr=subprocess.PIPE, cwd=None):
8187 non-zero return code
8288 :yield: a string holding a line of message in the standard output
8389 """
84- process = subprocess . Popen (
90+ process = Popen ( # nosec
8591 command ,
8692 bufsize = 1 ,
87- stdout = subprocess . PIPE ,
93+ stdout = PIPE ,
8894 stderr = stderr ,
8995 universal_newlines = True ,
9096 cwd = cwd ,
@@ -109,29 +115,26 @@ def execute_command(command, stderr=subprocess.PIPE, cwd=None):
109115
110116 if return_code :
111117 error_messages = ""
112- if stderr == subprocess . PIPE :
118+ if stderr == PIPE :
113119 for message in process .stderr .readlines ():
114120 error_messages = error_messages + message
115121
116- raise subprocess .CalledProcessError (
117- return_code , command , stderr = error_messages
118- )
122+ raise CalledProcessError (return_code , command , stderr = error_messages )
119123
120- if stderr == subprocess . PIPE :
124+ if stderr == PIPE :
121125 process .stderr .close ()
122126
123127
124- def get_rizin_version (rizin_path ) -> Str :
128+ def _get_rizin_version (rizin_path ) -> Str :
125129 """
126130 Get the version number of the Rizin instance in the path.
127131
128132 :param rizin_path: a path to the Rizin executable
129133 :return: the version number of the Rizin instance
130134 """
131135 try :
132- result = subprocess .check_output (
133- [rizin_path , "-v" ], timeout = 5 , check = True , stdout = subprocess .PIPE
134- )
136+ result = check_output ([rizin_path , "-v" ], timeout = 5 ) # nosec
137+ result = str (result )
135138
136139 matched_versions = re .finditer (
137140 r"[0-9]+\.[0-9]+\.[0-9]+" , result [: result .index ("@" )]
@@ -143,7 +146,7 @@ def get_rizin_version(rizin_path) -> Str:
143146 else :
144147 return None
145148
146- except subprocess . CalledProcessError :
149+ except CalledProcessError :
147150 return None
148151
149152 except OSError :
@@ -166,21 +169,21 @@ def download_rizin(target_path) -> Boolean:
166169 try :
167170 print ()
168171
169- for line in execute_command (
172+ for line in _execute_command (
170173 [
171174 "git" ,
172175 "clone" ,
173176 "--progress" ,
174177 "https://github.com/rizinorg/rizin" ,
175178 target_path ,
176179 ],
177- stderr = subprocess . STDOUT ,
180+ stderr = STDOUT ,
178181 ):
179182 print_info (line )
180183
181184 return True
182185
183- except subprocess . CalledProcessError :
186+ except CalledProcessError :
184187 print_error ("An error occurred when downloading Rizin.\n " )
185188
186189 except OSError :
@@ -205,48 +208,54 @@ def update_rizin(source_path, target_commit) -> Boolean:
205208 print ()
206209
207210 # Checkout to target commit
208- for line in execute_command (
211+ for line in _execute_command (
209212 ["git" , "checkout" , target_commit ], cwd = source_path
210213 ):
211214 print_info (line )
212215
213216 # Remove the last build
214- for line in execute_command (["rm" , "-rf" , "build" ], cwd = source_path ):
217+ for line in _execute_command (["rm" , "-rf" , "build" ], cwd = source_path ):
215218 print_info (line )
216219
217220 # Clean out old subproject
218- for line in execute_command (
221+ for line in _execute_command (
219222 ["git" , "clean" , "-dxff" , "subprojects/" ], cwd = source_path
220223 ):
221224 print_info (line )
222225
223- except subprocess . CalledProcessError as error :
226+ except CalledProcessError as error :
224227 print_error ("An error occurred when updating Rizin.\n " )
225228
226229 for line in error .stderr .decode ().splitlines ():
227230 print_error (line )
228231
229232 return False
230233
234+ except OSError as error :
235+ print_error ("An error occurred when updating Rizin.\n " )
236+ print_error (error )
237+
238+ return False
239+
231240 # Compile Rizin
232241 try :
233242 print ()
234243
235244 # Configure
236- for line in execute_command (
245+ for line in _execute_command (
237246 ["meson" , "--buildtype=release" , "build" ], cwd = source_path
238247 ):
239248 print_info (line )
240249
241250 # Compile the source code
242- for line in execute_command (
251+ for line in _execute_command (
243252 ["meson" , "compile" , "-C" , "build" ], cwd = source_path
244253 ):
245254 print_info (line )
246255
247256 return True
248257
249- except subprocess . CalledProcessError as error :
258+ except CalledProcessError as error :
250259 pass
251260 except OSError :
252261 pass
@@ -286,13 +295,13 @@ def find_rizin_instance(
286295 # Search Rizin in PATH
287296 which_result = shutil .which ("rizin" )
288297 if which_result :
289- version = get_rizin_version (which_result )
298+ version = _get_rizin_version (which_result )
290299 if version in COMPATIBLE_RAZIN_VERSIONS :
291300 return which_result
292301
293302 # Otherwise, search the home path
294303 rizin_executable_path = rizin_source_path + "build/binrz/rizin/rizin"
295- current_version = get_rizin_version (rizin_executable_path )
304+ current_version = _get_rizin_version (rizin_executable_path )
296305
297306 if not current_version and not disable_rizin_installation :
298307 print_info ("Cannot find a compatible Rizin instance." )
0 commit comments