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: recovery.py --commands stops without TV_HOST instead of printing the whole-list batch
xbsheng34b32a99d9d7
diff --git a/docs/restore.md b/docs/restore.mdindex be6c7166f..88e2ad503 100644--- a/docs/restore.md+++ b/docs/restore.md@@ -29,6 +29,9 @@ Before `tools/disable.sh` changes anything it writes down what the TV looked likThat record lives in `~/.cleanyourtv/`, one file per TV and list.+- The file is named after the address the TV was reached at, so `TV_HOST` is what picks the record:+ a run without it stops with tv.py's line and sends nothing to the TV, rather than falling back to+ the whole list for a record it cannot name.- A second run of the same list keeps the first record instead of saving the cleaned-up state over it.- If you added packages to the list since, that run adds them to the record before disabling them, anda listed package that was not installed then but is installed and enabled now is recorded as enabled.diff --git a/tests/test_debloat_paths.py b/tests/test_debloat_paths.pyindex 105626be6..5ba80f6a0 100644--- a/tests/test_debloat_paths.py+++ b/tests/test_debloat_paths.py@@ -66,9 +66,12 @@ class DebloatPathsTest(unittest.TestCase):if os.name != "posix" or not shutil.which("bash"):raise unittest.SkipTest("disable.sh and restore.sh are POSIX scripts; they need bash")capture = base / "commands.txt"- # No TV_HOST, so recovery.py looks for a record that cannot exist: that is the fallback these- # tests exercise. HOME is isolated so a record from a real run cannot answer instead.+ # No record under this address, so recovery.py falls back to enabling the whole list: that is+ # the plan these tests exercise. TV_HOST names the record (recovery.py refuses to guess one+ # without it) and never reaches a TV here; HOME is isolated so a record from a real run+ # cannot answer instead.env = {k: v for k, v in os.environ.items() if k != "TV_HOST"}+ env["TV_HOST"] = "192.168.1.50"env.update(CAPTURE=str(capture), HOME=str(home))# The fake tv.py reads its stdin, so an inherited terminal would leave it waiting.result = subprocess.run(diff --git a/tests/test_recovery_commands_needs_host.py b/tests/test_recovery_commands_needs_host.pynew file mode 100644index 000000000..9575bb6e9--- /dev/null+++ b/tests/test_recovery_commands_needs_host.py@@ -0,0 +1,178 @@+"""`recovery.py --commands` without TV_HOST cannot name the record, so it must not print a plan.++The record is one file per TV and list, and `record_path` names it after the address the TV was+reached at. A run with no TV_HOST therefore has no name to look under: it used to fall back to the+"no-host" tag `record_name` gives an empty tag, find nothing, and print the no-record fallback - the+batch that enables every listed package - while saying "no recovery record for this TV and list",+about a TV whose record may well be sitting right there. That fallback is the one plan produced+without looking (docs/restore.md: packages the owner had already disabled are enabled too), so it is+the one run that must say it did not look instead. Nothing reaches the TV either, so it is a run that+never asked the TV anything: exit 2, as tools/tv.py, check.py and privacy.py answer that shape.++`--commands` still asks the TV nothing and needs no ADB client; TV_HOST is only the record's name.++Run from the repository root: python3 -m unittest discover -s tests -v+"""+import hashlib+import json+import os+from pathlib import Path+import shutil+import subprocess+import sys+import tempfile+import unittest+++ROOT = Path(__file__).resolve().parents[1]+RECOVERY = ROOT / "tools" / "recovery.py"+ADDRESS = "192.168.1.50"+# Stands in for tools/tv.py under restore.sh: keeps the batch it is sent, so a test can see that+# nothing was sent.+FAKE_TV = ('import sys\n'+ 'open(__import__("os").environ["CAPTURE"], "w").write(sys.stdin.read())\n')+++def digest(list_path):+ return hashlib.sha1(str(Path(list_path).resolve()).encode()).hexdigest()[:8]+++def record_for(list_path, host):+ return {+ "version": 1,+ "created": "2026-10-30T10:00:00+0800",+ "host": host,+ "list": str(Path(list_path).resolve()),+ "home": "com.google.android.tvlauncher/.MainActivity",+ "settings": {"window_animation_scale": "0.5",+ "transition_animation_scale": "null",+ "animator_duration_scale": "1.0"},+ "packages": {"com.example.enabled": "enabled",+ "com.example.already_off": "disabled"},+ }+++class NoTVHostTest(unittest.TestCase):+ def setUp(self):+ tmp = tempfile.TemporaryDirectory()+ self.addCleanup(tmp.cleanup)+ self.base = Path(tmp.name)+ self.home = self.base / "home"+ self.home.mkdir()+ self.list_path = self.base / "debloat.txt"+ self.list_path.write_text("# My TV\ncom.example.enabled\ncom.example.already_off\n")+ self.records = self.home / ".cleanyourtv"++ def write_record(self, host=ADDRESS):+ self.records.mkdir(exist_ok=True)+ path = self.records / f"recovery-{host}-{digest(self.list_path)}.json"+ path.write_text(json.dumps(record_for(self.list_path, host)))+ return path++ def env(self, host=None):+ env = {key: value for key, value in os.environ.items()+ if key not in ("TV_HOST", "TV_PORT")}+ env["HOME"] = str(self.home)+ if host is not None:+ env["TV_HOST"] = host+ return env++ def run_recovery(self, *args, host=None):+ return subprocess.run(+ [sys.executable, "-I", str(RECOVERY), *args],+ env=self.env(host), capture_output=True, text=True, timeout=120,+ )++ def test_a_run_without_tv_host_stops_instead_of_printing_the_fallback(self):+ """The case that matters: a record for this list is there, under an address not given."""+ self.write_record()+ result = self.run_recovery("--commands", str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertEqual(result.stdout, "", "a plan was printed for a record this run never read")+ self.assertIn("Set TV_HOST to your TV's IP address", result.stderr)+ self.assertIn("named after the address the TV was reached at", result.stderr)+ self.assertIn("Nothing was sent to the TV.", result.stderr)+ self.assertNotIn("no recovery record", result.stderr)+ self.assertNotIn("pm enable", result.stderr + result.stdout)++ def test_a_run_without_tv_host_writes_no_record_of_its_own(self):+ """Nothing in ~/.cleanyourtv/ is touched: a name is looked under, never made up."""+ self.write_record()+ before = {path.name: path.read_text() for path in self.records.iterdir()}+ result = self.run_recovery("--commands", str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stderr)+ after = {path.name: path.read_text() for path in self.records.iterdir()}+ self.assertEqual(before, after)+ self.assertFalse([name for name in after if "no-host" in name],+ f"a record under the no-host name appeared: {sorted(after)}")++ def test_a_run_without_tv_host_says_it_for_a_run_with_no_record_too(self):+ """The same answer whether or not a record exists: the run did not look either way."""+ result = self.run_recovery("--commands", str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertEqual(result.stdout, "")+ self.assertIn("Nothing was sent to the TV.", result.stderr)++ def test_the_address_names_the_record_and_the_plan_is_the_recorded_one(self):+ """The guard is about the missing address, not about printing a plan at all."""+ self.write_record()+ result = self.run_recovery("--commands", str(self.list_path), host=ADDRESS)+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual(result.stdout.splitlines(), [+ "pm enable com.example.enabled",+ "cmd package set-home-activity --user 0 com.google.android.tvlauncher/.MainActivity",+ "settings put global window_animation_scale 0.5",+ "settings delete global transition_animation_scale",+ "settings put global animator_duration_scale 1.0",+ "pm list packages -d",+ ])+ self.assertIn("leaving com.example.already_off alone (disabled when the record was taken)",+ result.stderr)++ def test_a_run_with_an_address_and_no_record_still_falls_back(self):+ """docs/restore.md's "Without a record": unchanged, and it still says what it enables."""+ result = self.run_recovery("--commands", str(self.list_path), host=ADDRESS)+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertIn("no recovery record for this TV and list", result.stderr)+ self.assertIn("Packages that were already disabled before cleanup will be enabled too",+ result.stderr)+ self.assertIn("pm enable com.example.already_off", result.stdout)++ def test_restore_sh_sends_nothing_without_tv_host(self):+ """restore.sh is the caller that matters: no batch, no TV, and it stops."""+ if os.name != "posix" or not shutil.which("bash"):+ raise unittest.SkipTest("restore.sh is a POSIX script; it needs bash")+ repo = self.base / "repo"+ tools = repo / "tools"+ tools.mkdir(parents=True)+ for name in ("restore.sh", "recovery.py"):+ shutil.copy2(ROOT / "tools" / name, tools / name)+ (tools / "tv.py").write_text(FAKE_TV)+ client = tools / "venv" / "bin" / "python"+ client.parent.mkdir(parents=True)+ client.write_text(f'#!/bin/sh\nexec "{sys.executable}" "$@"\n')+ client.chmod(0o755)+ self.write_record()+ capture = self.base / "sent.txt"+ env = self.env()+ env["CAPTURE"] = str(capture)+ result = subprocess.run(+ ["bash", str(tools / "restore.sh"), str(self.list_path)],+ cwd=repo, env=env, capture_output=True, text=True,+ stdin=subprocess.DEVNULL, timeout=120,+ )+ self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertIn("Nothing was sent to the TV.", result.stderr)+ self.assertFalse(capture.exists() and capture.read_text().strip(),+ f"a batch reached the TV: {capture.read_text() if capture.exists() else ''}")++ def test_the_usage_block_names_tv_host(self):+ """A read guard: the docstring that hands out the command says what it needs."""+ docstring = RECOVERY.read_text().split('"""')[1]+ self.assertIn("TV_HOST", docstring)+ self.assertIn("only the address that names the record", docstring)+ self.assertIn("so both need TV_HOST", docstring)+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/recovery.py b/tools/recovery.pyindex dadca79c9..7cbf1bb56 100644--- a/tools/recovery.py+++ b/tools/recovery.py@@ -5,6 +5,10 @@recovery.py --commands <list> [stock launcher] print a batch for tools/tv.py --batch that putsthe recorded states back+Both read the record for this TV and list, and the record is one file per TV and list named after+the address the TV was reached at, so both need TV_HOST. `--commands` asks the TV nothing: it needs+no TV and no ADB client, only the address that names the record.+`tools/restore.sh` used to enable every package in the list, including ones the owner had alreadydisabled, and reset Home and the animation scales to fixed values instead of to what was therebefore. Both scripts now go through this record: `disable.sh` writes it before the first command@@ -589,6 +593,17 @@ def restore_commands(list_path, stock, host):return commands+# Why a --commands run without TV_HOST stops instead of printing a plan: the record is one file per TV+# and list, named after the address the TV was reached at (record_path), so there is no name to look+# under, and the one it fell back to - "no-host", from record_name's tag - is a name save() cannot+# write. The run then found no record and printed the no-record fallback, the batch that enables every+# listed package, for a TV whose record may well be there (#239 is what that record exists to prevent).+# Nothing reaches a TV without an address either, so this is a run that never asked the TV anything:+# tv.py's own line, then exit 2.+NO_ADDRESS = ("The record is one file per TV and list, named after the address the TV was reached at, "+ "so a run without TV_HOST cannot say which one to read.")++def main():args = sys.argv[1:]host = os.environ.get("TV_HOST", "")@@ -596,7 +611,12 @@ def main():return save(args[1], host)if len(args) in (2, 3) and args[0] == "--commands":if not host:- sys.stderr.write("Set TV_HOST to your TV's IP address, e.g. TV_HOST=192.168.1.50\n")+ # tv.py's own words, then why this run cannot go on, then exit 2. The address is not+ # reachable from here and nothing is sent to a TV; it is only what names the record.+ sys.stderr.write("Set TV_HOST to your TV's IP address, e.g. TV_HOST=192.168.1.50\n"+ f"{NO_ADDRESS}\n"+ "Nothing was sent to the TV.\n")+ return 2stock = args[2] if len(args) == 3 else "com.google.android.tvlauncher"for command in restore_commands(args[1], stock, host):print(command)