diff --git a/lib/aaif_events/slides_export.py b/lib/aaif_events/slides_export.py index 8b118f9..6ae15e3 100644 --- a/lib/aaif_events/slides_export.py +++ b/lib/aaif_events/slides_export.py @@ -26,6 +26,28 @@ _gws_json = _gws_mod.json_out +_PNG_MAGIC = b"\x89PNG\r\n\x1a\n" + + +def _page_text(page): + """The text of a Slides API page: text in shapes, recursing into groups + (tables are not read). Runs within a shape are joined with nothing, since + one word can be split across runs and each paragraph's last run already + ends in a newline. Shapes are joined with a space.""" + out = [] + + def walk(elements): + for el in elements or (): + shape_text = "".join( + (text_el.get("textRun") or {}).get("content") or "" + for text_el in ((el.get("shape") or {}).get("text") or {}).get("textElements", ())) + if shape_text: + out.append(shape_text) + walk((el.get("elementGroup") or {}).get("children")) + walk(page.get("pageElements")) + return " ".join(out) + + def _download(url, out_path, timeout=30): """Fetch `url` to `out_path` with a bounded timeout. The bytes land in a temp file that is renamed over `out_path` only after the too-small sanity @@ -39,18 +61,39 @@ def _download(url, out_path, timeout=30): fh.write(r.read()) if os.path.getsize(tmp) < 1000: raise RuntimeError("rendered thumbnail suspiciously small (%s)" % out_path) + # Size alone let an HTML interstitial or error page through as a + # "PNG", and a caller that stamps what it uploads would then mark that + # page current and never look at it again. + with open(tmp, "rb") as fh: + if fh.read(8) != _PNG_MAGIC: + raise RuntimeError("rendered thumbnail is not a PNG (%s)" % out_path) os.replace(tmp, out_path) finally: if os.path.exists(tmp): # gone on success (os.replace consumed it) os.remove(tmp) -def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000_PX"): +def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000_PX", + check_text=None, strict_cleanup=False): """Export slide `slide_index` (0-based) of the Drive pptx `file_id` to a PNG at `out_path`. Makes a throwaway Google Slides copy to render from (Slides thumbnails only work on native Slides files, not stored .pptx blobs) and - trashes it afterward — the source file is never modified.""" + trashes it afterward — the source file is never modified. + + `check_text(text) -> error string or None` is run on the text of the page + that is about to be rendered, the same page object, so a caller can refuse + a slide without trusting that `slideN.xml` numbering follows presentation + order. It is a refusal, not a predicate: anything but None or a str raises + TypeError, so a `lambda t: want in t` cannot pass every wrong slide. + + A failure to trash the throwaway copy is always printed to stderr. + `strict_cleanup` also makes it part of the raised error (naming the copy's + id), whether the render succeeded or failed on its own, for callers that + report per-file results: a stray copy beside TemplateCity's decks is + cloned into every later chapter.""" presentation_id = None + cleanup_error = None + body_error = None try: # NO_RETRY: a `files.copy` that succeeded server-side but answered like # a timeout would, on a retry, make a SECOND throwaway copy — and the @@ -63,16 +106,33 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 body={"name": "TEMP - render_slide_png", "mimeType": _SLIDES_MIME}) presentation_id = copy["id"] + fields = ("slides(objectId,pageElements(shape/text/textElements/textRun/content," + "elementGroup))") if check_text else "slides.objectId" presentation = _gws_json("slides", "presentations", "get", - params={"presentationId": presentation_id, "fields": "slides.objectId"}) - page_object_id = presentation["slides"][slide_index]["objectId"] + params={"presentationId": presentation_id, "fields": fields}) + slides = presentation.get("slides") or [] + if slide_index >= len(slides): + raise RuntimeError("deck %s has %d slide(s); slide %d was requested" + % (file_id, len(slides), slide_index + 1)) + page_object_id = slides[slide_index]["objectId"] + if check_text: + err = check_text(_page_text(slides[slide_index])) + if err is not None and not isinstance(err, str): + raise TypeError("check_text must return an error string or None, got %r" + % (err,)) + if err: + raise RuntimeError(err) thumb = _gws_json("slides", "presentations", "pages", "getThumbnail", params={"presentationId": presentation_id, "pageObjectId": page_object_id, "thumbnailProperties.thumbnailSize": thumbnail_size}) os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True) + if not thumb.get("contentUrl"): + raise RuntimeError("no thumbnail URL for slide %d of %s" % (slide_index + 1, file_id)) _download(thumb["contentUrl"], out_path) - return out_path + except Exception as e: + body_error = e + raise finally: # The throwaway copy has no `parents` set, so it lands in the SAME # folder as the source file - not an out-of-sight scratch location. @@ -90,3 +150,16 @@ def render_slide_png(file_id, out_path, slide_index=0, thumbnail_size="WIDTH2000 except Exception as e: print("WARNING: could not trash temp Slides copy %s: %s" % (presentation_id, e), file=sys.stderr) + cleanup_error = e + if strict_cleanup and body_error is not None: + # Replaces the in-flight error on purpose, carrying it, so + # the stray id reaches the caller's failure line. + raise RuntimeError( + "%s; ALSO the temp Slides copy %s beside the source could not " + "be trashed (%s); trash it by hand" + % (body_error, presentation_id, e)) from body_error + if cleanup_error is not None and strict_cleanup: + raise RuntimeError("rendered, but the temp Slides copy %s beside the source " + "could not be trashed (%s); trash it by hand" + % (presentation_id, cleanup_error)) + return out_path diff --git a/lib/aaif_events/tests/test_slides_export.py b/lib/aaif_events/tests/test_slides_export.py index ddac027..08c8269 100644 --- a/lib/aaif_events/tests/test_slides_export.py +++ b/lib/aaif_events/tests/test_slides_export.py @@ -197,6 +197,117 @@ def test_successful_render_returns_out_path_and_trashes_copy(self): self.assertEqual(out, "/tmp/out.png") +class TestRenderGuards(unittest.TestCase): + """The page-text check, the slide count and strict cleanup.""" + + PAGE = {"objectId": "p4", "pageElements": [ + {"shape": {"text": {"textElements": [{"textRun": {"content": "AAIF Community Events"}}]}}}, + {"elementGroup": {"children": [ + {"shape": {"text": {"textElements": [{"textRun": {"content": "Boston"}}]}}}]}}]} + + def _run(self, slides, trash_fails=False, **kw): + calls = [] + + def fake(*args, **kwargs): + calls.append((args, kwargs)) + if args == ("drive", "files", "copy"): + return {"id": "pres1"} + if args == ("slides", "presentations", "get"): + return {"slides": slides} + if args[-1] == "getThumbnail": + return {"contentUrl": "https://example.invalid/x.png"} + if args == ("drive", "files", "update"): + if trash_fails: + raise RuntimeError("trash failed") + return {} + raise AssertionError(args) + with mock.patch.object(se, "_gws_json", side_effect=fake), \ + mock.patch.object(se, "_download") as dl, \ + mock.patch("sys.stderr"): + try: + return se.render_slide_png("file1", "/tmp/out.png", **kw), calls, dl + except RuntimeError as e: + return e, calls, dl + + def test_check_text_sees_the_page_being_rendered_including_groups(self): + seen = [] + out, calls, dl = self._run([{"objectId": "p1"}, self.PAGE], slide_index=1, + check_text=lambda t: seen.append(t)) + self.assertEqual(out, "/tmp/out.png") + self.assertEqual(seen, ["AAIF Community Events Boston"]) + thumb = [k for a, k in calls if a[-1] == "getThumbnail"][0] + self.assertEqual(thumb["params"]["pageObjectId"], "p4") + + def test_a_refused_page_is_never_rendered_and_the_copy_is_trashed(self): + err, calls, dl = self._run([self.PAGE], check_text=lambda t: "wrong city") + self.assertIn("wrong city", str(err)) + self.assertFalse(dl.called) + self.assertNotIn("getThumbnail", [a[-1] for a, _ in calls]) + self.assertIn(("drive", "files", "update"), [a for a, _ in calls]) + + def test_a_short_deck_names_the_slide_and_the_count(self): + err, _c, _d = self._run([{"objectId": "p1"}], slide_index=3) + self.assertIn("has 1 slide(s); slide 4", str(err)) + + def test_strict_cleanup_raises_with_the_orphan_id(self): + err, _c, _d = self._run([self.PAGE], trash_fails=True, strict_cleanup=True) + self.assertIn("pres1", str(err)) + self.assertIn("could not be trashed", str(err)) + + def test_default_cleanup_failure_still_returns(self): + out, _c, _d = self._run([self.PAGE], trash_fails=True) + self.assertEqual(out, "/tmp/out.png") + + def test_strict_cleanup_names_the_orphan_when_the_render_also_failed(self): + err, _c, dl = self._run([self.PAGE], trash_fails=True, strict_cleanup=True, + check_text=lambda t: "wrong city") + self.assertIn("wrong city", str(err)) + self.assertIn("pres1", str(err)) + self.assertIn("could not be trashed", str(err)) + self.assertFalse(dl.called) + + def test_a_boolean_check_text_is_a_type_error_not_a_pass(self): + with mock.patch.object(se, "_gws_json", side_effect=lambda *a, **k: ( + {"id": "pres1"} if a == ("drive", "files", "copy") + else {"slides": [self.PAGE]} if a == ("slides", "presentations", "get") + else {})), mock.patch.object(se, "_download"): + with self.assertRaises(TypeError): + se.render_slide_png("file1", "/tmp/out.png", check_text=lambda t: False) + + def test_the_fields_mask_asks_for_text_only_when_checking(self): + _o, calls, _d = self._run([self.PAGE], check_text=lambda t: None) + get = [k for a, k in calls if a == ("slides", "presentations", "get")][0] + self.assertIn("textRun/content", get["params"]["fields"]) + self.assertIn("elementGroup", get["params"]["fields"]) + _o, calls, _d = self._run([self.PAGE]) + get = [k for a, k in calls if a == ("slides", "presentations", "get")][0] + self.assertEqual(get["params"]["fields"], "slides.objectId") + + def test_slide_index_equal_to_the_count_is_refused(self): + err, _c, _d = self._run([{"objectId": "p1"}], slide_index=1) + self.assertIn("has 1 slide(s); slide 2", str(err)) + + def test_a_missing_thumbnail_url_says_so(self): + def fake(*args, **kwargs): + if args == ("drive", "files", "copy"): + return {"id": "pres1"} + if args == ("slides", "presentations", "get"): + return {"slides": [{"objectId": "p1"}]} + return {} + with mock.patch.object(se, "_gws_json", side_effect=fake), \ + mock.patch.object(se, "_download") as dl: + with self.assertRaisesRegex(RuntimeError, "no thumbnail URL"): + se.render_slide_png("file1", "/tmp/out.png") + self.assertFalse(dl.called) + + def test_a_word_split_across_runs_in_one_shape_is_read_whole(self): + page = {"pageElements": [{"shape": {"text": {"textElements": [ + {"textRun": {"content": "AAIF Community Events\n"}}, + {"textRun": {"content": "Amster"}}, {"textRun": {"content": "dam\n"}}]}}}]} + self.assertEqual(" ".join(se._page_text(page).split()), + "AAIF Community Events Amsterdam") + + class TestDownload(unittest.TestCase): """_download must never leave a partial PNG behind, whatever failed.""" @@ -210,9 +321,9 @@ def test_success_writes_the_file_with_a_timeout(self): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "x.png") with mock.patch("urllib.request.urlopen", - return_value=self._resp(b"p" * 2000)) as uo: + return_value=self._resp(se._PNG_MAGIC + b"p" * 2000)) as uo: self.assertIsNone(se._download("https://example.invalid/x.png", out)) - self.assertEqual(os.path.getsize(out), 2000) + self.assertEqual(os.path.getsize(out), 2008) self.assertEqual(uo.call_args.kwargs.get("timeout"), 30) def test_a_too_small_render_is_deleted_not_left_behind(self): @@ -224,6 +335,17 @@ def test_a_too_small_render_is_deleted_not_left_behind(self): self.assertIn("suspiciously small", str(cm.exception)) self.assertFalse(os.path.exists(out)) + def test_a_non_png_body_is_refused_and_deleted(self): + """An HTML interstitial is large enough to pass the size check.""" + with tempfile.TemporaryDirectory() as d: + out = os.path.join(d, "x.png") + with mock.patch("urllib.request.urlopen", + return_value=self._resp(b"" + b"x" * 2000)): + with self.assertRaises(RuntimeError) as cm: + se._download("https://example.invalid/x.png", out) + self.assertIn("not a PNG", str(cm.exception)) + self.assertEqual(os.listdir(d), []) + def test_a_read_failure_deletes_the_partial_file(self): r = self._resp(b"") r.read.side_effect = TimeoutError("timed out") diff --git a/skills/aaif-create-chapter/SKILL.md b/skills/aaif-create-chapter/SKILL.md index 0c36b70..7c3a1e3 100644 --- a/skills/aaif-create-chapter/SKILL.md +++ b/skills/aaif-create-chapter/SKILL.md @@ -172,6 +172,10 @@ template or the rebrand engine is broken. Fix that — don't resume. render_slide_png('', '$(mktemp -d)/slide5.png', slide_index=4) " ``` +- [ ] The run printed **`Rendered Web Banner.png`** (or `Web Banner.png already + current.` on a `--resume`). If it warned instead, the banner still says + the template city: run the command it names. If it said "not rendered: + fresh files still carry source tokens", fix the template first (above). - [ ] If the Luma page wasn't live, the user has been reminded to create it. - [ ] Unit tests still pass after any engine edit: ```bash @@ -192,6 +196,32 @@ template or the rebrand engine is broken. Fix that — don't resume. - **A chapter is not born with its own agent art.** Cloning TemplateCity would hand it TemplateCity's agent. That is `upload_agents.py`'s job — see `references/design-system-sweep.md`. +- **`Web Banner.png` is rendered, not cloned.** The city is drawn into the + image, so no text rebrand reaches it, and until 2026-11 every chapter cloned + from TemplateCity kept its "San Francisco" banner. `render_web_banner.py` + renders it from the fourth slide of the chapter's own + `Banners (…)/Square Logo.pptx` through Google's renderer. + - `create_chapter` calls it once no freshly cloned file carries source + tokens. Exit `2` if the banner failed, the same code as dirty existing files + under `--resume`; a dirty existing `Square Logo.pptx` is not rendered at all + until `--repair-existing` fixes it. `rename_chapter` calls it only after + the rename verifies (exit `1` if it failed). + - It refuses a slide that does not say exactly "AAIF Community Events" plus + the chapter's name (ignoring case, extra spaces and punctuation at the ends + of the city). + - It skips a banner stamped with the current deck and name, so a re-run is + cheap. + - A sweep leaves TemplateCity alone unless named with `--chapter`, and never + adds a banner to a folder that has none. `--chapter X --create` does, which + is what `create_chapter` prints when its render failed before the file + existed. + - Exit codes: `2` report-only with banners to render, `1` any failure, `0` + otherwise (folders with no banner are listed, not failed). + ```bash + python3 /scripts/render_web_banner.py # report + python3 /scripts/render_web_banner.py --write + python3 /scripts/render_web_banner.py --chapter --create --write + ``` - **`create_chapter` writes no Chapters List row**, so its 100-chapter cap check is an early warning, not a guarantee: three runs at 99 live chapters each read 99, each pass, and the next sync refuses all three rows together. diff --git a/skills/aaif-create-chapter/references/rename-chapter.md b/skills/aaif-create-chapter/references/rename-chapter.md index 08c3cbb..ba23000 100644 --- a/skills/aaif-create-chapter/references/rename-chapter.md +++ b/skills/aaif-create-chapter/references/rename-chapter.md @@ -65,6 +65,22 @@ Also deliberate: run and a re-run would double-apply it. - **A mid-run upload failure says exactly what landed** — which files were uploaded, that no names were changed, and where the originals are. +- **What the text pass cannot see, checked 2026-11-03.** The Japan and New + Zealand renames verified clean while four things still said the old city: + - **`Web Banner.png`** has the name drawn in. A `--write` that verifies now + re-renders it from `Square Logo.pptx` (`render_web_banner.py`) and exits + `1` if that fails. A failed verify skips it and prints the command to run + after the fix. A folder with no banner is not given one. + - **The `Badges/` set** has file names in a *lowercase* slug, which the + Title/UPPER name pass does not match, and its SVG/PNG contents are never + opened (only `.docx`/`.pptx`/`.xlsx` are rewritten). Regenerate it with + `aaif-sync-badges` (`--chapter `, a case-insensitive substring match, + so check it picked only the one chapter), then trash the old-slug files. + - **Luma links** stay until you pass `--slug-from`/`--slug-to`. Both pages had + moved, so the links pointed to redirects. + - **The Chapters List `Summary`** cell ("AAIF Tokyo connects…") is on the + feed sheet, outside the chapter folder. Edit it there with the rest of the + row (next bullet). - **The Chapters List row, the intake's city cells and Slack channels are NOT touched.** The first two are `aaif-sync-chapters`' surfaces and must be updated for the engines to keep matching the chapter to its folder; a channel rename diff --git a/skills/aaif-create-chapter/scripts/create_chapter.py b/skills/aaif-create-chapter/scripts/create_chapter.py index 38673cf..56448ae 100755 --- a/skills/aaif-create-chapter/scripts/create_chapter.py +++ b/skills/aaif-create-chapter/scripts/create_chapter.py @@ -802,6 +802,45 @@ def main(): # survivor is a PII copy on disk, so say so rather than hide it. print("WARNING: could not remove temp dir %s — it holds downloaded " "workbooks; delete it by hand." % ctx["tmp"], file=sys.stderr) + # The clone copied TemplateCity's Web Banner.png, an image that says "San + # Francisco" and that the text rebrand cannot reach. Re-render it from this + # chapter's own (rebranded) Square Logo deck — unless a fresh file still + # carries source tokens, in which case the deck may too, and the exit-1 + # path below is the real problem. A banner failure is not a failed clone: + # it is "done, one follow-up", exit 2, with the command that finishes it. + import render_web_banner as rwb # lazy: it imports this module + banner_failed = False + dirty_deck = [fn for fn, _t in ctx["existing_residuals"] if fn == rwb.DECK] + if ctx["residuals"]: + print("\n%s not rendered: fresh files still carry source tokens (below)." + % rwb.BANNER) + elif dirty_deck: + # Under --resume an existing deck still naming the template city is + # left alone on purpose; rendering it would only be refused. + banner_failed = True + print("%s not rendered: the existing %s still carries source tokens. After " + "--write --resume --repair-existing, run: %s" + % (rwb.BANNER, rwb.DECK, rwb.fallback_command(name, create=True)), + file=sys.stderr) + else: + r = rwb.sync_one(new_id, name, create=True) + if r.error is not None: + banner_failed = True + print("WARNING: %s was NOT re-rendered (%s) — it still shows the template " + "city. Run: %s" % (rwb.BANNER, r.error, + rwb.fallback_command(name, create=True)), + file=sys.stderr) + elif r.action == rwb.CURRENT: + print("%s already current." % rwb.BANNER) + elif r.action == rwb.RENDER: + print("Rendered %s from this chapter's %s." % (rwb.BANNER, rwb.DECK)) + else: + # Anything else (ABSENT should be impossible with create=True) is + # not a render, and saying "Rendered" would hide it. + banner_failed = True + print("WARNING: %s: unexpected outcome %r, nothing rendered. Run: %s" + % (rwb.BANNER, r.action, rwb.fallback_command(name, create=True)), + file=sys.stderr) print("\nDone. New chapter folder id: %s" % new_id) print("https://drive.google.com/drive/folders/%s" % new_id) if status == "absent": @@ -829,6 +868,8 @@ def main(): "--resume --repair-existing for design assets, fix CRM/Tracker by hand" % len(ctx["existing_residuals"]), file=sys.stderr) sys.exit(2) + if banner_failed: + sys.exit(2) if __name__ == "__main__": main() diff --git a/skills/aaif-create-chapter/scripts/rename_chapter.py b/skills/aaif-create-chapter/scripts/rename_chapter.py index 0e46866..90b1579 100755 --- a/skills/aaif-create-chapter/scripts/rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/rename_chapter.py @@ -439,12 +439,15 @@ def main(): print("VERIFY FAILED — still carrying the old name, or not inspected:") for x in left: print(" " + x) + import render_web_banner + print("%s was NOT re-rendered either. After fixing the files: %s" + % (render_web_banner.BANNER, render_web_banner.fallback_command(a.new))) return 1 print("Verified: no file name, and no text in any %s, still says %r%s." % ("/".join(OFFICE), a.old, " (native Google files and member data were not inspected)" if (native or skipped) else "")) - return 0 + return rerender_banner(folder_id, a.new) finally: # Member data on disk, cleaned up. The report path used to leave every # chapter workbook in a predictable world-readable /tmp directory. @@ -452,5 +455,34 @@ def main(): shutil.rmtree(scan, ignore_errors=True) +def rerender_banner(folder_id, new_name): + """Re-render Web Banner.png after a verified rename; returns the exit code. + + The banner has the city drawn into the image, which no text pass reaches. + It is rendered from the Square Logo deck, and its stamp carries the + chapter name, so a new name re-renders it even when the deck itself did + not change. A folder with no banner is left without one, as in a sweep.""" + import render_web_banner + r = render_web_banner.sync_one(folder_id, new_name, create=False) + if r.error is not None: + print("WARNING: %s still shows the old name (%s). Run: %s" + % (render_web_banner.BANNER, r.error, + render_web_banner.fallback_command(new_name)), file=sys.stderr) + return 1 + msg = {render_web_banner.RENDER: "%s rendered from the renamed %s." + % (render_web_banner.BANNER, render_web_banner.DECK), + render_web_banner.CURRENT: "%s already current." % render_web_banner.BANNER, + render_web_banner.ABSENT: "This chapter has no %s; none was added." + % render_web_banner.BANNER}.get(r.action) + if msg is None: + # The rename has landed; an outcome we do not know is not "done". + print("WARNING: %s: unexpected outcome %r, nothing rendered. Run: %s" + % (render_web_banner.BANNER, r.action, + render_web_banner.fallback_command(new_name)), file=sys.stderr) + return 1 + print(msg) + return 0 + + if __name__ == "__main__": sys.exit(main()) diff --git a/skills/aaif-create-chapter/scripts/render_web_banner.py b/skills/aaif-create-chapter/scripts/render_web_banner.py new file mode 100755 index 0000000..13411d9 --- /dev/null +++ b/skills/aaif-create-chapter/scripts/render_web_banner.py @@ -0,0 +1,352 @@ +#!/usr/bin/env python3 +"""Render each chapter's `Web Banner.png` from its own Square Logo deck. + +`Web Banner.png` sits at the root of every chapter folder: the AAIF mark over +"AAIF Community Events / ". It is an image, so `create_chapter.py`'s +rebrand — which rewrites OOXML text — cannot reach it, and the clone copied +TemplateCity's PNG byte for byte. Found 2026-11-03: every chapter cloned before +then (37 of them) had a "San Francisco" banner, and every renamed chapter's +still showed its old name. + +The same composition already exists as text, which the rebrand does reach: +slide 4 of `Banners (…)/Square Logo.pptx`, a 2000 x 2000 square like the PNG it +replaces. So the banner is not drawn here at all; it is that slide, rendered by +Google's own Slides renderer (`aaif_events.slides_export.render_slide_png`, +never LibreOffice), and so it is correct whenever the deck is. + +Guards: + + * **The slide must say exactly "AAIF Community Events" and the chapter's + name.** Rendering a deck that names another city would publish the wrong + name with a fresh timestamp on it. The check is exact, not a substring + test, so "Portland, OR" does not pass for Portland and a slide still + carrying a leftover city fails. ("Exact" ignores only case, runs of + whitespace, and punctuation at either end of the city.) It runs twice: on the deck's own slide 4 + (resolved through `presentation.xml`'s slide order, not the `slideN.xml` + part name) before anything is written, and again by the renderer on the + page it is about to draw. + * **A banner records what it was rendered from.** The upload stamps + `appProperties.aaifBannerDeck` with the deck's `md5Checksum` AND the + chapter name. A re-run skips a chapter whose stamp matches, so it is cheap + to re-run; a deck that `rename_chapter.py` rewrote, or a folder renamed by + hand, no longer matches and is checked again. (Renders are not byte-stable, + so comparing PNG bytes would re-upload every banner on every run.) Drive + scopes `appProperties` to the OAuth client that wrote them, so an operator + on a different `gws` client sees no stamps and re-renders everything once: + slower, never wrong. + * **A stray temp copy is a failure.** The renderer's throwaway Slides copy + lands beside the deck; if it cannot be trashed, that chapter FAILS and its + FAILED line names the copy's id, whether or not the render itself worked. + +A sweep skips TemplateCity (its deck says San Francisco by design, and a stray +temp copy there would be cloned into every later chapter) unless it is named +with `--chapter`, and never creates a banner in a folder that has none. +`--chapter X --create` does, for the one case that needs it: a new chapter +whose create-hook render failed before its banner file existed. + +Read-only by default. Exit codes: 0 otherwise (folders with no banner are +listed, not failed), 2 report-only with banners to render, 1 any failure. + +Usage: + python3 render_web_banner.py # report + python3 render_web_banner.py --write + python3 render_web_banner.py --chapter Aarhus --write + python3 render_web_banner.py --chapter Aarhus --create --write +""" +import argparse +import html +import os +import posixpath +import re +import shlex +import sys +import tempfile +import zipfile +from concurrent.futures import ThreadPoolExecutor +from typing import NamedTuple, Optional + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import create_chapter as cc # noqa: E402 (puts lib on the path) +from aaif_events import gws as gwsmod # noqa: E402 +from aaif_events import slides_export # noqa: E402 + +BANNER = "Web Banner.png" +DECK = "Square Logo.pptx" +#: The banners subfolder is "Banners (Chapter Specific, Changed Rarely)"; match +#: the stem so a reworded parenthetical does not hide it. +BANNERS_DIR = re.compile(r"^Banners\b") +#: 0-based: the fourth slide in presentation order is the "AAIF Community +#: Events / " square. +SLIDE_INDEX = 3 +HEADLINE = "AAIF Community Events" +#: The stamp records the deck and the name, not how it was drawn. Change +#: SLIDE_INDEX or the render size and you must also rename this key (say to +#: "aaifBannerDeck2"), or every banner stays "current" in the old rendering. +STAMP = "aaifBannerDeck" +PNG = "image/png" +TEMPLATE = "TemplateCity" +#: Folder name -> the name its deck is expected to show, where they differ. +#: TemplateCity's deck carries the rebrand source city by design. +DECK_NAME = {TEMPLATE: cc.SOURCE_NAME} +#: Absolute, so the printed fallback command works from an installed plugin. +SCRIPT = os.path.abspath(__file__) + +CURRENT, RENDER, ABSENT = "current", "render", "absent" + + +class Result(NamedTuple): + """One chapter's outcome. Exactly one of `action` / `error` is set; + callers treat any other shape as a failure.""" + name: str + #: CURRENT (stamp matched, nothing done), RENDER (rendered under + #: write=True, would render under write=False), or ABSENT (no banner, and + #: not asked to create one). + action: Optional[str] = None + error: Optional[str] = None + + +def _norm(s): + return re.sub(r"\s+", " ", s).strip().casefold() + + +def _edges(s): + """Strip punctuation from both ends (the separator after the headline, a + display full stop after the city).""" + return re.sub(r"^[\W_]+|[\W_]+$", "", s) + + +def fallback_command(name, create=False): + """The command that finishes a failed hook render, shell-quoted.""" + return "python3 %s --chapter %s%s --write" % ( + shlex.quote(SCRIPT), shlex.quote(name), " --create" if create else "") + + +def expected_name(folder): + return DECK_NAME.get(folder, folder) + + +def stamp_value(md5, want): + return "%s|%s" % (md5, _norm(want)) + + +def name_mismatch(text, want): + """None when `text` is exactly the headline plus `want`, else the reason. + + Exact rather than "contains": a slide saying "Portland, OR" must not pass + for Portland, nor one still carrying the template city beside the new one. + Case and runs of whitespace are ignored, and so is punctuation at either + end of the city, on both sides of the comparison: a separator after the + headline, a display full stop after the city, or a name like "D.C.".""" + t, head = _norm(text), _norm(HEADLINE) + if not t.startswith(head): + return "slide %d does not start with %r (it says %r)" % (SLIDE_INDEX + 1, HEADLINE, text[:80]) + rest = _edges(t[len(head):]) + if rest != _edges(_norm(want)): + return ("slide %d says %r, expected %r — fix the deck, or map the folder in " + "DECK_NAME if the deck is right" % (SLIDE_INDEX + 1, rest, want)) + return None + + +def slide_part(z, index=SLIDE_INDEX): + """The zip part of the `index`-th slide (0-based) in presentation order. + + Order lives in `presentation.xml`'s ``, not in the part names: + a reordered deck keeps `slide4.xml` wherever the slide now sits, while the + renderer draws the fourth slide in order.""" + pres_xml = z.read("ppt/presentation.xml").decode("utf-8", "replace") + rids = re.findall(r']*\br:id="([^"]+)"', pres_xml) + if index >= len(rids): + raise RuntimeError("deck has %d slide(s); slide %d is needed" % (len(rids), index + 1)) + rels = z.read("ppt/_rels/presentation.xml.rels").decode("utf-8", "replace") + for rel in re.findall(r"]*>", rels): + if re.search(r'\bId="%s"' % re.escape(rids[index]), rel): + target = re.search(r'\bTarget="([^"]+)"', rel).group(1) + if target.startswith("/"): # package-absolute + return target[1:] + return posixpath.normpath(posixpath.join("ppt", target)) + raise RuntimeError("slide %d's relationship %s is missing" % (index + 1, rids[index])) + + +_RUN = re.compile(r"]*)?>([^<]*)") +_PARA = re.compile(r"]*)?>(.*?)", re.S) + + +def slide_text(pptx_path, index=SLIDE_INDEX): + """The text of the `index`-th slide (presentation order), unescaped. + + Runs within a paragraph are joined with nothing, since an edit can split + one word across runs ("Amster" + "dam"). Paragraphs are joined with a + space.""" + with zipfile.ZipFile(pptx_path) as z: + xml = z.read(slide_part(z, index)).decode("utf-8", "replace") + return " ".join("".join(html.unescape(r) for r in _RUN.findall(p)) + for p in _PARA.findall(xml)) + + +def chapters(include_template=False): + """[(id, name)] for every chapter folder under Chapters, sorted.""" + return sorted(((c["id"], c["name"]) for c in cc.list_children(cc.CHAPTERS_PARENT) + if c["mimeType"] == cc.FOLDER + and (include_template or c["name"] != TEMPLATE)), + key=lambda t: t[1]) + + +def _find(children, pred, what): + hits = [c for c in children if pred(c)] + if len(hits) > 1: + raise RuntimeError("%d copies of %s (ids %s) — trash the extra one(s)" + % (len(hits), what, ", ".join(h["id"] for h in hits))) + return hits[0] if hits else None + + +def sync_chapter(chapter_id, name, write, tmpdir, create=False): + """Bring one chapter's banner up to date. Returns a `Result`; never raises + an Exception, because this runs under a thread pool and one chapter's + failure must not end the others. + + A banner is created in a folder that has none only when `create` is set + (the create hook, or `--create`). Otherwise such a folder is ABSENT, and + that is decided first, before the Banners folder or deck is looked up: the + folders without a banner predate the template that carries it (the + Tatooine test chapter), may lack the deck too, and are not failures.""" + created = None + try: + want = expected_name(name) + kids = cc.list_children(chapter_id) + banner = _find(kids, lambda c: c["name"] == BANNER, BANNER) + if banner is None and not create: + return Result(name, ABSENT) + bdir = _find(kids, lambda c: c["mimeType"] == cc.FOLDER + and BANNERS_DIR.match(c["name"]), "the Banners folder") + if bdir is None: + return Result(name, error="no Banners folder") + deck = _find(cc.list_children(bdir["id"]), lambda c: c["name"] == DECK, DECK) + if deck is None: + return Result(name, error="no %s in %s" % (DECK, bdir["name"])) + md5 = cc.gws_json("drive", "files", "get", params={ + "fileId": deck["id"], "supportsAllDrives": True, + "fields": "md5Checksum"}).get("md5Checksum") + if not md5: + # Without this, a None checksum would compare equal to an unstamped + # banner's missing stamp and report it current forever. + return Result(name, error="%s has no checksum (not a stored .pptx?)" % DECK) + stamp = stamp_value(md5, want) + + if banner is not None: + props = cc.gws_json("drive", "files", "get", params={ + "fileId": banner["id"], "supportsAllDrives": True, + "fields": "appProperties"}).get("appProperties") or {} + if props.get(STAMP) == stamp: + return Result(name, CURRENT) + + local = os.path.join(tmpdir, "%s.pptx" % chapter_id) + cc.gws_download(deck["id"], local) + try: + why = name_mismatch(slide_text(local), want) + finally: + os.remove(local) + if why: + return Result(name, error=why) + if not write: + return Result(name, RENDER) + + png = os.path.join(tmpdir, "%s.png" % chapter_id) + try: + slides_export.render_slide_png( + deck["id"], png, slide_index=SLIDE_INDEX, strict_cleanup=True, + check_text=lambda text: name_mismatch(text, want)) + if banner is None: + # NO_RETRY: a create that landed but answered like a timeout + # would, re-sent, leave two banners in the folder. + banner = created = cc.gws_json( + "drive", "files", "create", retries=gwsmod.NO_RETRY, + params={"supportsAllDrives": True, "fields": "id"}, + body={"name": BANNER, "parents": [chapter_id], "mimeType": PNG}) + cc.gws_upload(banner["id"], png, PNG) + # Stamp last: a banner whose upload failed must not be marked current. + cc.gws_json("drive", "files", "update", + params={"fileId": banner["id"], "supportsAllDrives": True}, + body={"appProperties": {STAMP: stamp}}) + finally: + if os.path.exists(png): + os.remove(png) + return Result(name, RENDER) + except Exception as e: + msg = "%s: %s" % (type(e).__name__, str(e)[:400]) + if created is not None: + msg = ("created an EMPTY %s (id %s) but did not finish: %s — re-run to " + "fill it" % (BANNER, created["id"], msg)) + return Result(name, error=msg) + + +def sync_one(chapter_id, name, create): + """Render one chapter's banner now (the create/rename hooks). Always + writes. Returns the `Result`, so a caller can tell a real render from an + already-current one.""" + with tempfile.TemporaryDirectory() as tmp: + return sync_chapter(chapter_id, name, True, tmp, create=create) + + +def _positive(v): + n = int(v) + if n < 1: + raise argparse.ArgumentTypeError("must be at least 1") + return n + + +def main(argv=None): + ap = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--write", action="store_true", + help="render and upload (default: report only)") + ap.add_argument("--chapter", help="only this chapter (exact name, case-insensitive; " + "the only way to include TemplateCity)") + ap.add_argument("--create", action="store_true", + help="with --chapter: create the banner if the folder has none") + ap.add_argument("--jobs", type=_positive, default=4) + a = ap.parse_args(argv) + if a.create and not a.chapter: + ap.error("--create needs --chapter (a sweep never adds a banner)") + + todo = chapters(include_template=bool(a.chapter)) + if a.chapter: + todo = [(i, n) for i, n in todo if n.lower() == a.chapter.lower()] + if not todo: + print("no chapters matched.") + return 1 + print("%d chapter(s).%s\n" % (len(todo), "" if a.write + else " REPORT ONLY — nothing will be written.")) + pending, current, failed, absent = [], 0, [], [] + with tempfile.TemporaryDirectory() as tmp, \ + ThreadPoolExecutor(max_workers=a.jobs) as pool: + for r in pool.map(lambda t: sync_chapter(t[0], t[1], a.write, tmp, + create=a.create), todo): + if r.error is not None: + failed.append(r) + print(" FAILED %-26s %s" % (r.name, r.error)) + elif r.action == RENDER: + pending.append(r.name) + print(" %s %s" % ("rendered " if a.write else "would render", r.name)) + elif r.action == ABSENT: + absent.append(r.name) + elif r.action == CURRENT: + current += 1 + else: + # An outcome this loop does not know is not "clean". + failed.append(r) + print(" FAILED %-26s unknown outcome %r" % (r.name, r.action)) + print("\n%d %s, %d already current, %d failed." + % (len(pending), "rendered" if a.write else "to render", current, len(failed))) + if absent: + print("No %s at all (left alone; a sweep never adds one): %s" + % (BANNER, ", ".join(sorted(absent)))) + if failed: + return 1 + if pending and not a.write: + print("Re-run with --write to apply.") + return 2 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/skills/aaif-create-chapter/scripts/test_create_chapter.py b/skills/aaif-create-chapter/scripts/test_create_chapter.py index 3616d1e..2176211 100644 --- a/skills/aaif-create-chapter/scripts/test_create_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_create_chapter.py @@ -12,6 +12,7 @@ sys.path.insert(0, os.path.dirname(__file__)) import create_chapter as cc # noqa: E402 +import render_web_banner # noqa: E402 from aaif_events import gws as gwsmod # noqa: E402 A = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"' @@ -722,7 +723,7 @@ class TestMainGuards(unittest.TestCase): """Argument checks in main(), the plan-by-default / tempdir contract of a --write run, and which invocations consult the 100-chapter cap.""" - def run_main(self, argv, clone=None, cap_calls=None): + def run_main(self, argv, clone=None, cap_calls=None, banner=None): """Drive main() with every external call stubbed. `assert_under_cap` joined that list when the 100-chapter cap landed: it @@ -739,9 +740,91 @@ def run_main(self, argv, clone=None, cap_calls=None): mock.patch.object(cc, "resolve_latlon", lambda *a: (1.0, 2.0)), \ mock.patch.object(cc._chapters, "assert_under_cap", lambda what="", **k: calls.append(what)), \ - mock.patch.object(cc, "clone_and_rebrand", clone): + mock.patch.object(cc, "clone_and_rebrand", clone), \ + mock.patch.object(render_web_banner, "sync_one", + banner or (lambda fid, name, create: + render_web_banner.Result(name, render_web_banner.RENDER))): cc.main() + def test_a_write_re_renders_the_new_chapters_banner(self): + """The clone copies TemplateCity's Web Banner.png, which says "San + Francisco"; the text rebrand cannot reach an image.""" + seen = [] + + def banner(fid, name, create): + seen.append((fid, name, create)) + return render_web_banner.Result(name, render_web_banner.RENDER) + with mock.patch("sys.stdout"): + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=banner) + self.assertEqual(seen, [("new-id", "Zed", True)]) + + def test_a_failed_banner_render_warns_and_exits_2(self): + """Done with one follow-up, like the existing-residuals path: a wrapper + checking the exit code must not read a San Francisco banner as success.""" + import io + err = io.StringIO() + with mock.patch("sys.stdout"), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name, create: render_web_banner.Result( + name, error="slide 4 says the wrong city")) + self.assertEqual(cm.exception.code, 2) + self.assertIn("NOT re-rendered", err.getvalue()) + self.assertIn("render_web_banner.py --chapter Zed --create --write", err.getvalue()) + + def test_an_already_current_banner_does_not_claim_a_render(self): + import io + out = io.StringIO() + with mock.patch("sys.stdout", out): + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name, create: render_web_banner.Result( + name, render_web_banner.CURRENT)) + self.assertIn("already current", out.getvalue()) + self.assertNotIn("Rendered", out.getvalue()) + + def test_dirty_fresh_files_skip_the_banner(self): + """A deck still carrying source tokens could be rendered as-is.""" + def dirty_clone(_tpl, _parent, _name, ctx, existing_id=None): + ctx["residuals"].append(("Square Logo.pptx", ["San Francisco"])) + return "new-id" + with mock.patch("sys.stdout"), self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=dirty_clone, + banner=lambda *a, **k: self.fail("must not render a dirty deck")) + # The residuals exit (a message, i.e. 1), not the banner's 2. + self.assertIsInstance(cm.exception.code, str) + + def test_a_dirty_existing_deck_under_resume_is_not_rendered(self): + """It is left dirty on purpose; the advice must come after the repair.""" + import io + + def resumed(_tpl, _parent, _name, ctx, existing_id=None): + ctx["existing_residuals"].append(("Square Logo.pptx", ["San Francisco"])) + return "new-id" + err = io.StringIO() + with mock.patch("sys.stdout"), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=resumed, + banner=lambda *a, **k: self.fail("must not render a dirty deck")) + self.assertEqual(cm.exception.code, 2) + self.assertIn("--repair-existing, run:", err.getvalue()) + + def test_an_unexpected_outcome_is_not_reported_as_a_render(self): + import io + out, err = io.StringIO(), io.StringIO() + with mock.patch("sys.stdout", out), mock.patch("sys.stderr", err), \ + self.assertRaises(SystemExit) as cm: + self.run_main(["--city", "Zed", "--write"], clone=lambda *a, **k: "new-id", + banner=lambda fid, name, create: render_web_banner.Result( + name, render_web_banner.ABSENT)) + self.assertEqual(cm.exception.code, 2) + self.assertNotIn("Rendered", out.getvalue()) + self.assertIn("unexpected outcome", err.getvalue()) + + def test_a_plan_run_renders_no_banner(self): + self.run_main(["--city", "Zed"], + banner=lambda *a, **k: self.fail("plan must not render")) + def test_plan_runs_never_consult_the_cap(self): """Seeing what a chapter WOULD look like is how someone decides which existing one to retire, so refusing the plan would remove the tool they diff --git a/skills/aaif-create-chapter/scripts/test_rename_chapter.py b/skills/aaif-create-chapter/scripts/test_rename_chapter.py index 62844f2..2ef30c1 100755 --- a/skills/aaif-create-chapter/scripts/test_rename_chapter.py +++ b/skills/aaif-create-chapter/scripts/test_rename_chapter.py @@ -225,6 +225,79 @@ def zipped(parts): finally: _redact.REDACT = False +# --- the banner hook after a verified rename -------------------------------- +import io as _io # noqa: E402 +from unittest import mock as _mock # noqa: E402 +import render_web_banner as _rwb # noqa: E402 + + +def _rerender(result): + seen, err, out = [], _io.StringIO(), _io.StringIO() + + def fake(fid, name, create): + seen.append((fid, name, create)) + return result + with _mock.patch.object(_rwb, "sync_one", fake), \ + _mock.patch("sys.stderr", err), _mock.patch("sys.stdout", out): + code = rc.rerender_banner("folder-1", "Edinburgh") + return code, seen, err.getvalue(), out.getvalue() + + +code, seen, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.RENDER)) +check("banner hook gets the NEW name and never creates a banner", + seen, [("folder-1", "Edinburgh", False)]) +check("...a render exits 0", code, 0) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", error="slide 4 says Scotland")) +check("a failed banner render exits 1", code, 1) +check("...with the command on stderr", + "--chapter Edinburgh --write" in err and "slide 4 says Scotland" in err, True) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.CURRENT)) +check("an already-current banner exits 0 and does not claim a render", + (code, "already current" in out, "rendered" in out), (0, True, False)) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh", _rwb.ABSENT)) +check("a folder with no banner exits 0 and says none was added", + (code, "none was added" in out), (0, True)) +code, _s, err, out = _rerender(_rwb.Result("Edinburgh")) +check("an unknown outcome exits 1 with a warning, not a traceback", + (code, "unexpected outcome" in err), (1, True)) + + +# --- main() wires the hook: only after a verified --write --------------------- +def _main(argv, verify_names): + """Drive rename_chapter.main() over a fake folder holding one non-Office + file. `verify_names` is what the re-read walk sees after the rename.""" + import tempfile as _tf + walks = [[("Scotland Notes.txt", "f1", "Scotland Notes.txt", False)], + [(n, "f1", n, False) for n in verify_names]] + banner, out = [], _io.StringIO() + + def fake_sync(fid, name, create): + banner.append((fid, name, create)) + return _rwb.Result(name, _rwb.RENDER) + with _tf.TemporaryDirectory() as tmp, \ + _mock.patch.object(sys, "argv", ["x"] + argv), \ + _mock.patch.object(rc, "find_folder", lambda name: "folder-1"), \ + _mock.patch.object(rc, "walk", lambda fid: iter(walks.pop(0))), \ + _mock.patch.object(rc, "rename_file", lambda fid, new: None), \ + _mock.patch.object(rc.datadir, "path", lambda *p: os.path.join(tmp, *p)), \ + _mock.patch.object(_rwb, "sync_one", fake_sync), \ + _mock.patch("sys.stdout", out), _mock.patch("sys.stderr", _io.StringIO()): + code = rc.main() + return code, banner, out.getvalue() + + +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh", "--write"], + ["Edinburgh Notes.txt"]) +check("a verified write re-renders the banner under the NEW name, never creating", + (code, banner), (0, [("folder-1", "Edinburgh", False)])) +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh", "--write"], + ["Scotland Notes.txt"]) +check("a failed verify skips the banner and prints the command to run after", + (code, banner, "render_web_banner.py --chapter Edinburgh --write" in out), + (1, [], True)) +code, banner, out = _main(["--from", "Scotland", "--to", "Edinburgh"], []) +check("a report run never renders", (code, banner), (2, [])) + print() print("FAILED %d check(s)" % len(FAILS) if FAILS else "rename_chapter: all checks passed.") diff --git a/skills/aaif-create-chapter/scripts/test_render_web_banner.py b/skills/aaif-create-chapter/scripts/test_render_web_banner.py new file mode 100755 index 0000000..c74cd44 --- /dev/null +++ b/skills/aaif-create-chapter/scripts/test_render_web_banner.py @@ -0,0 +1,450 @@ +#!/usr/bin/env python3 +"""Unit tests for rendering each chapter's Web Banner from its Square Logo deck. + +The properties pinned here are the ones that fail quietly: + + (a) **a slide that does not say exactly the chapter's name is never + rendered** — another city, a qualified spelling, a leftover template city + beside the new one. Rendering it would publish the wrong name with a + fresh date on it. + (b) **the slide checked is the slide rendered**: the fourth in presentation + order, not the `slide4.xml` part. + (c) **a banner stamped with this deck AND this name is skipped**, and a moved + deck or a new name is not — otherwise a re-run re-renders the estate, or + a rename never reaches the banner. + (d) **the upload and the stamp land on the banner, never the deck**, and the + stamp only after the upload. + (e) **a chapter with no banner gets one created exactly once** (NO_RETRY), + and only when asked; a sweep never adds one. + (f) `main()`'s exit codes: 1 on any failure, 2 for report-only with work, 0 + otherwise; ABSENT is not a failure, an unknown outcome is. `main()` + passes `write` through, so the default run stays read-only. + (g) a folder with no banner is ABSENT before anything else is looked up; a + missing Banners folder or deck under an existing banner is a failure. + +Nothing touches Drive: `create_chapter`'s Drive calls and the renderer are +replaced with a fake folder tree. + +Run: python3 skills/aaif-create-chapter/scripts/test_render_web_banner.py +""" +import io +import os +import sys +import tempfile +import unittest +import zipfile +from unittest import mock + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import render_web_banner as rwb # noqa: E402 +import create_chapter as cc # noqa: E402 +from aaif_events import gws as gwsmod # noqa: E402 + +BANNERS = "Banners (Chapter Specific, Changed Rarely)" + + +def _pptx(fourth_runs, order=(1, 2, 3, 4, 5), part_runs=None, absolute=False): + """Bytes of a minimal .pptx. `order` lists slide part numbers in + presentation order; the part shown FOURTH gets `fourth_runs`, unless + `part_runs` maps part numbers to runs explicitly. Each entry is one + paragraph; a list entry is one paragraph split into several runs. + `absolute` writes package-absolute relationship targets.""" + if part_runs is None: + part_runs = {order[3]: fourth_runs} + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w") as z: + ids = "".join('' % (255 + n, n) for n in order) + z.writestr("ppt/presentation.xml", + "%s" % ids) + rels = "".join('' + % (n, "/ppt/" if absolute else "", n) for n in order) + z.writestr("ppt/_rels/presentation.xml.rels", "%s" % rels) + for n in order: + paras = part_runs.get(n, ["AAIF"]) + body = "" + for i, para in enumerate(paras): + runs = para if isinstance(para, list) else [para] + body += "%s" % "".join( + '%s' % r + if i % 2 else "%s" % r for r in runs) + z.writestr("ppt/slides/slide%d.xml" % n, "%s" % body) + return buf.getvalue() + + +def _path(data): + fh = tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) + fh.write(data) + fh.close() + return fh.name + + +class FakeDrive: + """One chapter folder, `boston`, with a Banners folder and a deck.""" + + def __init__(self, runs, stamp=None, banner=True, md5="m1", banners=1, + bdir=True, decks=1): + self.deck = _pptx(runs) + self.bdir = bdir + self.decks = decks + self.md5 = md5 + self.stamp = stamp + self.banners = banners if banner else 0 + self.calls = [] # (verb, fileId, retries, body) + self.renders = [] + self.render_check = None + self.fail_upload = False + + def list_children(self, fid): + if fid == "boston": + kids = [{"id": "bdir", "name": BANNERS, "mimeType": cc.FOLDER}] if self.bdir else [] + kids.append({"id": "other", "name": "Event Template", "mimeType": cc.FOLDER}) + kids += [{"id": "wb" if i == 0 else "wb%d" % i, "name": rwb.BANNER, + "mimeType": "image/png"} for i in range(self.banners)] + return kids + if fid == "bdir": + return [{"id": "deck" if i == 0 else "deck%d" % i, "name": rwb.DECK, + "mimeType": "x"} for i in range(self.decks)] + return [] + + def gws_json(self, *verb, params=None, body=None, retries=None): + fid = (params or {}).get("fileId") + self.calls.append((verb[-1], fid, retries, body)) + if verb[-1] == "get" and fid == "deck": + return {"md5Checksum": self.md5} if self.md5 else {} + if verb[-1] == "get" and fid == "wb": + return {"appProperties": {rwb.STAMP: self.stamp} if self.stamp else {}} + if verb[-1] == "create": + self.banners = 1 + return {"id": "wb"} + if verb[-1] == "update": + if fid != "wb": + raise AssertionError("stamp written to %r, not the banner" % fid) + self.stamp = body["appProperties"][rwb.STAMP] + return {} + raise AssertionError("unexpected call %r %r" % (verb, params)) + + def download(self, fid, out): + assert fid == "deck" + with open(out, "wb") as fh: + fh.write(self.deck) + + def upload(self, fid, path, mime): + if fid != "wb": + raise AssertionError("PNG uploaded over %r, not the banner" % fid) + if self.fail_upload: + raise RuntimeError("upload blew up") + self.calls.append(("upload", fid, None, mime)) + + def render(self, fid, out, slide_index=0, check_text=None, strict_cleanup=False): + assert (fid, slide_index, strict_cleanup) == ("deck", rwb.SLIDE_INDEX, True) + self.render_check = check_text + self.renders.append(fid) + with open(out, "wb") as fh: + fh.write(b"\x89PNG fake") + return out + + def verbs(self): + return [c[0] for c in self.calls] + + +def run(fake, write=True, name="Boston", create=False): + with mock.patch.object(cc, "list_children", fake.list_children), \ + mock.patch.object(cc, "gws_json", fake.gws_json), \ + mock.patch.object(cc, "gws_download", fake.download), \ + mock.patch.object(cc, "gws_upload", fake.upload), \ + mock.patch.object(rwb.slides_export, "render_slide_png", fake.render), \ + tempfile.TemporaryDirectory() as tmp: + return rwb.sync_chapter("boston", name, write, tmp, create=create) + + +class TestNameCheck(unittest.TestCase): + def test_exactly_the_headline_and_the_name_passes(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events new york", "New York")) + + def test_a_trailing_full_stop_is_ignored(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events Japan.", "Japan")) + + def test_another_city_is_refused(self): + self.assertIn("san francisco", + rwb.name_mismatch("AAIF Community Events San Francisco", "Boston")) + + def test_a_qualified_spelling_is_not_a_match(self): + self.assertIsNotNone(rwb.name_mismatch("AAIF Community Events Portland, OR", "Portland")) + self.assertIsNotNone(rwb.name_mismatch("AAIF Community Events New York", "York")) + + def test_a_leftover_template_city_beside_the_new_one_is_refused(self): + self.assertIsNotNone(rwb.name_mismatch( + "AAIF Community Events Boston San Francisco", "Boston")) + + def test_a_slide_without_the_headline_is_refused(self): + self.assertIn("does not start", rwb.name_mismatch("Boston", "Boston")) + + def test_punctuation_at_the_end_of_the_wanted_name_is_ignored_too(self): + self.assertIsNone(rwb.name_mismatch("AAIF Community Events Washington D.C.", + "Washington D.C.")) + + def test_templatecity_expects_the_rebrand_source_city(self): + self.assertEqual(rwb.expected_name("TemplateCity"), cc.SOURCE_NAME) + self.assertEqual(rwb.expected_name("Boston"), "Boston") + + +class TestSlideText(unittest.TestCase): + def test_runs_with_and_without_attributes_are_read(self): + p = _path(_pptx(["AAIF Community Events", "New York"])) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events New York") + + def test_the_fourth_slide_in_presentation_order_not_the_part_name(self): + # Reordered deck: part slide4.xml now sits second; slide2.xml is fourth. + p = _path(_pptx(None, order=(1, 4, 3, 2, 5), part_runs={ + 4: ["AAIF Community Events", "San Francisco"], + 2: ["AAIF Community Events", "Boston"]})) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Boston") + + def test_xml_entities_are_unescaped(self): + p = _path(_pptx(["AAIF Community Events", "Trinidad & Tobago"])) + self.addCleanup(os.remove, p) + self.assertIsNone(rwb.name_mismatch(rwb.slide_text(p), "Trinidad & Tobago")) + + def test_a_word_split_across_runs_is_read_whole(self): + p = _path(_pptx(["AAIF Community Events", ["Amster", "dam"]])) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Amsterdam") + + def test_package_absolute_slide_targets_resolve(self): + p = _path(_pptx(["AAIF Community Events", "Boston"], absolute=True)) + self.addCleanup(os.remove, p) + self.assertEqual(rwb.slide_text(p), "AAIF Community Events Boston") + + def test_a_short_deck_says_so(self): + p = _path(_pptx(None, order=(1, 2), part_runs={})) + self.addCleanup(os.remove, p) + with self.assertRaisesRegex(RuntimeError, "2 slide"): + rwb.slide_text(p) + + +class TestSync(unittest.TestCase): + OK = ["AAIF Community Events", "Boston"] + + def test_a_deck_naming_another_city_is_never_rendered(self): + fake = FakeDrive(["AAIF Community Events", "San Francisco"]) + r = run(fake) + self.assertIn("expected 'Boston'", r.error) + self.assertEqual(fake.renders, []) + self.assertNotIn("upload", fake.verbs()) + + def test_a_banner_stamped_with_this_deck_and_name_is_skipped(self): + fake = FakeDrive(self.OK, stamp=rwb.stamp_value("m1", "Boston")) + self.assertEqual(run(fake), rwb.Result("Boston", rwb.CURRENT)) + self.assertEqual(fake.renders, []) + + def test_a_new_name_with_an_unchanged_deck_is_checked_again(self): + """A folder renamed by hand: the deck md5 did not move.""" + fake = FakeDrive(["AAIF Community Events", "Scotland"], + stamp=rwb.stamp_value("m1", "Scotland")) + r = run(fake, name="Edinburgh") + self.assertIn("expected 'Edinburgh'", r.error) + self.assertEqual(fake.renders, []) + + def test_a_moved_deck_is_re_rendered_onto_the_banner_and_restamped(self): + fake = FakeDrive(self.OK, stamp=rwb.stamp_value("old", "Boston"), md5="new") + self.assertEqual(run(fake).action, rwb.RENDER) + self.assertEqual(fake.renders, ["deck"]) + self.assertEqual([c for c in fake.calls if c[0] == "upload"], + [("upload", "wb", None, "image/png")]) + self.assertEqual(fake.stamp, rwb.stamp_value("new", "Boston")) + self.assertLess(fake.verbs().index("upload"), fake.verbs().index("update")) + + def test_the_renderer_rechecks_the_page_it_draws(self): + fake = FakeDrive(self.OK) + run(fake) + self.assertIsNone(fake.render_check("AAIF Community Events Boston")) + self.assertIsNotNone(fake.render_check("AAIF Community Events Oslo")) + + def test_report_mode_renders_and_writes_nothing(self): + fake = FakeDrive(self.OK) + self.assertEqual(run(fake, write=False).action, rwb.RENDER) + self.assertEqual(fake.renders, []) + self.assertFalse({"create", "update", "upload"} & set(fake.verbs())) + + def test_a_failed_upload_is_not_stamped_current(self): + fake = FakeDrive(self.OK, stamp="old") + fake.fail_upload = True + r = run(fake) + self.assertIn("upload blew up", r.error) + self.assertEqual(fake.stamp, "old") + + def test_a_deck_with_no_checksum_is_an_error_not_current(self): + """Otherwise None == an unstamped banner's missing stamp.""" + fake = FakeDrive(self.OK, md5=None) + r = run(fake) + self.assertIn("no checksum", r.error) + self.assertEqual(fake.renders, []) + + def test_duplicate_banners_are_refused_with_their_ids(self): + fake = FakeDrive(self.OK, banners=2) + r = run(fake) + self.assertIn("wb, wb1", r.error) + self.assertEqual(fake.renders, []) + + def test_no_banner_is_absent_even_without_a_banners_folder_or_deck(self): + for fake in (FakeDrive(self.OK, banner=False, bdir=False), + FakeDrive(self.OK, banner=False, decks=0)): + self.assertEqual(run(fake), rwb.Result("Boston", rwb.ABSENT)) + + def test_a_banner_with_no_banners_folder_is_a_failure(self): + r = run(FakeDrive(self.OK, bdir=False)) + self.assertEqual(r.error, "no Banners folder") + + def test_a_banner_with_no_deck_is_a_failure(self): + r = run(FakeDrive(self.OK, decks=0)) + self.assertIn("no Square Logo.pptx", r.error) + + def test_duplicate_decks_are_refused(self): + fake = FakeDrive(self.OK, decks=2) + self.assertIn("deck, deck1", run(fake).error) + self.assertEqual(fake.renders, []) + + def test_a_sweep_never_creates_a_missing_banner(self): + fake = FakeDrive(self.OK, banner=False) + self.assertEqual(run(fake).action, rwb.ABSENT) + self.assertNotIn("create", fake.verbs()) + self.assertEqual(fake.renders, []) + + def test_templatecity_is_checked_against_the_city_it_clones_from(self): + fake = FakeDrive(["AAIF Community Events", "San Francisco"]) + self.assertEqual(run(fake, name="TemplateCity").action, rwb.RENDER) + + def test_a_missing_banner_is_created_once_without_retries_and_stamped(self): + fake = FakeDrive(self.OK, banner=False) + self.assertEqual(run(fake, create=True).action, rwb.RENDER) + creates = [c for c in fake.calls if c[0] == "create"] + self.assertEqual(len(creates), 1) + self.assertIs(creates[0][2], gwsmod.NO_RETRY) + self.assertEqual(creates[0][3]["parents"], ["boston"]) + self.assertEqual(fake.stamp, rwb.stamp_value("m1", "Boston")) + + def test_the_create_reaches_gws_once_even_when_it_looks_like_a_timeout(self): + """Driven to a counted subprocess, not a recorded kwarg (AGENTS.md).""" + fake = FakeDrive(self.OK, banner=False) + fake_json = fake.gws_json + + def json_through_real_create(*verb, **kw): + if verb[-1] == "create": + return gwsmod.json_out(*verb, **kw) + return fake_json(*verb, **kw) + fake.gws_json = json_through_real_create + with mock.patch("subprocess.run", return_value=mock.Mock( + returncode=1, stdout="", stderr="503 timed out")) as sp, \ + mock.patch("time.sleep"): + r = run(fake, create=True) + self.assertIsNotNone(r.error) + self.assertEqual(sp.call_count, 1) + self.assertNotIn("upload", fake.verbs()) + + def test_sync_one_writes_and_creates(self): + fake = FakeDrive(self.OK, banner=False) + with mock.patch.object(cc, "list_children", fake.list_children), \ + mock.patch.object(cc, "gws_json", fake.gws_json), \ + mock.patch.object(cc, "gws_download", fake.download), \ + mock.patch.object(cc, "gws_upload", fake.upload), \ + mock.patch.object(rwb.slides_export, "render_slide_png", fake.render): + r = rwb.sync_one("boston", "Boston", create=True) + self.assertEqual(r, rwb.Result("Boston", rwb.RENDER)) + self.assertEqual(fake.renders, ["deck"]) + self.assertIn("upload", fake.verbs()) + self.assertEqual(fake.stamp, rwb.stamp_value("m1", "Boston")) + + def test_a_create_then_failed_upload_names_the_empty_file(self): + fake = FakeDrive(self.OK, banner=False) + fake.fail_upload = True + r = run(fake, create=True) + self.assertIn("EMPTY", r.error) + self.assertIn("wb", r.error) + self.assertIsNone(fake.stamp) + + def test_errors_are_returned_not_raised(self): + fake = FakeDrive(self.OK) + fake.list_children = lambda fid: (_ for _ in ()).throw(OSError("drive down")) + self.assertIn("drive down", run(fake).error) + + +class TestMain(unittest.TestCase): + CHAPTERS = [("t", "TemplateCity"), ("a", "Aarhus"), ("b", "Boston")] + + def _main(self, results, argv=()): + seen = [] + self.writes = [] + + def fake_list(parent): + return [{"id": i, "name": n, "mimeType": cc.FOLDER} for i, n in self.CHAPTERS] + + def fake_sync(cid, name, write, tmp, create=False): + seen.append((name, create)) + self.writes.append(write) + return results.get(name, rwb.Result(name, rwb.CURRENT)) + with mock.patch.object(cc, "list_children", fake_list), \ + mock.patch.object(rwb, "sync_chapter", fake_sync), \ + mock.patch("sys.stdout", io.StringIO()) as out: + code = rwb.main(list(argv)) + return code, seen, out.getvalue() + + def test_a_sweep_skips_templatecity_and_never_creates(self): + code, seen, _ = self._main({}) + self.assertEqual(seen, [("Aarhus", False), ("Boston", False)]) + self.assertEqual(code, 0) + + def test_templatecity_only_by_name_case_insensitive(self): + _c, seen, _ = self._main({}, ["--chapter", "templatecity"]) + self.assertEqual(seen, [("TemplateCity", False)]) + + def test_report_with_work_exits_2(self): + code, _s, _o = self._main({"Aarhus": rwb.Result("Aarhus", rwb.RENDER)}) + self.assertEqual(code, 2) + + def test_absent_is_listed_not_failed(self): + code, _s, out = self._main({"Boston": rwb.Result("Boston", rwb.ABSENT)}) + self.assertEqual(code, 0) + self.assertIn("No Web Banner.png at all", out) + + def test_any_failure_exits_1(self): + code, _s, _o = self._main({"Boston": rwb.Result("Boston", error="boom")}) + self.assertEqual(code, 1) + + def test_an_unknown_outcome_is_a_failure_not_clean(self): + code, _s, out = self._main({"Boston": rwb.Result("Boston")}) + self.assertEqual(code, 1) + self.assertIn("unknown outcome", out) + + def test_report_mode_passes_write_false_and_write_passes_true(self): + self._main({}) + self.assertEqual(self.writes, [False, False]) + self._main({}, ["--write"]) + self.assertEqual(self.writes, [True, True]) + + def test_create_is_passed_through_and_needs_a_chapter(self): + _c, seen, _ = self._main({}, ["--chapter", "Boston", "--create"]) + self.assertEqual(seen, [("Boston", True)]) + with mock.patch("sys.stderr", io.StringIO()), self.assertRaises(SystemExit): + self._main({}, ["--create"]) + + def test_jobs_must_be_positive(self): + with mock.patch("sys.stderr", io.StringIO()), self.assertRaises(SystemExit): + self._main({}, ["--jobs", "0"]) + + def test_the_fallback_command_is_shell_quoted(self): + cmd = rwb.fallback_command("Ada's \"Town\"", create=True) + import shlex + argv = shlex.split(cmd) + self.assertEqual(argv[argv.index("--chapter") + 1], "Ada's \"Town\"") + self.assertIn("--create", argv) + self.assertNotIn("--create", rwb.fallback_command("Boston")) + + def test_no_match_exits_1(self): + code, _s, _o = self._main({}, ["--chapter", "Atlantis"]) + self.assertEqual(code, 1) + + +if __name__ == "__main__": + unittest.main()