use sync queue instead of list, just in case...

This commit is contained in:
Jean-Marie Mineau 2025-05-21 10:39:29 +02:00
parent 08b5a603ee
commit bcd7687dcb
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2

View file

@ -7,6 +7,7 @@ import time
import subprocess import subprocess
import threading import threading
import argparse import argparse
import queue
EMULATORS = [f"root34-{i}" for i in range(20)] EMULATORS = [f"root34-{i}" for i in range(20)]
ANDROID_IMG = "system-images;android-34;default;x86_64" ANDROID_IMG = "system-images;android-34;default;x86_64"
@ -233,15 +234,17 @@ def restore_emu(emu: str, proc: None | subprocess.Popen) -> subprocess.Popen:
return proc return proc
def worker(emu: str, apklist: list[str], out_folder: Path, script: Path): def worker(emu: str, apklist: queue.Queue[str], out_folder: Path, script: Path):
console_port, adb_port = get_ports(emu) console_port, adb_port = get_ports(emu)
script_env = os.environ.copy() script_env = os.environ.copy()
script_env["ANDROID_HOME"] = str(ANDROID_HOME) script_env["ANDROID_HOME"] = str(ANDROID_HOME)
proc_emu = restore_emu(emu, None) proc_emu = restore_emu(emu, None)
while apklist: while True:
apk = apklist.pop() apk = apklist.get()
folder_name = apk.split("/")[-1].removesuffix(".apk") folder_name = apk.split("/")[-1].removesuffix(".apk")
folder = out_folder / folder_name folder = out_folder / folder_name
# Check if XP has already run without error or timeout
if folder.exists() and (folder / "data.json").exists(): if folder.exists() and (folder / "data.json").exists():
has_error = False has_error = False
with (folder / "data.json").open() as fp: with (folder / "data.json").open() as fp:
@ -256,7 +259,10 @@ def worker(emu: str, apklist: list[str], out_folder: Path, script: Path):
) )
shutil.rmtree(str(folder)) shutil.rmtree(str(folder))
else: else:
# We already have a valid result, mark task done and skip xp
apklist.task_done()
continue continue
folder.mkdir(parents=True) folder.mkdir(parents=True)
with ( with (
@ -264,8 +270,9 @@ def worker(emu: str, apklist: list[str], out_folder: Path, script: Path):
(folder / "analysis.err").open("w") as fp_anly_stderr, (folder / "analysis.err").open("w") as fp_anly_stderr,
): ):
# Start emulator with wipped data
print(f"START ANALYSIS: {apk=}, emulator-{console_port}") print(f"START ANALYSIS: {apk=}, emulator-{console_port}")
# Reset the emulator and make sure it is runing
i = 0 i = 0
started = False started = False
while not started: while not started:
@ -293,13 +300,16 @@ def worker(emu: str, apklist: list[str], out_folder: Path, script: Path):
break break
j += 1 j += 1
i += 1 i += 1
print(f"emulator-{console_port} running") print(f"emulator-{console_port} running")
fp_anly_stdout.write(f"START ANALYSIS: {apk=}, emulator-{console_port}\n") fp_anly_stdout.write(f"START ANALYSIS: {apk=}, emulator-{console_port}\n")
# should help debuging:
subprocess.run( subprocess.run(
[ADB, "devices"], [ADB, "devices"],
stdout=fp_anly_stdout, stdout=fp_anly_stdout,
stderr=fp_anly_stderr, stderr=fp_anly_stderr,
) )
# Run script # Run script
try: try:
subprocess.run( subprocess.run(
@ -310,24 +320,29 @@ def worker(emu: str, apklist: list[str], out_folder: Path, script: Path):
timeout=TIMEOUT, timeout=TIMEOUT,
) )
print(f"FINISHED ANALYSIS: {apk=}, emulator-{console_port}") print(f"FINISHED ANALYSIS: {apk=}, emulator-{console_port}")
# If timeout:
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
with (folder / "TIMEOUT").open("w") as fp: with (folder / "TIMEOUT").open("w") as fp:
fp.write("Process timedout") fp.write("Process timedout")
print(f"TIMEOUT ANALYSIS: {apk=}, emulator-{console_port}") print(f"TIMEOUT ANALYSIS: {apk=}, emulator-{console_port}")
# again, for debuging:
with (folder / "emu").open("w") as fp: with (folder / "emu").open("w") as fp:
fp.write(f"Used emulator {emu}: emulator-{console_port}") fp.write(f"Used emulator {emu}: emulator-{console_port}")
apklist.task_done()
def run(apklist: list[str], out_folder: Path, script: Path): def run(apklist: list[str], out_folder: Path, script: Path):
gen_emulators() gen_emulators()
workers = [] workers = []
q: queue.Queue[str] = queue.Queue()
for apk in apklist:
q.put(apk)
for emu in EMULATORS: for emu in EMULATORS:
workers.append( workers.append(
threading.Thread(target=lambda: worker(emu, apklist, out_folder, script)) threading.Thread(target=lambda: worker(emu, q, out_folder, script))
) )
workers[-1].start() workers[-1].start()
for w in workers: q.join()
w.join()
def main(): def main():