5
5
import copy
6
6
import re
7
7
import shutil
8
- import subprocess
8
+ from subprocess import ( # nosec
9
+ STDOUT ,
10
+ CalledProcessError ,
11
+ Popen ,
12
+ PIPE ,
13
+ check_output ,
14
+ )
9
15
from ast import Str
10
16
from os import F_OK , PathLike , access , mkdir
11
17
from xmlrpc .client import Boolean
@@ -70,7 +76,7 @@ def descriptor_to_androguard_format(descriptor):
70
76
return new_descriptor
71
77
72
78
73
- def execute_command (command , stderr = subprocess . PIPE , cwd = None ):
79
+ def _execute_command (command , stderr = PIPE , cwd = None ):
74
80
"""
75
81
Execute a given command and yield the messages from the standard output.
76
82
@@ -81,10 +87,10 @@ def execute_command(command, stderr=subprocess.PIPE, cwd=None):
81
87
non-zero return code
82
88
:yield: a string holding a line of message in the standard output
83
89
"""
84
- process = subprocess . Popen (
90
+ process = Popen ( # nosec
85
91
command ,
86
92
bufsize = 1 ,
87
- stdout = subprocess . PIPE ,
93
+ stdout = PIPE ,
88
94
stderr = stderr ,
89
95
universal_newlines = True ,
90
96
cwd = cwd ,
@@ -109,29 +115,26 @@ def execute_command(command, stderr=subprocess.PIPE, cwd=None):
109
115
110
116
if return_code :
111
117
error_messages = ""
112
- if stderr == subprocess . PIPE :
118
+ if stderr == PIPE :
113
119
for message in process .stderr .readlines ():
114
120
error_messages = error_messages + message
115
121
116
- raise subprocess .CalledProcessError (
117
- return_code , command , stderr = error_messages
118
- )
122
+ raise CalledProcessError (return_code , command , stderr = error_messages )
119
123
120
- if stderr == subprocess . PIPE :
124
+ if stderr == PIPE :
121
125
process .stderr .close ()
122
126
123
127
124
- def get_rizin_version (rizin_path ) -> Str :
128
+ def _get_rizin_version (rizin_path ) -> Str :
125
129
"""
126
130
Get the version number of the Rizin instance in the path.
127
131
128
132
:param rizin_path: a path to the Rizin executable
129
133
:return: the version number of the Rizin instance
130
134
"""
131
135
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 )
135
138
136
139
matched_versions = re .finditer (
137
140
r"[0-9]+\.[0-9]+\.[0-9]+" , result [: result .index ("@" )]
@@ -143,7 +146,7 @@ def get_rizin_version(rizin_path) -> Str:
143
146
else :
144
147
return None
145
148
146
- except subprocess . CalledProcessError :
149
+ except CalledProcessError :
147
150
return None
148
151
149
152
except OSError :
@@ -166,21 +169,21 @@ def download_rizin(target_path) -> Boolean:
166
169
try :
167
170
print ()
168
171
169
- for line in execute_command (
172
+ for line in _execute_command (
170
173
[
171
174
"git" ,
172
175
"clone" ,
173
176
"--progress" ,
174
177
"https://github.com/rizinorg/rizin" ,
175
178
target_path ,
176
179
],
177
- stderr = subprocess . STDOUT ,
180
+ stderr = STDOUT ,
178
181
):
179
182
print_info (line )
180
183
181
184
return True
182
185
183
- except subprocess . CalledProcessError :
186
+ except CalledProcessError :
184
187
print_error ("An error occurred when downloading Rizin.\n " )
185
188
186
189
except OSError :
@@ -205,48 +208,54 @@ def update_rizin(source_path, target_commit) -> Boolean:
205
208
print ()
206
209
207
210
# Checkout to target commit
208
- for line in execute_command (
211
+ for line in _execute_command (
209
212
["git" , "checkout" , target_commit ], cwd = source_path
210
213
):
211
214
print_info (line )
212
215
213
216
# 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 ):
215
218
print_info (line )
216
219
217
220
# Clean out old subproject
218
- for line in execute_command (
221
+ for line in _execute_command (
219
222
["git" , "clean" , "-dxff" , "subprojects/" ], cwd = source_path
220
223
):
221
224
print_info (line )
222
225
223
- except subprocess . CalledProcessError as error :
226
+ except CalledProcessError as error :
224
227
print_error ("An error occurred when updating Rizin.\n " )
225
228
226
229
for line in error .stderr .decode ().splitlines ():
227
230
print_error (line )
228
231
229
232
return False
230
233
234
+ except OSError as error :
235
+ print_error ("An error occurred when updating Rizin.\n " )
236
+ print_error (error )
237
+
238
+ return False
239
+
231
240
# Compile Rizin
232
241
try :
233
242
print ()
234
243
235
244
# Configure
236
- for line in execute_command (
245
+ for line in _execute_command (
237
246
["meson" , "--buildtype=release" , "build" ], cwd = source_path
238
247
):
239
248
print_info (line )
240
249
241
250
# Compile the source code
242
- for line in execute_command (
251
+ for line in _execute_command (
243
252
["meson" , "compile" , "-C" , "build" ], cwd = source_path
244
253
):
245
254
print_info (line )
246
255
247
256
return True
248
257
249
- except subprocess . CalledProcessError as error :
258
+ except CalledProcessError as error :
250
259
pass
251
260
except OSError :
252
261
pass
@@ -286,13 +295,13 @@ def find_rizin_instance(
286
295
# Search Rizin in PATH
287
296
which_result = shutil .which ("rizin" )
288
297
if which_result :
289
- version = get_rizin_version (which_result )
298
+ version = _get_rizin_version (which_result )
290
299
if version in COMPATIBLE_RAZIN_VERSIONS :
291
300
return which_result
292
301
293
302
# Otherwise, search the home path
294
303
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 )
296
305
297
306
if not current_version and not disable_rizin_installation :
298
307
print_info ("Cannot find a compatible Rizin instance." )
0 commit comments