fix(droiddeckctl): address review on logs latest

Stream each archive entry to disk instead of reading it into memory,
refuse an archive from a session that started after the state call, and
delete the exported zip from shared storage once it has been pulled.
This commit is contained in:
angelrenefm authored and Kurt Himebauch committed 2026-11-05 13:52:43 -05:00
1 parent 2fb066507a
commit 8cc652e812
2 files changed
+24 -6

No files matched your search

+14 -6
View File
@@ -434,7 +434,8 @@ def unpack_logs(archive: Path, destination: Path, session_id: str) -> None:
if entry.is_dir() or not name or not target.is_relative_to(destination):
continue
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(z.read(entry))
with z.open(entry) as source, target.open("wb") as copy:
shutil.copyfileobj(source, copy)
except (OSError, zipfile.BadZipFile) as exc:
raise ControlError(f"Could not unpack the session logs: {exc}", "LOG_PULL_FAILED") from exc
@@ -765,11 +766,18 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
result = run_adb(adb, serial, "pull", remote_dir, str(destination), timeout=120)
else:
zip_path = checked(exported, "LOG_PULL_FAILED")["zip"]
with tempfile.TemporaryDirectory() as temp:
local_zip = Path(temp) / "logs.zip"
result = run_adb(adb, serial, "pull", zip_path, str(local_zip), timeout=120)
if result.returncode == 0:
unpack_logs(local_zip, destination, exported.get("sessionId") or session["id"])
try:
if exported.get("sessionId", session["id"]) != session["id"]:
raise ControlError("A new session started while the logs were exported; run logs again",
"LOG_PULL_FAILED")
with tempfile.TemporaryDirectory() as temp:
local_zip = Path(temp) / "logs.zip"
result = run_adb(adb, serial, "pull", zip_path, str(local_zip), timeout=120)
if result.returncode == 0:
unpack_logs(local_zip, destination, session["id"])
finally:
# The copy sits in shared storage, so it does not outlive the pull.
run_adb(adb, serial, "shell", "rm", "-f", zip_path, timeout=30)
if result.returncode != 0:
detail = (result.stderr or result.stdout).decode(errors="replace").strip()
raise ControlError(detail or "adb pull failed", "LOG_PULL_FAILED")
+10
View File
@@ -542,6 +542,16 @@ class DroiddeckctlTest(FakeAdbTest):
self.assertEqual("e", (destination / "stores" / "epic.log").read_text())
self.assertFalse((self.dir / "outside").exists())
self.assertEqual([["pull", EXPORTED_ZIP]], [c["args"][:2] for c in self.calls() if c["args"][:1] == ["pull"]])
self.assertIn(["shell", "rm", "-f", EXPORTED_ZIP], [c["args"] for c in self.calls()])
def test_logs_of_a_session_that_started_meanwhile_are_refused(self):
self.configure({"state": ended_state(), "logs": {"ok": True, "sessionId": "s2", "zip": EXPORTED_ZIP}},
files={EXPORTED_ZIP: share_zip({"s2/session.log": "other"})})
code, out = self.run_ctl("logs", "latest", str(self.dir / "artifacts"))
self.assertEqual(6, code)
self.assertEqual("LOG_PULL_FAILED", out["error"]["code"])
self.assertFalse((self.dir / "artifacts").exists())
self.assertIn(["shell", "rm", "-f", EXPORTED_ZIP], [c["args"] for c in self.calls()])
def test_logs_from_an_older_build_fall_back_to_the_private_folder(self):
self.configure({"state": ended_state()})