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
fix(tools): 🐛 preserve wallpapers when photo conversion fails
cixiangtao7826ce84742f
diff --git a/tests/test_wallpaper_prep.py b/tests/test_wallpaper_prep.pyindex cd6d56510..93fa83239 100644--- a/tests/test_wallpaper_prep.py+++ b/tests/test_wallpaper_prep.py@@ -1,8 +1,11 @@"""tools/wallpaper_prep.py: size, darkening, numbering, the size limit and the refusals."""import importlib.util+import contextlib+import ioimport osimport tempfileimport unittest+from unittest import mockfrom pathlib import PathROOT = Path(__file__).resolve().parent.parent@@ -88,6 +91,79 @@ class OverlapTest(unittest.TestCase):@unittest.skipIf(Image is None, "Pillow not installed")class PrepareTest(unittest.TestCase):+ def test_cleanup_failure_after_replace_still_counts_the_picture(self):+ with tempfile.TemporaryDirectory() as d:+ src, out = Path(d, "photos"), Path(d, "out")+ src.mkdir()+ make_photo(src / "a.png")+ cleanup = tempfile.TemporaryDirectory.cleanup++ def cleanup_then_fail(stage):+ cleanup(stage)+ raise PermissionError("injected cleanup failure")++ err, output = io.StringIO(), io.StringIO()+ with mock.patch.object(tempfile.TemporaryDirectory, "cleanup", cleanup_then_fail), \+ contextlib.redirect_stderr(err), contextlib.redirect_stdout(output):+ self.assertEqual(prep.main([str(src), str(out), "--size", "32x18"]), 0)+ with Image.open(out / "01.jpg") as image:+ self.assertEqual(image.size, (32, 18))+ self.assertIn("warning:", err.getvalue())+ self.assertIn("1 of 1 pictures written", output.getvalue())++ def test_failed_encoder_keeps_previous_output_and_cleans_stage(self):+ with tempfile.TemporaryDirectory() as d:+ src, out = Path(d, "in.png"), Path(d, "out.jpg")+ make_photo(src)+ out.write_bytes(b"previous wallpaper")++ def fail_save(image, path, *args, **kwargs):+ Path(path).write_bytes(b"partial JPEG")+ raise OSError(28, "No space left on device")++ with mock.patch.object(Image.Image, "save", fail_save), self.assertRaises(OSError):+ prep.prepare(str(src), str(out), (32, 18), 1, 0, 84, 300)+ self.assertEqual(out.read_bytes(), b"previous wallpaper")+ self.assertEqual(sorted(p.name for p in Path(d).iterdir()), ["in.png", "out.jpg"])++ def test_failed_replace_keeps_previous_output(self):+ with tempfile.TemporaryDirectory() as d:+ src, out = Path(d, "in.png"), Path(d, "out.jpg")+ make_photo(src)+ out.write_bytes(b"previous wallpaper")+ with mock.patch.object(prep.os, "replace", side_effect=PermissionError("busy")), \+ self.assertRaises(PermissionError):+ prep.prepare(str(src), str(out), (32, 18), 1, 0, 84, 300)+ self.assertEqual(out.read_bytes(), b"previous wallpaper")+ self.assertEqual(sorted(p.name for p in Path(d).iterdir()), ["in.png", "out.jpg"])++ def test_success_preserves_symlink_and_target_permissions(self):+ with tempfile.TemporaryDirectory() as d:+ src, target, out = Path(d, "in.png"), Path(d, "target.jpg"), Path(d, "out.jpg")+ make_photo(src)+ target.write_bytes(b"old")+ target.chmod(0o640)+ try:+ out.symlink_to(target)+ except (OSError, NotImplementedError):+ self.skipTest("symlinks are not available here")+ prep.prepare(str(src), str(out), (32, 18), 1, 0, 84, 300)+ self.assertTrue(out.is_symlink())+ with Image.open(target) as image:+ self.assertEqual(image.size, (32, 18))+ if os.name == "posix":+ self.assertEqual(target.stat().st_mode & 0o777, 0o640)++ def test_failed_new_output_leaves_no_partial_picture(self):+ with tempfile.TemporaryDirectory() as d:+ src, out = Path(d, "in.png"), Path(d, "out.jpg")+ make_photo(src)+ with mock.patch.object(Image.Image, "save", side_effect=OSError("encoder failed")), \+ self.assertRaises(OSError):+ prep.prepare(str(src), str(out), (32, 18), 1, 0, 84, 300)+ self.assertFalse(out.exists())+ self.assertEqual(list(Path(d).iterdir()), [src])+def test_output_is_1280x720_jpeg_and_darker(self):with tempfile.TemporaryDirectory() as d:src, out = Path(d, "in.png"), Path(d, "out.jpg")diff --git a/tools/wallpaper_prep.py b/tools/wallpaper_prep.pyindex 241d1963f..bc2831b80 100644--- a/tools/wallpaper_prep.py+++ b/tools/wallpaper_prep.py@@ -8,6 +8,8 @@ second folder. The originals are never touched: a target that is the source foldlink), or an 01.jpg there that is a symlink or hard link to one of your photos, stops the run beforeanything is written. Each picture is kept under --max-kb (300 by default, whatthemes/README.md asks for) by lowering the JPEG quality a step at a time.+Each output is replaced only after a complete JPEG is ready; a failed conversion leaves any earlier+wallpaper at that name intact.The darkening is painted into the picture, so wallpaper_dim can stay at 0 and the launcher draws no shadelayer. The numbers that worked on a Xiaomi Mi Box S (2 GB) are the defaults: brightness 0.25 and a gradient@@ -21,7 +23,10 @@ Use pictures you have the right to show."""import argparseimport os+from pathlib import Path+import statimport sys+import tempfileEXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".heic", ".heif"}@@ -80,13 +85,38 @@ def prepare(path, out_path, size, brightness, gradient, quality, max_kb):image = ImageOps.exif_transpose(opened).convert("RGB")image = ImageOps.fit(image, size, Image.LANCZOS, centering=(0.5, 0.5))image = darken(image, brightness, gradient)- q = quality- while True:- image.save(out_path, "JPEG", quality=q, optimize=True)- kb = os.path.getsize(out_path) / 1024- if kb <= max_kb or q <= 50:- return round(kb), q- q -= 6+ # Save candidates beside the destination, so an encoder/disk failure leaves+ # an earlier wallpaper intact. Resolve links as Image.save did, but reject loops.+ target = Path(out_path)+ try:+ target = target.resolve(strict=True)+ except FileNotFoundError:+ target = target.resolve()+ mode = stat.S_IMODE(target.stat().st_mode) if target.exists() else None+ if mode is not None and not os.access(target, os.W_OK):+ raise PermissionError(f"cannot write {out_path}")+ committed = False+ try:+ with tempfile.TemporaryDirectory(prefix=".wallpaper-", dir=target.parent) as folder:+ stage = Path(folder) / "wallpaper.jpg"+ q = quality+ while True:+ image.save(stage, "JPEG", quality=q, optimize=True)+ kb = stage.stat().st_size / 1024+ if kb <= max_kb or q <= 50:+ break+ q -= 6+ if mode is not None:+ stage.chmod(mode)+ os.replace(stage, target)+ committed = True+ except OSError as error:+ if not committed:+ raise+ # Replacement succeeded; failing to remove the staging directory must+ # not make main() report that this picture was skipped.+ print(f"warning: {out_path} was written, but staging cleanup failed: {error}", file=sys.stderr)+ return round(kb), qdef main(argv=None):