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: check.py exits 2, not 1, when TV_HOST is unset or the ADB client is missing
xbsheng7e1a43405e5b
diff --git a/docs/firmware-updates.md b/docs/firmware-updates.mdindex 2822eb00a..78f63a676 100644--- a/docs/firmware-updates.md+++ b/docs/firmware-updates.md@@ -38,8 +38,9 @@ well, and a weekly job on a home server or a Pi is enough to hear about it:````TV_HOST` is read before any of that: a run that leaves it unset stops in `tools/tv.py` with its own-message and also exits 1, so set it in the job line, as above, where cron does not inherit it from-your shell.+message and exits 2, the code for a check that never ran, so set it in the job line, as above, where+cron does not inherit it from your shell. A job on a computer where the tools' venv is gone answers 2+the same way, naming the line that builds it.The job only gets an answer from a TV that keeps network ADB listening between runs and hasaccepted "Allow USB debugging?" for the computer that runs it with "Always allow from this computer"diff --git a/tests/test_check_exit_codes.py b/tests/test_check_exit_codes.pyindex 7fd32fbc4..755c950c4 100644--- a/tests/test_check_exit_codes.py+++ b/tests/test_check_exit_codes.py@@ -6,9 +6,11 @@ drift and nothing else had a name. check.py answers `0` when the TV still matchesomething drifted, and `2` when the run could not check at all (the TV did not answer, nobody acceptedthe "Allow USB debugging?" prompt, the list path is not there, the list disables Android's corepackages); its own source says why the two are kept apart - "exit 2, not the 1 a weekly job reads as-drift". A job built from the page alone could therefore not tell "the TV drifted" from "the check-never ran", and one more case ends at 1 as well: with `TV_HOST` unset the run stops in tools/tv.py-before any of the above, with tv.py's own message and its own exit code, which is 1.+"drift". A job built from the page alone could therefore not tell "the TV drifted" from "the check+never ran". Two shapes that stop before any connection used to end at 1 for the same reason: with+`TV_HOST` unset the run stops in tools/tv.py with its own message, and on a computer without the ADB+client recovery.connect() stops with the line that builds the venv - both SystemExit, not Exception,+so the clause that answers 2 never saw them.The section has since moved out of the README into docs/firmware-updates.md ("Exit codes, and a weeklyjob"), which the README's "After a firmware update" links; the checks below read it there.@@ -131,16 +133,20 @@ class CheckExitCodeTest(unittest.TestCase):state.update(overrides)self.state_path.write_text(json.dumps(state))- def run_check(self, *args, host="192.168.1.50"):+ def run_check(self, *args, host="192.168.1.50", python_args=()):env = dict(os.environ, HOME=str(self.home), FAKE_TV_STATE=str(self.state_path))env.pop("TV_HOST", None)if host is not None:env["TV_HOST"] = hostreturn subprocess.run(- [sys.executable, str(self.repo / "tools" / "check.py"), *args],+ [sys.executable, *python_args, str(self.repo / "tools" / "check.py"), *args],cwd=self.repo, env=env, capture_output=True, text=True, timeout=120,)+ def commands_sent(self):+ """What the run sent the (simulated) TV: empty means it was never asked."""+ return json.loads(self.state_path.read_text()).get("commands", [])+def test_the_checker_answers_three_codes(self):codes = literal_returns(CHECK.read_text(encoding="utf-8"))self.assertEqual(codes, {0, 1, 2},@@ -168,11 +174,12 @@ class CheckExitCodeTest(unittest.TestCase):result = self.run_check(str(self.base / "gone.txt"))self.assertEqual(result.returncode, 2, result.stdout + result.stderr)- def test_a_run_without_tv_host_ends_at_1(self):- # The real tv.py, not the fake: it stops at import time with its own message and exit code- # 1 - the same code the page calls drift. Two messages are possible: the TV_HOST note from- # tv.py, or recovery.py's note when adb_shell is not installed (CI runs the suite without- # the venv the tools use).+ def test_a_run_without_tv_host_is_2(self):+ # The real tv.py, not the fake: it stops at import time with its own message, before any+ # connection, so the TV was never asked and the run answers 2 - the code the page calls "a+ # check that never ran" - rather than 1, which the page tells a weekly job to mail about.+ # Which of the two messages comes back depends on the interpreter: the TV_HOST note here, or+ # recovery.py's note when adb_shell is not installed (CI runs the suite without the venv).source = (ROOT / "tools" / "recovery.py").read_text(encoding="utf-8")host_note = "Set TV_HOST to your TV's IP address"loader_note = "cannot load tools/tv.py"@@ -181,9 +188,28 @@ class CheckExitCodeTest(unittest.TestCase):shutil.copy2(ROOT / "tools" / "tv.py", self.repo / "tools" / "tv.py")self.tv()result = self.run_check(str(self.list_path), host=None)- self.assertEqual(result.returncode, 1, result.stdout + result.stderr)+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)self.assertTrue(host_note in result.stderr or loader_note in result.stderr, result.stderr)+ self.assertIn("Nothing was checked or changed.", result.stderr)self.assertNotIn("could not read the TV", result.stderr)+ self.assertNotIn("Traceback", result.stderr)+ self.assertEqual(result.stdout, "", "the report of a run that never asked the TV")+ self.assertEqual([], self.commands_sent(), "the TV was asked by a run that could not read it")++ def test_a_run_without_the_adb_client_is_2(self):+ # -S drops site-packages, so adb_shell cannot be imported even under tools/venv's Python: the+ # same shape on a computer where the venv is gone, which is the other message recovery.py+ # stops with. The line that builds the venv is the whole answer the owner needs.+ shutil.copy2(ROOT / "tools" / "tv.py", self.repo / "tools" / "tv.py")+ self.tv()+ result = self.run_check(str(self.list_path), python_args=("-S",))+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertIn("cannot load tools/tv.py", result.stderr)+ self.assertIn("python3 -m venv tools/venv", result.stderr)+ self.assertIn("Nothing was checked or changed.", result.stderr)+ self.assertNotIn("Traceback", result.stderr)+ self.assertEqual(result.stdout, "")+ self.assertEqual([], self.commands_sent())def test_the_readme_names_every_code_the_checker_can_return(self):self.assertIn("(docs/firmware-updates.md)", "\n".join(readme_section(README.read_text(encoding="utf-8"))))diff --git a/tests/test_drift_check_adb_errors.py b/tests/test_drift_check_adb_errors.pyindex ce1d66549..b62ec69d9 100644--- a/tests/test_drift_check_adb_errors.py+++ b/tests/test_drift_check_adb_errors.py@@ -147,15 +147,21 @@ class CheckAdbErrorTest(unittest.TestCase):self.assertIn("KeyboardInterrupt", result.stderr)self.assertNotIn("could not read the TV", result.stderr)- def test_an_exit_from_tv_py_is_left_as_it_is(self):+ def test_an_exit_from_tv_py_is_2(self):# tv.py and recovery.py stop with SystemExit and their own message (TV_HOST unset, adb_shell- # missing); that is not an ADB error and keeps its own wording and exit code.+ # missing); that is not an ADB error, so it keeps its own wording, but the TV was never asked+ # and the answer is 2 - the code for a check that never ran - not the 1 Python gives it, which+ # the page tells a weekly job to mail about as drift.message = "Set TV_HOST to your TV's IP address"self.tv(connect_raises={"type": "SystemExit", "message": message})result = self.run_check(str(self.list_path))- self.assertEqual(result.returncode, 1, result.stdout + result.stderr)+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)self.assertIn(message, result.stderr)+ self.assertIn("Nothing was checked or changed.", result.stderr)self.assertNotIn("could not read the TV", result.stderr)+ self.assertNotIn("Traceback", result.stderr)+ self.assertEqual(result.stdout, "")+ self.assertEqual(self.sent(), [], "the TV was asked by a run that could not read it")if __name__ == "__main__":diff --git a/tools/check.py b/tools/check.pyindex a6934348c..24a9711ba 100644--- a/tools/check.py+++ b/tools/check.py@@ -22,12 +22,13 @@ moment those come back into play: the update can turn setupwraith back on for aowns, and a fix that skipped the checks would take that TV off Wi-Fi with no way back in.Exit codes: 0 in sync (or fixed), 1 drift, an incomplete fix, or packages a safety check left on,-2 the TV or the list could not be read, which includes ADB timing out or failing partway (after which a---fix may be partial, and the next check says what is left), or the command line asked for something this-script does not do, which includes naming a launcher the TV does not have. That last one matters:-README.md tells an owner to run this from a weekly job and hear about it on exit 1, so a mistyped flag-must not come back as drift, nor a mistyped launcher as "nothing drifted". A recovery record --fix-cannot read or update is 2 as well, with nothing changed on the TV.+2 the TV or the list could not be read, which includes ADB timing out or failing partway (after which+a --fix may be partial, and the next check says what is left), a run with no TV_HOST set or no ADB+client to read the TV with, or the command line asked for something this script does not do, which+includes naming a launcher the TV does not have. That last one matters: README.md tells an owner to+run this from a weekly job and hear about it on exit 1, so a mistyped flag must not come back as+drift, nor a mistyped launcher as "nothing drifted". A recovery record --fix cannot read or update is+2 as well, with nothing changed on the TV."""import osimport re@@ -380,12 +381,20 @@ def main():# authorisations were revoked, or for a key the TV has not seen) or when the TV goes to standby# partway, and AdbTimeoutError and DeviceAuthError. Left alone they end the run with a traceback# and exit 1, which a cron job reads as drift, so every Exception here is exit 2, the TV could- # not be read. SystemExit and KeyboardInterrupt are not Exceptions: tv.py's own exits (TV_HOST- # unset), the refusals in recovery.py and in this file once --fix has started, and Ctrl-C still- # stop the run as before.+ # not be read. The two shapes that stop the run before any connection are SystemExit rather than+ # Exception, so they need the clause below: tv.py stops with its own message when TV_HOST is+ # unset, and recovery.connect() stops with one when adb_shell is not installed. Both mean the TV+ # was never asked, and both used to answer 1 - "the TV drifted" for a job that never ran.probe = "tools/venv/bin/python -I tools/tv.py --probe"try:dev = recovery.connect()+ except SystemExit as e:+ # A string code is one of those two messages: print it and answer 2. Nothing else in tv.py+ # or recovery.py exits with a string, so a numeric one (or none) keeps its own meaning.+ if not isinstance(e.code, str):+ raise+ sys.stderr.write(f"{e.code}\nNothing was checked or changed.\n")+ return 2except (OSError, ConnectionError) as e:sys.stderr.write(f"could not reach the TV ({e}). Check it is on and TV_HOST is right "f"({probe}).\n")