mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
double down on PD
This commit is contained in:
1 parent
9310d19857
commit
ef261d9724
30 files changed
+689
-432
No files matched your search
+14
-11
@@ -71,14 +71,14 @@ def _content_result(output: str) -> dict:
|
||||
raise ControlError("Could not decode the agent provider response", "INVALID_PROVIDER_RESPONSE") from exc
|
||||
|
||||
|
||||
def provider_call(adb: str, serial: str, method: str, arg: str | None = None, request: dict | None = None) -> dict:
|
||||
def provider_call(adb: str, serial: str, method: str, arg: str | None = None, request: dict | None = None, timeout: int = 30) -> dict:
|
||||
command = ["shell", "content", "call", "--uri", AUTHORITY, "--method", method]
|
||||
if arg is not None:
|
||||
command.extend(["--arg", arg])
|
||||
if request is not None:
|
||||
encoded = base64.b64encode(json.dumps(request, separators=(",", ":")).encode()).decode()
|
||||
command.extend(["--extra", f"request:s:{encoded}"])
|
||||
result = run_adb(adb, serial, *command)
|
||||
result = run_adb(adb, serial, *command, timeout=timeout)
|
||||
output = (result.stdout + result.stderr).decode(errors="replace")
|
||||
response = _content_result(output)
|
||||
if result.returncode != 0 and response.get("ok") is not False:
|
||||
@@ -248,15 +248,18 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
if not session.get("running") and session.get("artifactsAvailable") and not session.get("artifactsComplete"):
|
||||
state = wait_for_artifacts(adb, serial, state, args.timeout)
|
||||
session = state.get("session", {})
|
||||
remote_dir = session.get("logDir")
|
||||
if not remote_dir or not session.get("id") or not session.get("artifactsAvailable"):
|
||||
raise ControlError("No session artifact directory is available", "NO_SESSION_LOGS")
|
||||
destination = Path(args.destination or session["id"]).expanduser().resolve()
|
||||
result = run_adb(adb, serial, "pull", remote_dir, str(destination), timeout=120)
|
||||
response = provider_call(adb, serial, "logs", timeout=120)
|
||||
if response.get("ok") is False:
|
||||
error = response.get("error", {})
|
||||
raise ControlError(error.get("message", "log export failed"), error.get("code", "LOG_EXPORT_FAILED"))
|
||||
destination = Path(args.destination or f"{response['sessionId']}.zip").expanduser().resolve()
|
||||
result = run_adb(adb, serial, "exec-out", "run-as", "com.droiddeck.launcher", "cat", response["path"], timeout=120)
|
||||
if result.returncode != 0:
|
||||
detail = (result.stderr or result.stdout).decode(errors="replace").strip()
|
||||
raise ControlError(detail or "adb pull failed", "LOG_PULL_FAILED")
|
||||
return add_serial({"ok": True, "command": "logs", "sessionId": session["id"], "path": str(destination)}, serial)
|
||||
detail = result.stderr.decode(errors="replace").strip()
|
||||
raise ControlError(detail or "log export failed", "LOG_PULL_FAILED")
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_bytes(result.stdout)
|
||||
return add_serial({"ok": True, "command": "logs", "sessionId": response["sessionId"], "path": str(destination)}, serial)
|
||||
|
||||
if args.command == "screenshot":
|
||||
destination = Path(args.path).expanduser().resolve()
|
||||
@@ -302,7 +305,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
stop.add_argument("--timeout", type=float, default=90)
|
||||
commands.add_parser("resume", help="resume a suspended session")
|
||||
|
||||
logs = commands.add_parser("logs", help="pull the latest session artifact directory")
|
||||
logs = commands.add_parser("logs", help="export sanitized session logs as a ZIP")
|
||||
logs.add_argument("which", choices=["latest"])
|
||||
logs.add_argument("destination", nargs="?")
|
||||
logs.add_argument("--timeout", type=float, default=90)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import argparse
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
TOOLS = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(TOOLS))
|
||||
loader = importlib.machinery.SourceFileLoader("droiddeckctl", str(TOOLS / "droiddeckctl"))
|
||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||
ctl = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(ctl)
|
||||
|
||||
|
||||
class LogExportTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.destination = Path(self.tmp.name) / "session.zip"
|
||||
self.args = argparse.Namespace(command="logs", timeout=90, destination=str(self.destination))
|
||||
self.state = {"session": {"running": True, "id": "session1", "artifactsAvailable": True}}
|
||||
|
||||
def test_logs_fetch_only_the_provider_sanitized_zip(self):
|
||||
response = {"ok": True, "sessionId": "session1", "path": "/data/user/0/com.droiddeck.launcher/cache/shared-logs/session.zip"}
|
||||
result = subprocess.CompletedProcess([], 0, b"sanitized ZIP", b"")
|
||||
with patch.object(ctl, "get_state", return_value=self.state), patch.object(ctl, "provider_call", return_value=response) as provider, patch.object(ctl, "run_adb", return_value=result) as adb:
|
||||
output = ctl.execute(self.args, "adb", "device")
|
||||
provider.assert_called_once_with("adb", "device", "logs", timeout=120)
|
||||
adb.assert_called_once_with("adb", "device", "exec-out", "run-as", "com.droiddeck.launcher", "cat", response["path"], timeout=120)
|
||||
self.assertEqual(b"sanitized ZIP", self.destination.read_bytes())
|
||||
self.assertEqual(str(self.destination.resolve()), output["path"])
|
||||
|
||||
def test_export_failure_never_falls_back_to_pulling_raw_logs(self):
|
||||
response = {"ok": False, "error": {"code": "NO_SESSION_LOGS", "message": "unavailable"}}
|
||||
with patch.object(ctl, "get_state", return_value=self.state), patch.object(ctl, "provider_call", return_value=response), patch.object(ctl, "run_adb") as adb:
|
||||
with self.assertRaises(ctl.ControlError):
|
||||
ctl.execute(self.args, "adb", "device")
|
||||
adb.assert_not_called()
|
||||
self.assertFalse(self.destination.exists())
|
||||
|
||||
def test_failed_retrieval_does_not_write_a_partial_zip(self):
|
||||
response = {"ok": True, "sessionId": "session1", "path": "/private/session.zip"}
|
||||
result = subprocess.CompletedProcess([], 1, b"partial data", b"read failure")
|
||||
with patch.object(ctl, "get_state", return_value=self.state), patch.object(ctl, "provider_call", return_value=response), patch.object(ctl, "run_adb", return_value=result):
|
||||
with self.assertRaises(ctl.ControlError):
|
||||
ctl.execute(self.args, "adb", "device")
|
||||
self.assertFalse(self.destination.exists())
|
||||
Reference in new issue
Block a user