Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(parity): fix mobile device preflight resolver ordering

laurenspriemaec38ec05d8b
diff --git a/infra/ml/test/run_ml_parity_tests.sh b/infra/ml/test/run_ml_parity_tests.sh
index 7b5db50ff5..aaf12b6476 100755
--- a/infra/ml/test/run_ml_parity_tests.sh
+++ b/infra/ml/test/run_ml_parity_tests.sh
@@ -462,12 +462,202 @@ print(hashlib.sha256(sys.argv[1].encode("utf-8")).hexdigest())
PY
}
+platform_device_id() {
+ local platform="$1"
+ local requested_device_id="${2:-}"
+ local selected
+ selected="$(
+ python3 - "$platform" "$requested_device_id" <<'PY'
+from __future__ import annotations
+
+import json
+import subprocess
+import sys
+
+platform = sys.argv[1]
+requested_device_id = sys.argv[2]
+
+
+def command_output(command: list[str]) -> str | None:
+ try:
+ return subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
+ except Exception:
+ return None
+
+
+def command_json(command: list[str]) -> object | None:
+ output = command_output(command)
+ if output is None:
+ return None
+ try:
+ return json.loads(output)
+ except json.JSONDecodeError:
+ return None
+
+raw = command_output(["flutter", "devices", "--machine"])
+if raw is None:
+ sys.exit(2)
+
+try:
+ devices = json.loads(raw)
+except json.JSONDecodeError:
+ sys.exit(2)
+
+if not isinstance(devices, list):
+ sys.exit(2)
+
+
+def is_match(device: dict[str, object]) -> bool:
+ target = str(device.get("targetPlatform", "")).lower()
+ name = str(device.get("name", "")).lower()
+ if platform == "android":
+ return "android" in target or "android" in name
+ if platform == "ios":
+ return "ios" in target or "iphone" in name or "ipad" in name
+ return False
+
+
+def ios_simulator_metadata() -> dict[str, dict[str, object]]:
+ payload = command_json(["xcrun", "simctl", "list", "devices", "--json"])
+ if not isinstance(payload, dict):
+ return {}
+ devices_by_runtime = payload.get("devices", {})
+ if not isinstance(devices_by_runtime, dict):
+ return {}
+
+ metadata: dict[str, dict[str, object]] = {}
+ for simulator_list in devices_by_runtime.values():
+ if not isinstance(simulator_list, list):
+ continue
+ for simulator in simulator_list:
+ if not isinstance(simulator, dict):
+ continue
+ udid = str(simulator.get("udid", "")).strip()
+ if not udid:
+ continue
+ metadata[udid] = {
+ "is_available": bool(simulator.get("isAvailable", True)),
+ "is_booted": str(simulator.get("state", "")).strip().lower() == "booted",
+ }
+ return metadata
+
+
+def ios_physical_available_ids() -> set[str]:
+ payload = command_json(["xcrun", "xcdevice", "list"])
+ if not isinstance(payload, list):
+ return set()
+
+ available_ids: set[str] = set()
+ for device in payload:
+ if not isinstance(device, dict):
+ continue
+ if bool(device.get("simulator", False)):
+ continue
+ identifier = str(device.get("identifier", "")).strip()
+ if not identifier:
+ continue
+ if bool(device.get("available", False)):
+ available_ids.add(identifier)
+ return available_ids
+
+
+def android_device_states() -> dict[str, str]:
+ adb_output = command_output(["adb", "devices"])
+ if adb_output is None:
+ return {}
+
+ states: dict[str, str] = {}
+ for line in adb_output.splitlines()[1:]:
+ parts = line.strip().split()
+ if len(parts) >= 2:
+ states[parts[0]] = parts[1]
+ return states
+
+
+ios_simulators = ios_simulator_metadata() if platform == "ios" else {}
+ios_physical_ids = ios_physical_available_ids() if platform == "ios" else set()
+android_states = android_device_states() if platform == "android" else {}
+android_state_probe_attempted = platform == "android"
+
+candidates: list[tuple[tuple[int, str, int], str]] = []
+for index, device in enumerate(devices):
+ if not isinstance(device, dict):
+ continue
+ if not is_match(device):
+ continue
+ if not bool(device.get("isSupported", True)):
+ continue
+
+ identifier = str(device.get("id", "")).strip()
+ if not identifier:
+ continue
+
+ name = str(device.get("name", "")).strip().lower()
+ is_emulator = bool(device.get("emulator", False))
+
+ if platform == "ios":
+ if is_emulator:
+ simulator = ios_simulators.get(identifier)
+ if simulator is not None:
+ if not bool(simulator.get("is_available", True)):
+ continue
+ score = 0 if bool(simulator.get("is_booted", False)) else 1
+ else:
+ # Keep flutter-visible simulators even when simctl metadata is unavailable.
+ score = 2
+ else:
+ if ios_physical_ids and identifier not in ios_physical_ids:
+ continue
+ score = 10
+ elif platform == "android":
+ if android_states:
+ if android_states.get(identifier) != "device":
+ continue
+ elif android_state_probe_attempted:
+ # adb unavailable/unreadable; keep flutter-visible devices as fallback.
+ pass
+
+ if is_emulator or identifier.startswith("emulator-"):
+ score = 0
+ else:
+ score = 1
+ else:
+ continue
+
+ candidates.append(((score, name, index), identifier))
+
+if requested_device_id:
+ for _, identifier in candidates:
+ if identifier == requested_device_id:
+ print(identifier)
+ sys.exit(0)
+ sys.exit(1)
+
+if not candidates:
+ sys.exit(1)
+
+candidates.sort(key=lambda item: item[0])
+print(candidates[0][1])
+sys.exit(0)
+PY
+ )"
+
+ if [[ -n "$selected" ]]; then
+ echo "$selected"
+ return 0
+ fi
+
+ return 1
+}
+
preflight_platform_device_available() {
local platform="$1"
+ local device_lookup_exit=0
if platform_device_id "$platform" >/dev/null 2>&1; then
return 0
+ else
+ device_lookup_exit=$?
fi
- local device_lookup_exit=$?
case "$device_lookup_exit" in
1)
return 1
@@ -482,8 +672,11 @@ preflight_platform_device_id_available() {
local platform="$1"
local device_id="$2"
local selected
- if ! selected="$(platform_device_id "$platform" "$device_id" 2>/dev/null)"; then
- local device_lookup_exit=$?
+ local device_lookup_exit=0
+ if selected="$(platform_device_id "$platform" "$device_id" 2>/dev/null)"; then
+ :
+ else
+ device_lookup_exit=$?
case "$device_lookup_exit" in
1)
return 1
@@ -1276,194 +1469,6 @@ platform_device_available() {
preflight_platform_device_available "$platform"
}
-platform_device_id() {
- local platform="$1"
- local requested_device_id="${2:-}"
- local selected
- selected="$(
- python3 - "$platform" "$requested_device_id" <<'PY'
-from __future__ import annotations
-
-import json
-import subprocess
-import sys
-
-platform = sys.argv[1]
-requested_device_id = sys.argv[2]
-
-
-def command_output(command: list[str]) -> str | None:
- try:
- return subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
- except Exception:
- return None
-
-
-def command_json(command: list[str]) -> object | None:
- output = command_output(command)
- if output is None:
- return None
- try:
- return json.loads(output)
- except json.JSONDecodeError:
- return None
-
-raw = command_output(["flutter", "devices", "--machine"])
-if raw is None:
- sys.exit(2)
-
-try:
- devices = json.loads(raw)
-except json.JSONDecodeError:
- sys.exit(2)
-
-if not isinstance(devices, list):
- sys.exit(2)
-
-
-def is_match(device: dict[str, object]) -> bool:
- target = str(device.get("targetPlatform", "")).lower()
- name = str(device.get("name", "")).lower()
- if platform == "android":
- return "android" in target or "android" in name
- if platform == "ios":
- return "ios" in target or "iphone" in name or "ipad" in name
- return False
-
-
-def ios_simulator_metadata() -> dict[str, dict[str, object]]:
- payload = command_json(["xcrun", "simctl", "list", "devices", "--json"])
- if not isinstance(payload, dict):
- return {}
- devices_by_runtime = payload.get("devices", {})
- if not isinstance(devices_by_runtime, dict):
- return {}
-
- metadata: dict[str, dict[str, object]] = {}
- for simulator_list in devices_by_runtime.values():
- if not isinstance(simulator_list, list):
- continue
- for simulator in simulator_list:
- if not isinstance(simulator, dict):
- continue
- udid = str(simulator.get("udid", "")).strip()
- if not udid:
- continue
- metadata[udid] = {
- "is_available": bool(simulator.get("isAvailable", True)),
- "is_booted": str(simulator.get("state", "")).strip().lower() == "booted",
- }
- return metadata
-
-
-def ios_physical_available_ids() -> set[str]:
- payload = command_json(["xcrun", "xcdevice", "list"])
- if not isinstance(payload, list):
- return set()
-
- available_ids: set[str] = set()
- for device in payload:
- if not isinstance(device, dict):
- continue
- if bool(device.get("simulator", False)):
- continue
- identifier = str(device.get("identifier", "")).strip()
- if not identifier:
- continue
- if bool(device.get("available", False)):
- available_ids.add(identifier)
- return available_ids
-
-
-def android_device_states() -> dict[str, str]:
- adb_output = command_output(["adb", "devices"])
- if adb_output is None:
- return {}
-
- states: dict[str, str] = {}
- for line in adb_output.splitlines()[1:]:
- parts = line.strip().split()
- if len(parts) >= 2:
- states[parts[0]] = parts[1]
- return states
-
-
-ios_simulators = ios_simulator_metadata() if platform == "ios" else {}
-ios_physical_ids = ios_physical_available_ids() if platform == "ios" else set()
-android_states = android_device_states() if platform == "android" else {}
-android_state_probe_attempted = platform == "android"
-
-candidates: list[tuple[tuple[int, str, int], str]] = []
-for index, device in enumerate(devices):
- if not isinstance(device, dict):
- continue
- if not is_match(device):
- continue
- if not bool(device.get("isSupported", True)):
- continue
-
- identifier = str(device.get("id", "")).strip()
- if not identifier:
- continue
-
- name = str(device.get("name", "")).strip().lower()
- is_emulator = bool(device.get("emulator", False))
-
- if platform == "ios":
- if is_emulator:
- simulator = ios_simulators.get(identifier)
- if simulator is not None:
- if not bool(simulator.get("is_available", True)):
- continue
- score = 0 if bool(simulator.get("is_booted", False)) else 1
- else:
- # Keep flutter-visible simulators even when simctl metadata is unavailable.
- score = 2
- else:
- if ios_physical_ids and identifier not in ios_physical_ids:
- continue
- score = 10
- elif platform == "android":
- if android_states:
- if android_states.get(identifier) != "device":
- continue
- elif android_state_probe_attempted:
- # adb unavailable/unreadable; keep flutter-visible devices as fallback.
- pass
-
- if is_emulator or identifier.startswith("emulator-"):
- score = 0
- else:
- score = 1
- else:
- continue
-
- candidates.append(((score, name, index), identifier))
-
-if requested_device_id:
- for _, identifier in candidates:
- if identifier == requested_device_id:
- print(identifier)
- sys.exit(0)
- sys.exit(1)
-
-if not candidates:
- sys.exit(1)
-
-candidates.sort(key=lambda item: item[0])
-print(candidates[0][1])
-sys.exit(0)
-PY
- )"
-
- if [[ -n "$selected" ]]; then
- echo "$selected"
- return 0
- fi
-
- return 1
-}
-
run_mobile_runner() {
local platform="$1"
local target="$2"