Skip to content

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

tools: recovery.py keeps one recovery record per TV on the network, with or without its port

Mazin Kanugaafe7a69ffc33
diff --git a/tests/test_recovery_record_lan_address.py b/tests/test_recovery_record_lan_address.py
new file mode 100644
index 000000000..f5d85da9b
--- /dev/null
+++ b/tests/test_recovery_record_lan_address.py
@@ -0,0 +1,153 @@
+"""One recovery record per TV on the network, however TV_HOST names it.
+
+tools/recovery.py named a record after TV_HOST exactly as typed and ignored TV_PORT. So the same TV
+saved as 192.168.1.50 and restored as 192.168.1.50:5555 found no record, and restore fell back to
+enabling every listed package, the ones the owner had disabled before cleanup included. On the
+network the record is now named after the TV's IP address alone, records written under the old
+name (with the port) are still read, and with records under both names the first one wins. A relay
+(a loopback address) and a host name keep TV_HOST as typed, as before. The TV is the fake tools/tv.py
+from tests/test_recovery_record.py.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import unittest
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+import test_recovery_record as harness # noqa: E402
+
+STOCK_HOME = harness.STOCK_HOME
+# What restore sends for the harness's usual TV (test_valid_record_restores_the_same_batch).
+USUAL_BATCH = [
+ "pm enable com.example.enabled",
+ "pm enable com.google.android.tvlauncher",
+ f"cmd package set-home-activity --user 0 {STOCK_HOME}",
+ "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",
+]
+# The same TV after the list was applied: a record of it would leave com.example.enabled off.
+CLEANED_TV = {"disabled": ["com.example.enabled", "com.example.already_off"]}
+
+
+class RecordPerLanAddressTest(unittest.TestCase):
+ # The harness's copy of tools/ with the fake tv.py, HOME and TV state. The methods are borrowed
+ # rather than inherited, so that the harness's own tests do not run twice.
+ setUp = harness.RecoveryRecordTest.setUp
+ write_list = harness.RecoveryRecordTest.write_list
+ tv = harness.RecoveryRecordTest.tv
+ sent = harness.RecoveryRecordTest.sent
+
+ def run_script(self, script, host, port=None):
+ env = dict(os.environ, TV_HOST=host, HOME=str(self.home), FAKE_TV_STATE=str(self.state_path))
+ env.pop("TV_PORT", None)
+ if port:
+ env["TV_PORT"] = port
+ return subprocess.run(["bash", str(self.repo / "tools" / script), str(self.list_path)],
+ cwd=self.repo, env=env, capture_output=True, text=True, timeout=120)
+
+ def disable(self, host, port=None):
+ result = self.run_script("disable.sh", host, port)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ return result.stdout
+
+ def restore(self, host, port=None):
+ state = json.loads(self.state_path.read_text())
+ state["commands"] = []
+ self.state_path.write_text(json.dumps(state))
+ result = self.run_script("restore.sh", host, port)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertNotIn("no recovery record", result.stderr)
+ return self.sent(), result.stderr
+
+ def records(self):
+ return sorted(p.name.rsplit("-", 1)[0] for p in (self.home / ".cleanyourtv").glob("recovery-*.json"))
+
+ def rename_record(self, old_tag, new_tag):
+ [path] = (self.home / ".cleanyourtv").glob(f"recovery-{old_tag}-*.json")
+ path.rename(path.with_name(path.name.replace(f"recovery-{old_tag}-", f"recovery-{new_tag}-")))
+
+ def set_created(self, tag, created):
+ [path] = (self.home / ".cleanyourtv").glob(f"recovery-{tag}-*.json")
+ record = json.loads(path.read_text())
+ record["created"] = created
+ path.write_text(json.dumps(record))
+
+ def test_the_address_with_and_without_its_port_is_one_record(self):
+ forms = [("192.168.1.50", None), ("192.168.1.50:5555", None), ("192.168.1.50", "5555")]
+ # Each form saves once, and the next one keeps and restores that record.
+ for i, saved in enumerate(forms):
+ restored = forms[(i + 1) % len(forms)]
+ with self.subTest(saved=saved, restored=restored):
+ self.setUp()
+ self.tv()
+ self.disable(*saved)
+ self.assertEqual(self.records(), ["recovery-192.168.1.50"])
+ self.tv(**CLEANED_TV)
+ self.assertIn("keeping the recovery record", self.disable(*restored))
+ self.assertEqual(self.records(), ["recovery-192.168.1.50"])
+ self.assertEqual(self.restore(*restored)[0], USUAL_BATCH)
+
+ def test_a_record_under_the_old_name_with_the_port_is_still_used(self):
+ for typed, port, old_tag in (("192.168.1.50", None, "192.168.1.50_5555"),
+ ("192.168.1.50:5877", None, "192.168.1.50_5877"),
+ ("192.168.1.50", "5877", "192.168.1.50_5877")):
+ with self.subTest(typed=typed, port=port):
+ self.setUp()
+ self.tv()
+ self.disable(typed, port)
+ self.rename_record("192.168.1.50", old_tag)
+ self.tv(**CLEANED_TV)
+ out = self.disable(typed, port)
+ self.assertIn("keeping the recovery record", out)
+ self.assertEqual(self.records(), [f"recovery-{old_tag}"], "no second record of the cleaned TV")
+ sent, err = self.restore(typed, port)
+ self.assertEqual(sent, USUAL_BATCH)
+ self.assertIn("the name an older version gave", err)
+
+ def test_records_under_both_names_stop_before_the_tv_is_changed(self):
+ # They may be this TV before and after cleanup, or two TVs that had the address in turn.
+ self.tv()
+ self.disable("192.168.1.50")
+ records = self.home / ".cleanyourtv"
+ [first] = records.glob("recovery-192.168.1.50-*.json")
+ first.with_name(first.name.replace("192.168.1.50-", "192.168.1.50_5555-")).write_text(first.read_text())
+ for script in ("disable.sh", "restore.sh"):
+ with self.subTest(script=script):
+ state = json.loads(self.state_path.read_text())
+ state["commands"] = []
+ self.state_path.write_text(json.dumps(state))
+ result = self.run_script(script, "192.168.1.50")
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("two recovery records", result.stderr)
+ self.assertEqual([c for c in self.sent() if not c.startswith(("pm list", "cmd package query",
+ "cmd package resolve", "dumpsys", "getprop", "settings get"))], [],
+ "nothing is changed on the TV")
+
+ def test_ipv6_with_and_without_brackets_is_one_record_and_not_another_address(self):
+ self.tv()
+ self.disable("[fd00::50]:5555")
+ self.assertEqual(self.records(), ["recovery-fd00__50"])
+ self.assertEqual(self.restore("fd00::50")[0], USUAL_BATCH)
+ result = self.run_script("restore.sh", "fd00::5")
+ self.assertIn("no recovery record", result.stderr, "fd00::5 is another TV")
+
+ def test_a_relay_and_a_host_name_keep_the_name_as_typed(self):
+ for host, port, tag in (("127.0.0.1", "15555", "recovery-127.0.0.1"),
+ ("127.0.0.1:15556", None, "recovery-127.0.0.1_15556"),
+ ("mytv.local:5555", None, "recovery-mytv.local_5555")):
+ with self.subTest(host=host, port=port):
+ self.setUp()
+ self.tv()
+ self.disable(host, port)
+ self.assertEqual(self.records(), [tag], "unchanged from before this fix")
+ self.assertEqual(self.restore(host, port)[0], USUAL_BATCH)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/recovery.py b/tools/recovery.py
index a60a7d01c..f6f8bc9f2 100644
--- a/tools/recovery.py
+++ b/tools/recovery.py
@@ -15,6 +15,7 @@ half-applied list must not record the broken state as the original. A later run
the list gained since then, with their state before this list disabled them.
"""
import hashlib
+import ipaddress
import json
import os
from pathlib import Path
@@ -67,14 +68,75 @@ def read_list(path):
return names
-def record_path(list_path, host):
- """One record per TV and list. The list is identified by its absolute path, so a copy of the
- same file somewhere else gets its own record."""
- tag = re.sub(r"[^A-Za-z0-9.-]", "_", host or "no-host")
+def split_host_port(endpoint):
+ """host:port or [ipv6]:port as (host, port or None), the way tools/tv.py reads TV_HOST."""
+ if endpoint.startswith("["):
+ end = endpoint.find("]")
+ if end != -1:
+ rest = endpoint[end + 1:]
+ return endpoint[1:end], (rest[1:] if rest.startswith(":") else None)
+ elif endpoint.count(":") == 1:
+ host, port = endpoint.split(":", 1)
+ return host, port
+ return endpoint, None
+
+
+def lan_address(host):
+ """The TV's IP address when TV_HOST names a TV on the network, else None.
+
+ A loopback address (a relay, tools/relay.md) and a host name keep TV_HOST exactly as typed as the
+ record's name, as they always have; TV_PORT is not part of that name.
+ """
+ name, _ = split_host_port(host or "")
+ try:
+ return None if ipaddress.ip_address(name).is_loopback else name
+ except ValueError:
+ return None
+
+
+def record_name(list_path, tag):
digest = hashlib.sha1(str(Path(list_path).resolve()).encode()).hexdigest()[:8]
+ tag = re.sub(r"[^A-Za-z0-9.-]", "_", tag or "no-host")
return Path.home() / ".cleanyourtv" / f"recovery-{tag}-{digest}.json"
+def record_path(list_path, host):
+ """One record per TV and list. The list is identified by its absolute path, so a copy of the
+ same file somewhere else gets its own record. On the network a TV is its IP address:
+ TV_HOST=192.168.1.50, TV_HOST=192.168.1.50:5555 and TV_PORT=5555 reach the same TV, so they
+ share one record, named without the port."""
+ return record_name(list_path, lan_address(host) or host)
+
+
+def find_record(list_path, host):
+ """The record file for this TV and list: record_path's name, or the name older versions gave a
+ record on the network, TV_HOST exactly as typed with its port (recovery-192.168.1.50_5555-...).
+
+ Records under both names cannot be told apart from here: they may be this TV before and after
+ cleanup, or two TVs that had this address in turn. So nothing is chosen for the owner, and the
+ run stops before anything reaches the TV.
+ """
+ path = record_path(list_path, host)
+ address = lan_address(host)
+ if not address:
+ return path
+ port = split_host_port(host)[1] or os.environ.get("TV_PORT") or "5555"
+ typed = f"[{address}]:{port}" if ":" in address else f"{address}:{port}"
+ names = dict.fromkeys((path, record_name(list_path, typed), record_name(list_path, host)))
+ found = [name for name in names if name.exists()]
+ if not found:
+ return path
+ if len(found) > 1:
+ raise SystemExit("this TV and list have two recovery records, under the name older versions "
+ "gave it and the one\nit gets now:\n" + "".join(f" {name}\n" for name in found)
+ + "They may be this TV before and after cleanup, or two TVs that had this address "
+ "in turn.\nMove the one that is not this TV's state from before cleanup aside "
+ "deliberately, then run this again.\nNothing was changed on the TV.")
+ if found[0] != path:
+ sys.stderr.write(f"using {found[0]}, the name an older version gave this TV's record.\n")
+ return found[0]
+
+
def is_chooser(home):
"""Whether a Home component is Android's chooser rather than a launcher.
@@ -208,7 +270,7 @@ def package_state(name, installed, disabled):
def save(list_path, host):
- path = record_path(list_path, host)
+ path = find_record(list_path, host)
if path.exists():
record = load_record(path)
packages = read_list(list_path)
@@ -281,7 +343,7 @@ def restore_commands(list_path, stock, host):
packages = read_list(list_path)
if stock and not PACKAGE_RE.fullmatch(stock):
raise SystemExit(f"not a package name: {stock!r}")
- path = record_path(list_path, host)
+ path = find_record(list_path, host)
commands = []
if not path.exists():