Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
tools: the card scripts name a picture path they cannot write
xbsheng37d6033d7266
diff --git a/tests/test_card_out_path.py b/tests/test_card_out_path.pynew file mode 100644index 000000000..ca2b443e2--- /dev/null+++ b/tests/test_card_out_path.py@@ -0,0 +1,186 @@+"""The picture the README card scripts write is read as whatever was typed, and Pillow answered it last.++`docs/workflows/press.md` step 2 is where a contributor redraws `docs/featured.png` and `docs/card.jpg`,+and it prints `tools/readme_featured.py Fredoka.ttf docs/featured.png` and+`tools/readme_card.py Fredoka.ttf docs/card.jpg`; `tools/readme_stats.py`'s own docstring prints+`docs/stats.png`. The second argument is typed from wherever the reader stands, and each script handed it+to `img.save()` as the last thing in the draw:++ docs/ -> ValueError: unknown file extension: (a folder, read as a suffixless name)+ docs/card -> ValueError: unknown file extension: (no suffix)+ shots/after.png -> FileNotFoundError: [Errno 2] ... 'shots/after.png' (no such folder)+ a file in the way -> NotADirectoryError++each a 12-line traceback with exit 1 - the code a run that wrote nothing returns - after the whole picture+had been drawn, naming neither the argument that was wrong nor whether anything reached the disk.++The four shapes are judged now, in `out_problem()`, before Pillow is imported (the order the font guard+takes, and what lets CI, which has no Pillow, run these tests), with the path named, the file the script+writes as the example, and 'Nothing was written.'; the save at the end belongs to its own+`except (OSError, ValueError)`, so a folder that went read-only, a full disk or an extension Pillow does+not know (`.txt`) is named too instead of arriving as a traceback. A real picture still writes.++Run from the repository root: python3 -m unittest discover -s tests -v+"""+import importlib.util+import os+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++ROOT = Path(__file__).resolve().parents[1]+TOOLS = ROOT / "tools"++# The three scripts that draw the card, with the picture the page or the docstring tells the reader to write.+CARD_SCRIPTS = {+ "readme_stats.py": "docs/stats.png",+ "readme_featured.py": "docs/featured.png",+ "readme_card.py": "docs/card.jpg",+}++# The launcher's own font: one vertical weight axis, so it is the variable TTF these scripts ask for.+LAUNCHER_FONT = ROOT / "launcher" / "app" / "src" / "main" / "res" / "font" / "outfit.ttf"++HAVE_PIL = importlib.util.find_spec("PIL") is not None+++def run(argv, tmp):+ """A run with a PIL package that cannot be imported: the venv CI uses has no Pillow.++ The picture path is judged before that import, so its answer must arrive without it - and a test that+ only passed on a machine with Pillow would say nothing about the scripts as CI runs them.+ """+ pkg = Path(tmp) / "PIL"+ pkg.mkdir(exist_ok=True)+ (pkg / "__init__.py").write_text('raise ImportError("No module named \'PIL\'")\n', encoding="utf-8")+ env = dict(os.environ)+ env["PYTHONPATH"] = str(tmp)+ return subprocess.run([sys.executable, *argv], cwd=ROOT, env=env, capture_output=True, text=True)+++def run_plain(argv):+ """The same command with the Pillow this machine has, so a real picture is drawn."""+ env = dict(os.environ)+ env.pop("PYTHONPATH", None)+ return subprocess.run([sys.executable, *argv], cwd=ROOT, env=env, capture_output=True, text=True)+++class PicturePathTest(unittest.TestCase):+ def assert_refused(self, out, why):+ with tempfile.TemporaryDirectory() as tmp:+ for name, picture in CARD_SCRIPTS.items():+ with self.subTest(script=name, out=why):+ done = run([str(TOOLS / name), str(LAUNCHER_FONT), str(out)], tmp)+ self.assertEqual(done.returncode, 1,+ f"{name} did not refuse {why} the way the other tools refuse an argument")+ self.assertIn(str(out), done.stderr, f"{name} does not name the picture path it was given")+ self.assertIn("Nothing was written", done.stderr,+ f"{name} does not say whether a picture was written")+ self.assertNotIn("Traceback", done.stderr, f"{name} still dies on a Pillow traceback")+ self.assertNotIn("unknown file extension", done.stderr,+ f"{name} leaves {why} to Pillow, so the answer needs Pillow installed")+ self.assertNotIn("No such file or directory", done.stderr,+ f"{name} leaves {why} to Pillow, so the answer needs Pillow installed")+ self.assertEqual(done.stdout, "", f"{name} printed a picture path it did not write")++ def test_a_folder_is_not_a_picture_name(self):+ with tempfile.TemporaryDirectory() as tmp:+ self.assert_refused(Path(tmp), "a folder")++ def test_a_folder_that_is_not_there_is_named(self):+ with tempfile.TemporaryDirectory() as tmp:+ self.assert_refused(Path(tmp) / "shots" / "after.png", "a folder that is not there")++ def test_a_file_in_the_middle_of_the_path_is_named(self):+ with tempfile.TemporaryDirectory() as tmp:+ blocker = Path(tmp) / "after.png"+ blocker.write_bytes(b"not a folder")+ self.assert_refused(blocker / "x.png", "a file in the middle of the path")++ def test_a_name_with_no_extension_says_what_the_format_needs(self):+ with tempfile.TemporaryDirectory() as tmp:+ for name in CARD_SCRIPTS:+ with self.subTest(script=name):+ out = Path(tmp) / "picture"+ done = run([str(TOOLS / name), str(LAUNCHER_FONT), str(out)], tmp)+ self.assertEqual(done.returncode, 1, f"{name} took a suffixless name")+ self.assertIn("no extension", done.stderr, f"{name} does not say what is missing")+ self.assertIn(str(out), done.stderr, f"{name} does not name the argument")+ self.assertNotIn("Traceback", done.stderr, f"{name} still dies on a Pillow traceback")+ self.assertFalse(out.exists(), f"{name} wrote {out}")++ def test_the_folder_message_names_the_picture_the_script_writes(self):+ # The example is the file the page, or the docstring, tells the reader to write: gen_tables.py+ # redraws the same two paths, so a reader who follows the message lands on the tracked file.+ with tempfile.TemporaryDirectory() as tmp:+ for name, picture in CARD_SCRIPTS.items():+ with self.subTest(script=name):+ done = run([str(TOOLS / name), str(LAUNCHER_FONT), tmp], tmp)+ self.assertIn(picture, done.stderr, f"{name} does not name {picture} as the file to write")+ self.assertTrue((ROOT / picture).parent.is_dir(), f"{picture} has no folder in a checkout")++ def test_the_picture_is_judged_before_the_pillow_import(self):+ # What makes the checks above work in CI, where Pillow is absent: the guard sits above the import.+ for name in CARD_SCRIPTS:+ with self.subTest(script=name):+ text = (TOOLS / name).read_text(encoding="utf-8")+ guard = text.find("out_problem(OUT)")+ pillow = text.find("from PIL import")+ self.assertGreater(guard, 0, f"{name} does not judge the picture it is given")+ self.assertGreater(pillow, 0, f"{name} no longer imports Pillow, so this check would prove nothing")+ self.assertLess(guard, pillow, f"{name} judges the picture after importing Pillow")++ def test_the_write_at_the_end_is_guarded(self):+ # A folder that went read-only, a full disk or an unknown extension is found at the write, after+ # the drawing: that one belongs to its own handler rather than to a traceback.+ for name in CARD_SCRIPTS:+ with self.subTest(script=name):+ text = (TOOLS / name).read_text(encoding="utf-8")+ handler = text.find("except (OSError, ValueError) as error:")+ self.assertGreater(handler, 0, f"{name} leaves the save to a bare traceback")+ last_save = text.rfind(".save(OUT")+ self.assertGreater(last_save, 0, f"{name} no longer saves to OUT, so this check would prove nothing")+ self.assertLess(handler, last_save, f"{name} does not guard the write that ends it")++[email protected](HAVE_PIL, "drawing the card needs Pillow (tools/requirements-perf.txt)")+class RealPictureTest(unittest.TestCase):+ def test_a_variable_font_still_writes_the_picture(self):+ # The guard must not refuse a path that is fine: the page's own command still writes its picture.+ with tempfile.TemporaryDirectory() as tmp:+ for name, picture in CARD_SCRIPTS.items():+ with self.subTest(script=name):+ if name == "readme_card.py":+ for part in ("docs/stats.png", "docs/featured.png"):+ if not (ROOT / part).is_file():+ self.skipTest(f"{part} is not in this checkout, so the card cannot be stacked")+ out = Path(tmp) / Path(picture).name+ done = run_plain([str(TOOLS / name), str(LAUNCHER_FONT), str(out)])+ self.assertEqual(done.returncode, 0, f"{name} refused a picture path it should write: {done.stderr}")+ self.assertTrue(out.is_file(), f"{name} said it was fine and wrote nothing")+ self.assertGreater(out.stat().st_size, 1000, f"{name} wrote an empty picture")++ def test_a_write_that_fails_is_named(self):+ # The one failure only the write can find: /dev/full answers ENOSPC once the picture is drawn.+ if not Path("/dev/full").exists():+ self.skipTest("no /dev/full on this machine")+ with tempfile.TemporaryDirectory() as tmp:+ for name, picture in CARD_SCRIPTS.items():+ with self.subTest(script=name):+ if name == "readme_card.py":+ for part in ("docs/stats.png", "docs/featured.png"):+ if not (ROOT / part).is_file():+ self.skipTest(f"{part} is not in this checkout, so the card cannot be stacked")+ out = Path(tmp) / Path(picture).name+ out.symlink_to("/dev/full")+ done = run_plain([str(TOOLS / name), str(LAUNCHER_FONT), str(out)])+ self.assertEqual(done.returncode, 1, f"{name} took a write that failed as success")+ self.assertIn("cannot write", done.stderr, f"{name} does not name the file it could not write")+ self.assertIn("Nothing was written", done.stderr, f"{name} does not say the picture is missing")+ self.assertNotIn("Traceback", done.stderr, f"{name} still dies on the write")+++if __name__ == "__main__":+ unittest.main()diff --git a/tests/test_gen_tables.py b/tests/test_gen_tables.pyindex 232d962e0..cd8266f84 100644--- a/tests/test_gen_tables.py+++ b/tests/test_gen_tables.py@@ -267,6 +267,8 @@ class FontTest(unittest.TestCase):root = tree(Path(tmp) / "repo", [("tcl-a", tv("TCL A", "TCL"))])# The real script, whose counts this tree changes, so --font hands it the font.shutil.copy(ROOT / "tools" / "readme_stats.py", root / "tools" / "readme_stats.py")+ # A checkout has docs/, where the picture goes: the script names the folder it cannot find.+ (root / "docs").mkdir()caller = Path(tmp) / "caller"caller.mkdir()(caller / "Fredoka.ttf").write_bytes(b"")diff --git a/tools/readme_card.py b/tools/readme_card.pyindex e1dfb55f6..dfa6091b7 100644--- a/tools/readme_card.py+++ b/tools/readme_card.py@@ -30,8 +30,38 @@ def font_and_out(argv):return font, out+def out_problem(path):+ """Why the picture at `path` cannot be written, or None.++ The second argument the reader types, and the one Pillow only judges at the end of the draw:+ `img.save()` answers a folder with IsADirectoryError, a path through a file with NotADirectoryError,+ a folder that is not there with FileNotFoundError and a name with no extension with `unknown file+ extension` - each a traceback that names neither the argument nor whether a picture was written.+ Judged here, before Pillow is imported, so a machine without Pillow gives the same answer.+ """+ if os.path.isdir(path):+ return (f"{path} is a folder, and this writes one picture: pass a file name such as "+ f"docs/card.jpg")+ folder = os.path.dirname(path) or "."+ if not os.path.isdir(folder):+ return (f"there is no folder {folder} for {path}: create it first, or pass a name inside one "+ f"that is there")+ if not os.path.splitext(path)[1]:+ return (f"{path} has no extension, so the format to write is unknown: pass a name ending in "+ f".jpg")+ return None++FONT, OUT = font_and_out(sys.argv)+# The picture is the other argument the reader types, and Pillow only refuses it after the whole thing+# has been drawn: a folder, a folder that is not there, a path through a file and a suffixless name were+# each a 12-line traceback with exit 1, the code a run that wrote nothing returns, saying nothing about+# what was written. Judged before the Pillow import, so a machine without it answers the same way.+OUT_PROBLEM = out_problem(OUT)+if OUT_PROBLEM:+ sys.exit(f"{OUT_PROBLEM}. Nothing was written.")+# See readme_stats.py: Pillow lives in the optional requirements file, so name it rather than crash.try:from PIL import Image, ImageDraw, ImageFilter, ImageFont@@ -85,4 +115,9 @@ card = Image.new("RGB", (W, H), BG); d = ImageDraw.Draw(card); y = 0for i, p in enumerate(parts):if i: y += SEP; d.line([(PAD, y), (W - PAD, y)], fill=LINE, width=2); y += SEPcard.paste(p, (0, y)); y += p.height-card.save(OUT, quality=88, optimize=True)+try:+ card.save(OUT, quality=88, optimize=True)+except (OSError, ValueError) as error:+ # The read-only folder or the full disk, or an extension Pillow does not know (`.txt`): found at the+ # write, after the drawing. Name the file and say that no picture came of the run.+ sys.exit(f"cannot write {OUT}: {error}. Nothing was written.")diff --git a/tools/readme_featured.py b/tools/readme_featured.pyindex 24635cb0a..74a62b93c 100644--- a/tools/readme_featured.py+++ b/tools/readme_featured.py@@ -28,8 +28,38 @@ def font_and_out(argv):return font, out+def out_problem(path):+ """Why the picture at `path` cannot be written, or None.++ The second argument the reader types, and the one Pillow only judges at the end of the draw:+ `img.save()` answers a folder with IsADirectoryError, a path through a file with NotADirectoryError,+ a folder that is not there with FileNotFoundError and a name with no extension with `unknown file+ extension` - each a traceback that names neither the argument nor whether a picture was written.+ Judged here, before Pillow is imported, so a machine without Pillow gives the same answer.+ """+ if os.path.isdir(path):+ return (f"{path} is a folder, and this writes one picture: pass a file name such as "+ f"docs/featured.png")+ folder = os.path.dirname(path) or "."+ if not os.path.isdir(folder):+ return (f"there is no folder {folder} for {path}: create it first, or pass a name inside one "+ f"that is there")+ if not os.path.splitext(path)[1]:+ return (f"{path} has no extension, so the format to write is unknown: pass a name ending in "+ f".png")+ return None++FONT, OUT = font_and_out(sys.argv)+# The picture is the other argument the reader types, and Pillow only refuses it after the whole thing+# has been drawn: a folder, a folder that is not there, a path through a file and a suffixless name were+# each a 12-line traceback with exit 1, the code a run that wrote nothing returns, saying nothing about+# what was written. Judged before the Pillow import, so a machine without it answers the same way.+OUT_PROBLEM = out_problem(OUT)+if OUT_PROBLEM:+ sys.exit(f"{OUT_PROBLEM}. Nothing was written.")+# See readme_stats.py: Pillow lives in the optional requirements file, so name it rather than crash.try:from PIL import Image, ImageDraw, ImageFont@@ -80,4 +110,9 @@ d.text((x0, ty), MORE, font=m, fill="#aab3bd") # grey; the title is the soft whax, ay = x0 + w - 26, ty + 22 # arrow: Fredoka has no U+2192d.line([(ax - 12, ay), (ax + 12, ay)], fill="#cfffcf", width=4)d.line([(ax + 2, ay - 10), (ax + 13, ay), (ax + 2, ay + 10)], fill="#cfffcf", width=4, joint="curve")-img.convert("RGB").save(OUT, optimize=True)+try:+ img.convert("RGB").save(OUT, optimize=True)+except (OSError, ValueError) as error:+ # The read-only folder or the full disk, or an extension Pillow does not know (`.txt`): found at the+ # write, after the drawing. Name the file and say that no picture came of the run.+ sys.exit(f"cannot write {OUT}: {error}. Nothing was written.")diff --git a/tools/readme_stats.py b/tools/readme_stats.pyindex 8e43b6008..7e1a4349c 100644--- a/tools/readme_stats.py+++ b/tools/readme_stats.py@@ -28,8 +28,38 @@ def font_and_out(argv):return font, out+def out_problem(path):+ """Why the picture at `path` cannot be written, or None.++ The second argument the reader types, and the one Pillow only judges at the end of the draw:+ `img.save()` answers a folder with IsADirectoryError, a path through a file with NotADirectoryError,+ a folder that is not there with FileNotFoundError and a name with no extension with `unknown file+ extension` - each a traceback that names neither the argument nor whether a picture was written.+ Judged here, before Pillow is imported, so a machine without Pillow gives the same answer.+ """+ if os.path.isdir(path):+ return (f"{path} is a folder, and this writes one picture: pass a file name such as "+ f"docs/stats.png")+ folder = os.path.dirname(path) or "."+ if not os.path.isdir(folder):+ return (f"there is no folder {folder} for {path}: create it first, or pass a name inside one "+ f"that is there")+ if not os.path.splitext(path)[1]:+ return (f"{path} has no extension, so the format to write is unknown: pass a name ending in "+ f".png")+ return None++FONT, OUT = font_and_out(sys.argv)+# The picture is the other argument the reader types, and Pillow only refuses it after the whole thing+# has been drawn: a folder, a folder that is not there, a path through a file and a suffixless name were+# each a 12-line traceback with exit 1, the code a run that wrote nothing returns, saying nothing about+# what was written. Judged before the Pillow import, so a machine without it answers the same way.+OUT_PROBLEM = out_problem(OUT)+if OUT_PROBLEM:+ sys.exit(f"{OUT_PROBLEM}. Nothing was written.")+# Pillow is not in tools/requirements.txt: it is only needed to redraw the README card, so point at the# optional file the way tools/flamechart.py does, instead of ending in a bare traceback.try:@@ -63,4 +93,9 @@ for i, (n, l) in enumerate(STATS):sp = 5; w = sum(d.textlength(c, font=lab) for c in l) + sp * (len(l) - 1); x = cx - w / 2for c in l: d.text((x, 160), c, font=lab, fill="#aab3bd"); x += d.textlength(c, font=lab) + spif i: d.line([(PAD + col * i, 56), (PAD + col * i, H - 56)], fill="#2c313a", width=3)-img.save(OUT, optimize=True)+try:+ img.save(OUT, optimize=True)+except (OSError, ValueError) as error:+ # The read-only folder or the full disk, or an extension Pillow does not know (`.txt`): found at the+ # write, after the drawing. Name the file and say that no picture came of the run.+ sys.exit(f"cannot write {OUT}: {error}. Nothing was written.")