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: handle config link loops and staging cleanup failures
cixiangtaof9c8f8bf7495
diff --git a/tests/test_manage_set.py b/tests/test_manage_set.pyindex 4e871966e..9139b64db 100644--- a/tests/test_manage_set.py+++ b/tests/test_manage_set.py@@ -388,6 +388,113 @@ class SetThemeFolderTest(unittest.TestCase):self.assertEqual(set(Path(self.tmp.name).iterdir()), {self.theme, target})self.push.assert_called_once()+ def test_config_link_loops_are_refused_without_replacing_either_link(self):+ other = self.theme / "other.json"+ for two_links in (False, True):+ with self.subTest(two_links=two_links):+ try:+ self.config.symlink_to(other.name if two_links else self.config.name)+ if two_links:+ other.symlink_to(self.config.name)+ except (OSError, NotImplementedError):+ self.skipTest("symlinks unavailable")+ try:+ refusal = self.set_key()+ self.assertIsNotNone(refusal)+ self.assertIn("cannot write", refusal)+ self.assertIn("Nothing was pushed", refusal)+ self.assertTrue(self.config.is_symlink())+ self.assertEqual(self.config.readlink(), Path(other.name if two_links else self.config.name))+ if two_links:+ self.assertEqual(other.readlink(), Path(self.config.name))+ self.assertEqual(set(self.theme.iterdir()), {self.config, other} if two_links else {self.config})+ self.push.assert_not_called()+ finally:+ self.config.unlink(missing_ok=True)+ other.unlink(missing_ok=True)++ def test_an_older_python_resolve_loop_error_is_a_write_refusal(self):+ # Older pathlib versions report link loops as RuntimeError rather than OSError.+ resolve = Path.resolve+ config = self.theme.resolve() / self.config.name++ def loop_error(path, *args, **kwargs):+ if path == config:+ raise RuntimeError("Symlink loop")+ return resolve(path, *args, **kwargs)++ # The theme folder is resolved separately before config.json is considered.+ with mock.patch.object(Path, "resolve", loop_error):+ refusal = self.set_key()+ self.assertIn("cannot write", refusal)+ self.assertIn("Symlink loop", refusal)+ self.assertFalse(self.config.exists())+ self.push.assert_not_called()++ def test_a_dangling_config_link_still_creates_its_target(self):+ target = Path(self.tmp.name) / "new-shared.json"+ try:+ self.config.symlink_to(target)+ except (OSError, NotImplementedError):+ self.skipTest("symlinks unavailable")+ self.assertIsNone(self.set_key())+ self.assertTrue(self.config.is_symlink())+ self.assertEqual(json.loads(target.read_text()), {"dock_band": True})+ self.assertEqual(set(Path(self.tmp.name).iterdir()), {self.theme, target})+ self.push.assert_called_once()++ def test_cleanup_after_replacement_warns_and_keeps_the_push_result(self):+ class CleanupFails(tempfile.TemporaryDirectory):+ def __exit__(self, *args):+ super().__exit__(*args)+ raise PermissionError("staging cleanup refused")++ target = Path(self.tmp.name) / "shared.json"+ for linked in (False, True):+ for push_code in (0, 17):+ with self.subTest(linked=linked, push_code=push_code):+ self.config.unlink(missing_ok=True)+ destination = target if linked else self.config+ destination.write_text('{"greeting_name": "Keep me"}\n', encoding="utf-8")+ if linked:+ try:+ self.config.symlink_to(target)+ except (OSError, NotImplementedError):+ self.skipTest("symlinks unavailable")+ self.push.reset_mock()+ self.push.return_value = push_code+ with mock.patch.object(manage.tempfile, "TemporaryDirectory", CleanupFails), \+ mock.patch("sys.stdout"), mock.patch("sys.stderr") as stderr:+ self.assertEqual(manage.cmd_set("dock_band", "true", str(self.theme)), push_code)+ self.assertEqual(json.loads(destination.read_text()),+ {"greeting_name": "Keep me", "dock_band": True})+ self.assertEqual(self.config.is_symlink(), linked)+ self.push.assert_called_once()+ message = "".join(call.args[0] for call in stderr.write.call_args_list)+ self.assertIn("cleanup", message)+ self.assertIn("staging cleanup refused", message)+ self.assertNotIn("cannot write", message)+ self.assertNotIn("Nothing was pushed", message)+ self.assertEqual("theme push failed" in message, bool(push_code))++ def test_cleanup_failure_before_replacement_remains_a_write_refusal(self):+ class CleanupFails(tempfile.TemporaryDirectory):+ def __exit__(self, *args):+ super().__exit__(*args)+ raise PermissionError("staging cleanup refused")++ self.config.write_text('{"greeting_name": "Keep me"}\n', encoding="utf-8")+ original = self.config.read_bytes()+ with mock.patch.object(manage.tempfile, "TemporaryDirectory", CleanupFails), \+ mock.patch.object(manage.os, "replace", side_effect=PermissionError("replace refused")), \+ mock.patch("sys.stderr") as stderr:+ refusal = self.set_key()+ self.assertIn("cannot write", refusal)+ self.assertIn("Nothing was pushed", refusal)+ self.assertEqual(self.config.read_bytes(), original)+ stderr.write.assert_not_called()+ self.push.assert_not_called()+@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")diff --git a/tools/manage.py b/tools/manage.pyindex 1258645df..90dd0b11c 100644--- a/tools/manage.py+++ b/tools/manage.py@@ -554,11 +554,19 @@ def cmd_set(key, value, theme_folder):command = theme_command(folder)data[key] = parsedf = folder / "config.json"+ replaced = Falsetry:# own_folder_problem() above refuses a missing folder; an existing empty one is valid.folder.mkdir(parents=True, exist_ok=True)# Resolve the file as well as its folder: keep an owner's config.json symlink intact.- target = f.resolve()+ try:+ target = f.resolve(strict=True)+ except FileNotFoundError:+ # An empty folder or a dangling link may start a config, but a link loop must be refused.+ target = f.resolve()+ except RuntimeError as e:+ # Older pathlib versions use RuntimeError for a symlink loop; report the same write refusal.+ raise OSError(str(e)) from eif target.exists():with target.open("r+b"):pass # keep refusing a read-only file, even when its folder permits replacement@@ -568,10 +576,14 @@ def cmd_set(key, value, theme_folder):if target.exists():shutil.copymode(target, pending)os.replace(pending, target)+ replaced = Trueexcept OSError as e:- # 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.")+ if not replaced:+ # Until replace succeeds, a failed write leaves the owner's previous config intact.+ sys.exit(f"cannot write {f}: {reason(e)}. Nothing was pushed.")+ # The local edit is already saved: staging cleanup must not turn that into a write failure.+ sys.stderr.write(f"Saved {f} locally, but staging cleanup failed: {reason(e)}. "+ f"The temporary folder may remain at {staging}. Continuing with the theme push.\n")print(f"{key} = {json.dumps(parsed)} in {f}")code = push_theme(command)if code: