mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 16:51:29 +08:00
fix(video): discover browsers cross-platform and distinguish unavailable reviews
This commit is contained in:
@@ -136,6 +136,11 @@ class VideoLessonCapability(TurnCapability):
|
||||
self._layout_issues, base_dir, build, context.language
|
||||
)
|
||||
timings["render_check"] += round(time.perf_counter() - started, 3)
|
||||
if issues is None:
|
||||
attempts.append(
|
||||
{"attempt": attempt, "stage": "layout_unavailable", "errors": []}
|
||||
)
|
||||
break
|
||||
if not issues:
|
||||
break
|
||||
attempts.append({"attempt": attempt, "stage": "layout", "errors": issues})
|
||||
@@ -227,8 +232,8 @@ class VideoLessonCapability(TurnCapability):
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _layout_issues(base_dir: Path, build: Any, language: str) -> list[str]:
|
||||
"""Browser layout check of the freshly built bundle; empty when clean or unavailable."""
|
||||
def _layout_issues(base_dir: Path, build: Any, language: str) -> list[str] | None:
|
||||
"""Browser layout check of the freshly built bundle; empty when clean, None when unavailable."""
|
||||
from deeptutor.agents.video_lesson.layout_check import check_layout, sample_frames
|
||||
from deeptutor.agents.video_lesson.runtime import write_page
|
||||
|
||||
@@ -238,9 +243,9 @@ class VideoLessonCapability(TurnCapability):
|
||||
write_page(
|
||||
base_dir / "source" / "player.js", page, title="layout check", language=language
|
||||
)
|
||||
return check_layout(page, sample_frames(total)) or []
|
||||
return check_layout(page, sample_frames(total))
|
||||
except Exception: # the check is advisory; a broken probe must not fail the lesson
|
||||
return []
|
||||
return None
|
||||
finally:
|
||||
page.unlink(missing_ok=True)
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ samples points along every visible SVG stroke, and reports collisions in the
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
@@ -148,11 +149,38 @@ addEventListener("message", (e) => {
|
||||
|
||||
|
||||
def chrome_path() -> str | None:
|
||||
for name in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser"):
|
||||
"""Use an explicit browser first, then standard cross-platform installs."""
|
||||
configured = os.environ.get("DEEPTUTOR_CHROME_EXECUTABLE", "").strip()
|
||||
if configured:
|
||||
path = Path(configured).expanduser()
|
||||
return str(path) if path.is_file() else None
|
||||
for name in (
|
||||
"google-chrome",
|
||||
"google-chrome-stable",
|
||||
"chromium",
|
||||
"chromium-browser",
|
||||
"chrome",
|
||||
"msedge",
|
||||
):
|
||||
found = shutil.which(name)
|
||||
if found:
|
||||
return found
|
||||
return None
|
||||
candidates = [
|
||||
Path("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"),
|
||||
Path.home() / "Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
||||
Path("/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"),
|
||||
]
|
||||
for name in ("PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"):
|
||||
root = os.environ.get(name)
|
||||
if root:
|
||||
candidates.extend(
|
||||
Path(root) / suffix
|
||||
for suffix in (
|
||||
"Google/Chrome/Application/chrome.exe",
|
||||
"Microsoft/Edge/Application/msedge.exe",
|
||||
)
|
||||
)
|
||||
return next((str(path) for path in candidates if path.is_file()), None)
|
||||
|
||||
|
||||
def sample_frames(total: int, fps: int = 30, every_s: float = 4.0) -> list[int]:
|
||||
@@ -189,16 +217,26 @@ def check_layout(page: Path, frames: list[int], timeout_s: int = 180) -> list[st
|
||||
proc = subprocess.run(argv, capture_output=True, text=True, timeout=timeout_s) # nosec B603
|
||||
except subprocess.TimeoutExpired:
|
||||
return None
|
||||
if proc.returncode != 0:
|
||||
return None
|
||||
m = re.search(r'<pre id="out">(.*?)</pre>', proc.stdout, re.S)
|
||||
if not m or not m.group(1).strip():
|
||||
return None
|
||||
data = json.loads(
|
||||
m.group(1)
|
||||
.replace(""", '"')
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("&", "&")
|
||||
)
|
||||
try:
|
||||
data = json.loads(
|
||||
m.group(1)
|
||||
.replace(""", '"')
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("&", "&")
|
||||
)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
if not isinstance(data, dict) or any(
|
||||
not isinstance(data.get(str(frame)), dict) or data[str(frame)].get("error")
|
||||
for frame in frames
|
||||
):
|
||||
return None
|
||||
seen: dict[str, list[int]] = {}
|
||||
for frame in sorted(data, key=lambda f: int(f)):
|
||||
for issue in data[frame].get("issues") or []:
|
||||
|
||||
@@ -67,3 +67,12 @@ def test_stroke_through_text_and_caption_band_are_reported(tmp_path: Path) -> No
|
||||
assert issues is not None
|
||||
assert any('cross the text "Image layer"' in i for i in issues)
|
||||
assert any("caption band" in i for i in issues)
|
||||
|
||||
|
||||
def test_browser_executable_can_be_configured(monkeypatch, tmp_path):
|
||||
browser = tmp_path / "custom-chrome"
|
||||
browser.write_text("browser", encoding="utf-8")
|
||||
monkeypatch.setenv("DEEPTUTOR_CHROME_EXECUTABLE", str(browser))
|
||||
assert chrome_path() == str(browser)
|
||||
monkeypatch.setenv("DEEPTUTOR_CHROME_EXECUTABLE", str(tmp_path / "missing"))
|
||||
assert chrome_path() is None
|
||||
|
||||
Reference in New Issue
Block a user