-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstance_handler.py
More file actions
397 lines (323 loc) · 14.9 KB
/
instance_handler.py
File metadata and controls
397 lines (323 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""Operations related to BlueStacks instances."""
import os
import glob
import logging
import psutil
from typing import List, Optional
import constants
logger = logging.getLogger(__name__)
def modify_instance_files(instance_path: str, new_mode: str) -> None:
"""
Modifies the 'Type' attribute in .bstk files within an instance
directory to toggle R/W status for specific virtual disk files.
Args:
instance_path: Path to the BlueStacks instance directory
(e.g., "C:\\ProgramData\\BlueStacks_nxt\\Nougat64_1").
new_mode: The new mode string (constants.MODE_READWRITE or constants.MODE_READONLY).
Raises:
FileNotFoundError: If the instance path or essential .bstk files are missing.
ValueError: If new_mode is not a valid mode constant.
IOError: If there are problems reading or writing files.
Exception: For other unexpected errors.
"""
logger.info(f"Modifying R/W mode to '{new_mode}' for instance at: {instance_path}")
if new_mode not in [constants.MODE_READONLY, constants.MODE_READWRITE]:
raise ValueError(
f"Invalid new_mode specified: {new_mode}. Must be '{constants.MODE_READONLY}' or '{constants.MODE_READWRITE}'."
)
if not os.path.isdir(instance_path):
raise FileNotFoundError(f"Instance directory not found: {instance_path}")
bstk_files_to_process: List[str] = []
android_bstk_in_path = os.path.join(instance_path, constants.ANDROID_BSTK_IN_FILE)
if os.path.exists(android_bstk_in_path):
bstk_files_to_process.append(android_bstk_in_path)
else:
logger.warning(
f"'{constants.ANDROID_BSTK_IN_FILE}' not found in {instance_path}. R/W toggle might be incomplete."
)
try:
instance_bstk_files = glob.glob(
os.path.join(instance_path, constants.BSTK_FILE_PATTERN)
)
instance_bstk_files = [
f
for f in instance_bstk_files
if os.path.basename(f) != constants.ANDROID_BSTK_IN_FILE
]
bstk_files_to_process.extend(instance_bstk_files)
except Exception as e:
logger.error(f"Error finding .bstk files in {instance_path}: {e}")
raise IOError(f"Error finding .bstk files in {instance_path}") from e
if not bstk_files_to_process:
if not os.path.exists(android_bstk_in_path):
raise FileNotFoundError(
f"No .bstk files (including {constants.ANDROID_BSTK_IN_FILE}) found to modify in {instance_path}"
)
else:
logger.warning(
f"Only {constants.ANDROID_BSTK_IN_FILE} found in {instance_path}. Proceeding with modification."
)
logger.debug(f"Found .bstk files to process: {bstk_files_to_process}")
type_pattern = constants.REGEX_BSTK_TYPE_PATTERN
for bstk_file_path in bstk_files_to_process:
logger.debug(f"Processing file: {bstk_file_path}")
try:
with open(bstk_file_path, "r", encoding="utf-8") as f:
content = f.readlines()
updated_content = []
file_changed = False
for line in content:
modified_line = line
if any(
target_file in line for target_file in constants.FILES_TO_MODIFY_RW
):
match = type_pattern.search(line)
if match:
old_mode = match.group(2)
if old_mode.lower() != new_mode.lower():
modified_line = type_pattern.sub(
r"\1" + new_mode + r"\3", line
)
logger.info(
f" In '{os.path.basename(bstk_file_path)}': Changed mode from '{old_mode}' to '{new_mode}' for line containing target file."
)
file_changed = True
else:
logger.debug(
f" In '{os.path.basename(bstk_file_path)}': Mode already '{new_mode}' for line containing target file. No change needed."
)
else:
logger.warning(
f" In '{os.path.basename(bstk_file_path)}': Line contains target file but no standard 'Type=\"{constants.MODE_READONLY}|{constants.MODE_READWRITE}\"' attribute found. Line: {line.strip()}"
)
updated_content.append(modified_line)
if file_changed:
logger.debug(f"Writing changes back to {bstk_file_path}")
with open(bstk_file_path, "w", encoding="utf-8") as f:
f.writelines(updated_content)
else:
logger.debug(f"No changes needed for {bstk_file_path}")
except FileNotFoundError:
logger.error(f"File disappeared during processing: {bstk_file_path}")
raise
except IOError:
logger.exception(f"I/O error processing file {bstk_file_path}")
raise
except Exception:
logger.exception(f"Unexpected error processing file {bstk_file_path}")
raise
def is_instance_readonly(instance_path: str) -> Optional[bool]:
"""
Checks if any relevant disk file (.vdi, .vhd) is explicitly set to 'Readonly'
in the instance's .bstk files.
Args:
instance_path: Path to the BlueStacks instance directory.
Returns:
True if any relevant file is explicitly marked 'Readonly'.
False if relevant files are found but none are marked 'Readonly' (implies Writable/Normal).
None if the path doesn't exist, no relevant .bstk files are found,
or an error occurs during reading, indicating an indeterminate state.
"""
logger.debug(f"Checking readonly status for instance at: {instance_path}")
if not os.path.isdir(instance_path):
logger.warning(
f"Instance directory not found for readonly check: {instance_path}"
)
return None
bstk_files_to_check: List[str] = []
android_bstk_in_path = os.path.join(instance_path, constants.ANDROID_BSTK_IN_FILE)
if os.path.exists(android_bstk_in_path):
bstk_files_to_check.append(android_bstk_in_path)
try:
instance_bstk_files = glob.glob(
os.path.join(instance_path, constants.BSTK_FILE_PATTERN)
)
instance_bstk_files = [
f
for f in instance_bstk_files
if os.path.basename(f) != constants.ANDROID_BSTK_IN_FILE
]
bstk_files_to_check.extend(instance_bstk_files)
except Exception as e:
logger.error(
f"Error finding .bstk files in {instance_path} for readonly check: {e}"
)
return None
if not bstk_files_to_check:
logger.warning(
f"No .bstk files found to check readonly status in {instance_path}"
)
return None
logger.debug(f"Checking readonly status in files: {bstk_files_to_check}")
readonly_pattern = constants.REGEX_BSTK_READONLY_PATTERN
found_relevant_line = False
for file_path in bstk_files_to_check:
logger.debug(f"Scanning file for readonly check: {file_path}")
try:
with open(file_path, "r", encoding="utf-8") as f:
for line_num, line in enumerate(f, 1):
if any(
target_file in line
for target_file in constants.FILES_TO_MODIFY_RW
):
found_relevant_line = True
if readonly_pattern.search(line):
logger.debug(
f"Readonly status found in {os.path.basename(file_path)} (Line {line_num}) for a target disk file."
)
return True
except FileNotFoundError:
logger.warning(
f"File not found during readonly check (maybe deleted concurrently?): {file_path}. Skipping."
)
continue
except Exception:
logger.exception(f"Error reading file {file_path} during readonly check.")
return None
if found_relevant_line:
logger.debug(
f"Relevant disk file lines found in instance {instance_path}, but none were explicitly 'Type=\"{constants.MODE_READONLY}\"'. Assuming Writable."
)
return False
else:
logger.warning(
f"No configuration lines found for target disk files ({constants.FILES_TO_MODIFY_RW}) in instance {instance_path}. Cannot determine R/W status."
)
return None
def is_bluestacks_running() -> bool:
"""
Checks if any known BlueStacks processes are running.
Returns:
True if at least one known BlueStacks process is found, False otherwise.
"""
logger.debug("Checking for running BlueStacks processes...")
for proc in psutil.process_iter(["name"]):
try:
proc_name = proc.info["name"]
if proc_name in constants.BLUESTACKS_PROCESS_NAMES:
logger.info(
f"Detected running BlueStacks process: {proc_name} (PID: {proc.pid})"
)
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as e:
logger.debug(f"Skipping process check for PID {proc.pid}: {e}")
continue
except Exception as e:
logger.exception(f"Unexpected error checking process {proc.pid}: {e}")
continue
logger.debug("No running BlueStacks processes found matching the known list.")
return False
def terminate_bluestacks() -> bool:
"""
Attempts to terminate all known BlueStacks-related processes gracefully,
then forcefully kills any that remain after a timeout.
Returns:
True if termination was attempted (i.e., processes were found), False otherwise.
Note: This does not guarantee all processes were successfully terminated (e.g., due to permissions).
"""
terminated_any = False
logger.info("Attempting to terminate BlueStacks processes...")
processes_found: List[psutil.Process] = []
for proc in psutil.process_iter(["pid", "name"]):
try:
proc_name = proc.info.get("name")
if proc_name in constants.BLUESTACKS_PROCESS_NAMES:
processes_found.append(proc)
logger.debug(f"Found BlueStacks process: {proc_name} (PID: {proc.pid})")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
except Exception as e:
logger.warning(
f"Error accessing process info during discovery: {e} (PID: {proc.pid})"
)
if not processes_found:
logger.info("No running BlueStacks processes found to terminate.")
return False
terminated_names = []
logger.info(f"Sending terminate signal to {len(processes_found)} processes...")
for proc in processes_found:
try:
proc_name = proc.name()
proc_pid = proc.pid
logger.info(f"Terminating {proc_name} (PID: {proc_pid})...")
proc.terminate()
terminated_any = True
terminated_names.append(f"{proc_name}({proc_pid})")
except psutil.NoSuchProcess:
logger.warning(
f"Process {proc.name() if 'proc' in locals() and proc.is_running() else 'unknown'} (PID: {proc.pid if 'proc' in locals() else 'unknown'}) disappeared before termination signal."
)
except psutil.AccessDenied:
logger.error(
f"Permission denied trying to terminate {proc.name()} (PID: {proc.pid}). Try running as administrator."
)
terminated_any = True
terminated_names.append(f"{proc.name()}({proc.pid}) - FAILED (Permission)")
except Exception as e:
proc_id_str = (
f"{proc.name()}({proc.pid})"
if "proc" in locals() and proc.is_running()
else f"PID {proc.pid if 'proc' in locals() else 'unknown'}"
)
logger.exception(f"Error sending terminate signal to {proc_id_str}: {e}")
terminated_any = True
terminated_names.append(f"{proc_id_str} - FAILED (Error)")
if terminated_any:
logger.info(
f"Waiting up to {constants.PROCESS_KILL_TIMEOUT_S}s for processes to exit gracefully..."
)
try:
gone, alive = psutil.wait_procs(
processes_found, timeout=constants.PROCESS_KILL_TIMEOUT_S
)
except Exception as e:
logger.error(f"Error occurred during process wait: {e}")
alive = processes_found
gone = []
for proc in gone:
try:
logger.debug(
f"Process {proc.name()} (PID: {proc.pid}) terminated successfully."
)
except psutil.NoSuchProcess:
logger.debug(
f"Process (PID: {proc.pid}) terminated successfully (already gone)."
)
except Exception as e:
logger.warning(
f"Error getting info for terminated process PID {proc.pid}: {e}"
)
if alive:
logger.warning(
f"{len(alive)} processes did not terminate gracefully. Attempting to kill..."
)
for proc in alive:
try:
proc_name = proc.name()
proc_pid = proc.pid
logger.info(f"Force killing {proc_name} (PID: {proc_pid})...")
proc.kill()
proc.wait(timeout=constants.PROCESS_POST_KILL_WAIT_S)
logger.info(f"Force killed {proc_name} (PID: {proc_pid}).")
except psutil.NoSuchProcess:
logger.info(
f"Process {proc.name() if 'proc_name' in locals() else 'unknown'} (PID: {proc.pid if 'proc_pid' in locals() else 'unknown'}) exited after kill signal or before."
)
except psutil.AccessDenied:
logger.error(
f"Permission denied trying to force kill {proc.name()} (PID: {proc.pid})."
)
except Exception as e:
proc_id_str = (
f"{proc.name()}({proc.pid})"
if "proc" in locals() and proc.is_running()
else f"PID {proc.pid if 'proc' in locals() else 'unknown'}"
)
logger.error(f"Error force killing {proc_id_str}: {e}")
if terminated_names:
logger.info(f"Termination attempted for: {', '.join(terminated_names)}")
elif processes_found:
logger.warning(
"Processes were found, but none were marked for termination attempt (internal logic issue?)."
)
return bool(processes_found)