A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
advisor_clips: validate before replacing the shipped clip; refuse a differing picture for --lang he (Fixes #2027) (#2036)
Beri5ff8ad3ce90d
diff --git a/changelog.d/2027.md b/changelog.d/2027.mdnew file mode 100644index 000000000..7f155076c--- /dev/null+++ b/changelog.d/2027.md@@ -0,0 +1,5 @@+### Fixed++- `tools/advisor_clips.py ingest` now checks the new picture in a temporary folder before it replaces the shipped clip, so a+ picture that fails the checks leaves the old one in place, and `--lang he` refuses a picture that differs from the+ shared `<message>.mp4`.diff --git a/tools/advisor_clips.py b/tools/advisor_clips.pyindex a6318946c..1e7cda12f 100644--- a/tools/advisor_clips.py+++ b/tools/advisor_clips.py@@ -96,18 +96,23 @@ def ingest(message: str, picture: Path, voice: Path, lang: str = "en", out: Pathbad = voice_problems(voice)if bad:raise ValueError("; ".join(bad))- out.mkdir(parents=True, exist_ok=True)pic, wav = out / f"{message}.mp4", out / f"{message}.{lang}.wav"- if ev.problems(picture):- with tempfile.TemporaryDirectory() as tmp: # the delivered file is read from a copy, never in place- src = Path(tmp) / "src.mp4"+ # The picture has no language tag and both languages share it, so another language may only add its voice.+ if lang != "en" and pic.exists() and pic.read_bytes() != picture.read_bytes():+ raise ValueError(f"{pic.name} is shared by both languages and differs from {picture}: --lang {lang} only adds {wav.name}; pass the shipped {pic.name}, not a raw take")+ out.mkdir(parents=True, exist_ok=True)+ with tempfile.TemporaryDirectory() as tmp: # built and checked here, so a failed ingest leaves the shipped file alone+ staged = Path(tmp) / pic.name+ if ev.problems(picture):+ src = Path(tmp) / "src.mp4" # the delivered file is read from a copy, never in placeshutil.copyfile(picture, src)- subprocess.run(ev.encode_command(src, pic), check=True)- else:- shutil.copyfile(picture, pic)- bad = ev.problems(pic)- if bad:- raise ValueError(f"{pic.name}: {'; '.join(bad)}")+ subprocess.run(ev.encode_command(src, staged), check=True)+ else:+ shutil.copyfile(picture, staged)+ bad = ev.problems(staged)+ if bad:+ raise ValueError(f"{pic.name}: {'; '.join(bad)}")+ shutil.move(staged, pic)shutil.copyfile(voice, wav)for f in (pic, wav):meta = f.with_name(f.name + ".meta")diff --git a/tools/tests/test_advisor_clips.py b/tools/tests/test_advisor_clips.pyindex ff37e5732..b8bfe2642 100644--- a/tools/tests/test_advisor_clips.py+++ b/tools/tests/test_advisor_clips.py@@ -70,3 +70,34 @@ def test_check_names_an_orphan_and_a_missing_record(tmp_path):found = ac.check(out=out)assert any("stray.mp4: no media row uses it" in f for f in found)assert any("no ASSETS.yaml row" in f for f in found), "files outside the repository have no ASSETS.yaml rows"+++def _stub_encoder(monkeypatch, problems_of):+ """Stands in for ffmpeg/ffprobe: problems() is read from the file's text, encode_command() writes a marker file."""+ monkeypatch.setattr(ev, "problems", problems_of)+ monkeypatch.setattr(ev, "encode_command", lambda src, dst, row=None: ["cp", str(src), str(dst)])+++def test_a_picture_that_fails_the_checks_leaves_the_shipped_clip_untouched(tmp_path, monkeypatch):+ _stub_encoder(monkeypatch, lambda p: ["not 960x540"])+ out = tmp_path / "advisors"; out.mkdir()+ (out / "x.msg.mp4").write_bytes(b"shipped")+ (tmp_path / "bad.mp4").write_bytes(b"bad take")+ with pytest.raises(ValueError):+ ac.ingest("x.msg", tmp_path / "bad.mp4", _wav(tmp_path / "v.wav"), out=out)+ assert (out / "x.msg.mp4").read_bytes() == b"shipped"+ assert sorted(f.name for f in out.iterdir()) == ["x.msg.mp4"], "no wav, meta or temp file left behind"+++def test_another_language_may_not_replace_the_shared_picture(tmp_path, monkeypatch):+ _stub_encoder(monkeypatch, lambda p: [])+ out = tmp_path / "advisors"; out.mkdir()+ (out / "x.msg.mp4").write_bytes(b"shipped")+ (tmp_path / "other.mp4").write_bytes(b"another picture")+ with pytest.raises(ValueError, match="shared by both languages"):+ ac.ingest("x.msg", tmp_path / "other.mp4", _wav(tmp_path / "v.wav"), lang="he", out=out)+ assert (out / "x.msg.mp4").read_bytes() == b"shipped"+ assert not (out / "x.msg.he.wav").exists()+ (tmp_path / "same.mp4").write_bytes(b"shipped")+ ac.ingest("x.msg", tmp_path / "same.mp4", _wav(tmp_path / "v.wav"), lang="he", out=out)+ assert (out / "x.msg.he.wav").is_file(), "the identical picture is accepted: only the voice is added"