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