Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

fix(tools): 🐛 preserve theme config when manager writes fail

cixiangtaofa27efdec0a9
diff --git a/tests/test_manage_set.py b/tests/test_manage_set.py
index 4d6fbb310..4e871966e 100644
--- a/tests/test_manage_set.py
+++ b/tests/test_manage_set.py
@@ -332,6 +332,75 @@ class SetThemeFolderTest(unittest.TestCase):
self.assertIn("Nothing was pushed", refusal)
self.push.assert_not_called()
+ def test_a_partial_write_preserves_the_existing_config(self):
+ self.config.write_text('{"greeting_name": "Keep me", "dock_band": false}\n', encoding="utf-8")
+ original = self.config.read_bytes()
+ write_text = Path.write_text
+
+ def interrupted(path, text, *args, **kwargs):
+ write_text(path, text[:12], *args, **kwargs)
+ raise OSError(28, "No space left on device")
+
+ with mock.patch.object(Path, "write_text", interrupted):
+ refusal = self.set_key()
+ self.assertIn("cannot write", refusal)
+ self.assertEqual(self.config.read_bytes(), original)
+ self.assertEqual(list(self.theme.iterdir()), [self.config])
+ self.push.assert_not_called()
+
+ def test_a_partial_first_write_leaves_no_config(self):
+ write_text = Path.write_text
+
+ def interrupted(path, text, *args, **kwargs):
+ write_text(path, text[:12], *args, **kwargs)
+ raise OSError(28, "No space left on device")
+
+ with mock.patch.object(Path, "write_text", interrupted):
+ refusal = self.set_key()
+ self.assertIn("cannot write", refusal)
+ self.assertEqual(list(self.theme.iterdir()), [])
+ self.push.assert_not_called()
+
+ def test_a_failed_replace_keeps_the_original_and_cleans_staging(self):
+ self.config.write_text('{"greeting_name": "Keep me"}\n', encoding="utf-8")
+ original = self.config.read_bytes()
+ with mock.patch.object(manage.os, "replace", side_effect=PermissionError("replace refused")):
+ refusal = self.set_key()
+ self.assertIsNotNone(refusal)
+ self.assertEqual(self.config.read_bytes(), original)
+ self.assertEqual(list(self.theme.iterdir()), [self.config])
+ self.push.assert_not_called()
+
+ def test_a_symlink_still_updates_its_target_and_keeps_its_mode(self):
+ target = Path(self.tmp.name) / "shared.json"
+ target.write_text('{"greeting_name": "Keep me"}\n', encoding="utf-8")
+ target.chmod(0o640)
+ try:
+ self.config.symlink_to(target)
+ except (OSError, NotImplementedError):
+ self.skipTest("symlinks unavailable")
+ mode = target.stat().st_mode
+ self.assertIsNone(self.set_key())
+ self.assertTrue(self.config.is_symlink())
+ self.assertEqual(json.loads(target.read_text()), {"greeting_name": "Keep me", "dock_band": True})
+ self.assertEqual(target.stat().st_mode, mode)
+ self.assertEqual(list(self.theme.iterdir()), [self.config])
+ self.assertEqual(set(Path(self.tmp.name).iterdir()), {self.theme, target})
+ self.push.assert_called_once()
+
+ @unittest.skipIf(sys.platform == "win32", "POSIX write permissions")
+ def test_a_read_only_config_is_not_replaced(self):
+ self.config.write_text('{"greeting_name": "Keep me"}\n', encoding="utf-8")
+ original = self.config.read_bytes()
+ self.config.chmod(0o444)
+ self.addCleanup(self.config.chmod, 0o644)
+ if manage.os.access(self.config, manage.os.W_OK):
+ self.skipTest("process can write read-only files")
+ self.assertIsNotNone(self.set_key())
+ self.assertEqual(self.config.read_bytes(), original)
+ self.assertEqual(list(self.theme.iterdir()), [self.config])
+ self.push.assert_not_called()
+
def test_a_folder_this_checkout_ships_is_refused(self):
# A checkout of its own, so the project's real files are never touched by this test.
# Resolved like manage.ROOT is, so a temporary folder behind a symlink (/var on macOS) still compares.
diff --git a/tools/manage.py b/tools/manage.py
index 0eabcbc63..dcca15435 100644
--- a/tools/manage.py
+++ b/tools/manage.py
@@ -47,6 +47,7 @@ import re
import shutil
import subprocess
import sys
+import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
@@ -554,11 +555,20 @@ def cmd_set(key, value, theme_folder):
data[key] = parsed
f = folder / "config.json"
try:
- folder.mkdir(parents=True, exist_ok=True)
- f.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
+ # Resolve the file as well as its folder: keep an owner's config.json symlink intact.
+ target = f.resolve()
+ if target.exists():
+ with target.open("r+b"):
+ pass # keep refusing a read-only file, even when its folder permits replacement
+ with tempfile.TemporaryDirectory(prefix=".manage-config-", dir=target.parent) as staging:
+ pending = Path(staging) / target.name
+ pending.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
+ if target.exists():
+ shutil.copymode(target, pending)
+ os.replace(pending, target)
except OSError as e:
- # A folder that is read-only, a path with a file in the middle of it, a full disk: the key is not
- # written and the theme on the TV is untouched, so say which file and why rather than a traceback.
+ # Finish and close a copy beside the destination before replacing it, so a failed write
+ # cannot truncate the owner's previous config. Nothing has been pushed at this point.
sys.exit(f"cannot write {f}: {reason(e)}. Nothing was pushed.")
print(f"{key} = {json.dumps(parsed)} in {f}")
code = push_theme(command)