Skip to content

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

tools: recovery.py says when a new address makes a fresh record of a cleaned TV (#2139)

xbsheng27b6f44408bb
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index dad37d2f4..ce23fd16f 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -734,6 +734,16 @@ Things that bit us or contributors, so they don't bite you. Commands use `tools/
package that also claims HOME (PatchWall on Xiaomi) makes Android drop the default. The install
script now picks the known stock launcher among the candidates by itself; otherwise choose one on
the TV or pass `STOCK_LAUNCHER=<package>`.
+- **`tools/restore.sh` leaves the packages an earlier batch disabled off, after the TV changed its
+ address mid-cleanup.** The recovery record is one file per list and address
+ (`~/.cleanyourtv/recovery-<address>-<list hash>.json`), so a new DHCP lease - a router restart, or
+ the Wi-Fi forget and re-join the setupwraith step asks for itself - makes the next
+ `tools/disable.sh` run find no record and write a fresh one from a TV that is already partly
+ cleaned: every package the earlier batches disabled is in it as "already disabled, left alone by
+ undo", and undo at the new address leaves them off. `tools/disable.sh` now says so on stderr, names
+ the record under the old address and the packages the two disagree about. Run `tools/restore.sh` at
+ the old address first, or move that record to the new address's name once you are sure it is this
+ set and not a second one cleaned with the same list ([#2139](https://github.com/Gathin23/cleanyourtv/issues/2139)).
- **Stremio fails every stream with `ConnectException: Failed to connect to /127.0.0.1:11470`**,
and only Force stop brings it back until the next reboot. This is not the debloat. It is a Stremio
regression in 1.11 ([stremio-bugs#2866](https://github.com/Stremio/stremio-bugs/issues/2866)):
diff --git a/tests/test_recovery_address_change.py b/tests/test_recovery_address_change.py
new file mode 100644
index 000000000..3f0600fd2
--- /dev/null
+++ b/tests/test_recovery_address_change.py
@@ -0,0 +1,185 @@
+"""A recovery record is per address, so a new lease is a new record: save() says so (#2139).
+
+The record is one file per list and address (recovery-<address>-<list hash>.json). The setup
+instructions move that address mid-session themselves: the setupwraith step asks the owner to forget
+and re-join the TV's Wi-Fi network, and the router hands out a new lease. The next `tools/disable.sh`
+run then finds no record for the new address and writes a fresh one, and the fresh one is taken from a
+TV that is already partly cleaned: every package the earlier batches disabled reads "already disabled,
+left alone by undo", so `tools/restore.sh` at the new address leaves them off for good. Nothing in the
+run's output pointed at the address change.
+
+save() now names the record under the other address, the packages the two disagree about, and the two
+ways out. It is output on stderr and nothing else: the record is written, the list still reaches the
+TV, and a second set cleaned with the same list keeps both records. 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 shutil
+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
+# The TV after a first batch: the package the list disables is off now, as the record predicted.
+CLEANED_TV = {"disabled": ["com.example.enabled", "com.example.already_off"]}
+
+
+class AddressChangeWarningTest(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, list_path=None):
+ if os.name != "posix" or not shutil.which("bash"):
+ raise unittest.SkipTest(f"{script} is a POSIX script; it needs bash")
+ env = dict(os.environ, TV_HOST=host, HOME=str(self.home), FAKE_TV_STATE=str(self.state_path))
+ env.pop("TV_PORT", None)
+ return subprocess.run(["bash", str(self.repo / "tools" / script), str(list_path or self.list_path)],
+ cwd=self.repo, env=env, capture_output=True, text=True, timeout=120)
+
+ def disable(self, host, list_path=None):
+ result = self.run_script("disable.sh", host, list_path)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ return result
+
+ def restore(self, host, list_path=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, list_path)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ return self.sent()
+
+ def records(self):
+ return sorted(p.name.split("-", 2)[1] for p in (self.home / ".cleanyourtv").glob("recovery-*.json"))
+
+ def test_the_first_record_says_nothing_about_another_address(self):
+ self.tv()
+ result = self.disable("192.168.1.50")
+ self.assertNotIn("warning:", result.stderr)
+ self.assertEqual(self.records(), ["192.168.1.50"])
+
+ def test_a_second_run_on_the_same_address_says_nothing(self):
+ self.tv()
+ self.disable("192.168.1.50")
+ self.tv(**CLEANED_TV)
+ result = self.disable("192.168.1.50")
+ self.assertIn("keeping the recovery record", result.stdout)
+ self.assertNotIn("warning:", result.stderr, "the record it keeps already says what was on")
+ self.assertEqual(self.records(), ["192.168.1.50"])
+
+ def test_a_new_address_after_the_cleanup_names_the_old_record(self):
+ self.tv()
+ self.disable("192.168.1.50")
+ self.tv(**CLEANED_TV)
+
+ result = self.disable("192.168.1.11")
+
+ self.assertEqual(result.returncode, 0, "the record is written and the list still runs")
+ self.assertIn("warning:", result.stderr)
+ [old] = (self.home / ".cleanyourtv").glob("recovery-192.168.1.50-*.json")
+ self.assertIn(str(old), result.stderr, "the record that still describes the TV before cleanup")
+ self.assertIn("1 of the packages disabled now are recorded there as enabled", result.stderr)
+ self.assertIn("com.example.enabled", result.stderr)
+ self.assertIn("tools/restore.sh", result.stderr)
+ [new] = (self.home / ".cleanyourtv").glob("recovery-192.168.1.11-*.json")
+ self.assertIn(f"~/.cleanyourtv/{new.name}", result.stderr, "where to move it to")
+ self.assertIn("both records stay", result.stderr, "a second set on the same list is not an error")
+ self.assertEqual(self.records(), ["192.168.1.11", "192.168.1.50"])
+ self.assertIn("pm disable-user --user 0 com.example.enabled", self.sent())
+
+ def test_undo_at_the_new_address_is_what_the_warning_is_about(self):
+ self.tv()
+ self.disable("192.168.1.50")
+ self.tv(**CLEANED_TV)
+ self.disable("192.168.1.11")
+
+ sent = self.restore("192.168.1.11")
+
+ self.assertNotIn("pm enable com.example.enabled", sent,
+ "the fresh record holds it as already disabled, so undo leaves it off")
+ # The record from the old address is the one that would put it back.
+ self.assertIn("pm enable com.example.enabled", self.restore("192.168.1.50"))
+
+ def test_a_second_set_under_another_address_says_nothing(self):
+ # Both TVs are as they were before any list: the old record agrees with what is disabled now,
+ # so there is nothing to warn about even though the package name is in both.
+ self.tv()
+ self.disable("192.168.1.50")
+ self.disable("192.168.1.11")
+ result = self.disable("192.168.1.12")
+ self.assertNotIn("warning:", result.stderr)
+ self.assertEqual(self.records(), ["192.168.1.11", "192.168.1.12", "192.168.1.50"])
+
+ def test_another_list_under_another_address_says_nothing(self):
+ self.tv()
+ self.disable("192.168.1.50")
+ self.tv(**CLEANED_TV)
+ other = self.base / "second-list.txt"
+ other.write_text("# Another TV\ncom.example.enabled\n")
+ result = self.disable("192.168.1.11", list_path=other)
+ self.assertNotIn("warning:", result.stderr, "a record for another list is another TV's state")
+ self.assertEqual(len(list((self.home / ".cleanyourtv").glob("recovery-*.json"))), 2)
+
+ def test_a_record_that_cannot_be_read_does_not_stop_the_run(self):
+ self.tv()
+ self.disable("192.168.1.50")
+ [old] = (self.home / ".cleanyourtv").glob("recovery-192.168.1.50-*.json")
+ old.write_text("{not json")
+ self.tv(**CLEANED_TV)
+ result = self.disable("192.168.1.11")
+ self.assertEqual(result.returncode, 0, "a damaged record under another address is not this run's")
+ self.assertNotIn("Traceback", result.stderr)
+ self.assertNotIn("warning:", result.stderr)
+ self.assertEqual(old.read_text(), "{not json", "the file is left where it is")
+
+
+class ThePageAndTheCodeAgreeTest(unittest.TestCase):
+ """The troubleshooting entry and the warning in tools/recovery.py say the same thing.
+
+ The page is where an owner who already lost the packages looks, and the warning is what the next
+ run prints; one of them drifting leaves the other pointing at a file that is not there.
+ """
+ ROOT = Path(__file__).resolve().parents[1]
+
+ def page_bullet(self):
+ text = (self.ROOT / "docs/TROUBLESHOOTING.md").read_text(encoding="utf-8")
+ start = text.index("- **`tools/restore.sh` leaves the packages an earlier batch disabled off")
+ end = text.index("\n- **", start + 1)
+ return " ".join(text[start:end].split())
+
+ def test_the_page_names_the_record_the_fresh_copy_and_the_way_out(self):
+ bullet = self.page_bullet()
+ for phrase in ("~/.cleanyourtv/recovery-<address>-<list hash>.json",
+ "already disabled, left alone by undo",
+ "`tools/disable.sh` now says so on stderr",
+ "Run `tools/restore.sh` at the old address first",
+ "#2139"):
+ self.assertIn(phrase, bullet)
+ self.assertIn("not a second one cleaned with the same list", bullet,
+ "a second set is not a lost cleanup, and the page has to say so")
+
+ def test_the_warning_the_page_describes_is_the_one_save_prints(self):
+ source = (self.ROOT / "tools/recovery.py").read_text(encoding="utf-8")
+ self.assertIn('f"recovery-{tag}-{digest}.json"', source, "the name the page quotes")
+ self.assertIn("warning: a new record was written for this list", source)
+ self.assertIn("Run tools/restore.sh ", source, "the way out the page names")
+ # Nothing else in the file prints this warning, and it is save()'s: the run that writes a
+ # record and disables nothing has to say it, not --commands.
+ save = source[source.index("def save(list_path, host):"):source.index("def restore_commands(")]
+ self.assertIn("warning: a new record was written for this list", save)
+ self.assertIn("cleaned_by_another_record(list_path, path, disabled)", save)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/recovery.py b/tools/recovery.py
index 805ef9729..473fd5e55 100644
--- a/tools/recovery.py
+++ b/tools/recovery.py
@@ -11,7 +11,9 @@ before. Both scripts now go through this record: `disable.sh` writes it before t
reaches the TV, and `restore.sh` replays it.
Records live in ~/.cleanyourtv/, one per TV and list. The first one wins: a retry after a
-half-applied list must not record the broken state as the original. A later run, and check.py --fix,
+half-applied list must not record the broken state as the original. The TV is the address it was
+reached at, so a set that takes a new address gets a new record: save() warns when a record under
+another address still holds, as enabled, a package it just found disabled (#2139). A later run, and check.py --fix,
only add the names the list gained since then, with their state before this list disabled them, and
record a package that was absent then and is installed and enabled now as enabled.
"""
@@ -114,12 +116,63 @@ def lan_address(host):
return None
+def list_digest(list_path):
+ """The eight hex characters in a record's name that identify the list it was taken for."""
+ return hashlib.sha1(str(Path(list_path).resolve()).encode()).hexdigest()[:8]
+
+
def record_name(list_path, tag):
- digest = hashlib.sha1(str(Path(list_path).resolve()).encode()).hexdigest()[:8]
+ digest = list_digest(list_path)
tag = re.sub(r"[^A-Za-z0-9.-]", "_", tag or "no-host")
return Path.home() / ".cleanyourtv" / f"recovery-{tag}-{digest}.json"
+def records_under_other_addresses(list_path, path):
+ """The other records taken for this same list, as (tag, path), the file this run writes left out.
+
+ A record is one file per TV and list, and on the network the TV is the address it was reached at,
+ so a set that takes a new DHCP lease leaves its record behind under the old address: the next run
+ finds no record for the new one and writes a fresh one out of a TV that is already cleaned (#2139).
+ """
+ folder = path.parent
+ if not folder.is_dir():
+ return []
+ digest = list_digest(list_path)
+ suffix = f"-{digest}.json"
+ found = []
+ for other in sorted(folder.glob(f"recovery-*{suffix}")):
+ if other.name == path.name:
+ continue
+ found.append((other.name[len("recovery-"):-len(suffix)], other))
+ return found
+
+
+def cleaned_by_another_record(list_path, path, disabled):
+ """The record for this list, under another address, that reads like this TV before an earlier run.
+
+ A package this run finds disabled, which that record holds as enabled before its own cleanup, is
+ the shape of the same TV at a new address: the earlier run disabled it, and the record it left is
+ the only place that still says it was on. A second TV cleaned with the same list is the other
+ reading, and the warning says so rather than being sure here. A record that cannot be read is left
+ to the run that owns it: this is one more line of output, not a reason to stop.
+ """
+ found = []
+ for tag, other in records_under_other_addresses(list_path, path):
+ try:
+ with open(other) as handle:
+ record = json.load(handle)
+ except (OSError, ValueError):
+ continue
+ recorded = record.get("packages") if isinstance(record, dict) else None
+ if not isinstance(recorded, dict):
+ continue
+ names = sorted(name for name, state in recorded.items()
+ if state == "enabled" and name in disabled)
+ if names:
+ found.append((tag, other, names))
+ return found
+
+
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:
@@ -415,6 +468,23 @@ def save(list_path, host):
else:
print(f"HOME before: {home or 'no component answered'}")
print("undo with tools/restore.sh <same list>; keep this file, it is the only copy.")
+ # A fresh record is written whenever this address has none, which is also what a TV that took a
+ # new DHCP lease looks like: the record from before its cleanup is still under the old address,
+ # and nothing in it reaches undo from here (#2139). Say so while the owner is still at the TV.
+ for tag, other, names in cleaned_by_another_record(list_path, path, disabled):
+ shown = ", ".join(names[:5]) + (", ..." if len(names) > 5 else "")
+ sys.stderr.write(
+ f"warning: a new record was written for this list, and one for the same list is under "
+ f"{tag}:\n"
+ f" {other}\n"
+ f"{len(names)} of the packages disabled now are recorded there as enabled before an "
+ f"earlier cleanup ({shown}).\n"
+ "If this is that TV on a new address (a new DHCP lease, or the Wi-Fi forget and re-join "
+ "the setupwraith step asks for), undo run from here leaves those off. Run tools/restore.sh "
+ "at the old address first, or move that record to "
+ f"~/.cleanyourtv/{path.name} once you are sure it is this TV.\n"
+ "If this is a second set cleaned with the same list, nothing is wrong: both records "
+ "stay.\n")
return 0