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: manage.py keeps tv.py's exit status, so a TV it never reached is not a failed command
xbsheng12d2cb12f2ad
diff --git a/tests/test_manage.py b/tests/test_manage.pyindex 943e42bcc..8ab5b3e59 100644--- a/tests/test_manage.py+++ b/tests/test_manage.py@@ -60,15 +60,21 @@ class ManageTest(unittest.TestCase):return code, out.getvalue()def test_status_stops_when_the_tv_does_not_answer(self):- for answers, message in [- ([("", 1, "failed to connect")], "failed to connect"),- ([("package:com.a\n", 1, "")], "package:com.a"),- ([("", 255, "")], "tv.py exited with status 255"),- ([("", 0, ""), ("", 1, "unauthorized")], "unauthorized"),+ """The exit status is tv.py's - 2 for a TV it never reached, 1 for a command that failed - and the words+ it printed go to stderr, where a caller reads them."""+ for answers, code, message in [+ ([("", 1, "failed to connect")], 1, "failed to connect"),+ ([("package:com.a\n", 1, "")], 1, "package:com.a"),+ ([("", 255, "")], 1, "tv.py exited with status 255"),+ ([("", 0, ""), ("", 1, "unauthorized")], 1, "unauthorized"),+ ([("", 2, "127.0.0.1:1 refused the connection")], 2, "refused the connection"),]:- with self.subTest(message=message), self.assertRaises(SystemExit) as stop:- self.status(*answers)- self.assertEqual(stop.exception.code, message)+ with self.subTest(code=code, message=message):+ err = io.StringIO()+ with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as stop:+ self.status(*answers)+ self.assertEqual(stop.exception.code, code)+ self.assertIn(message, err.getvalue())def test_status_reads_an_empty_tv(self):code, out = self.status(("", 0, ""), ("", 0, ""))diff --git a/tests/test_manage_tv_status.py b/tests/test_manage_tv_status.pynew file mode 100644index 000000000..dc5a9f620--- /dev/null+++ b/tests/test_manage_tv_status.py@@ -0,0 +1,140 @@+"""tools/manage.py's exit status when the TV was never asked anything.++`tv()` ran `tools/tv.py` and turned any non-zero exit into `sys.exit(message)`, which is 1 - the status the+docstring keeps for "a command on the TV or a script this checkout runs failed". tools/tv.py answers 2 for+a TV it never reached (a refused port, a Wireless debugging port that answers TLS, an address with no route+to it, a Mac never granted Local Network), so `manage.py status` against a set that was asleep exited 1: a+status/install/remove that ran and failed. Reproduced on 10d76503 with `TV_HOST=127.0.0.1 TV_PORT=1`:+`tools/tv.py 'pm list packages'` exits 2, `tools/manage.py status` exits 1 with the same message. With no+TV_HOST at all, and in a clone that has no tools/venv, `tv()` and `tv_python()` answered 1 as well, while+check.py's own fixes (#2405) put both at 2 and b64push.py's (#2426) the missing ADB client.++The rule the tests below hold: nothing was asked of the TV -> 2, the TV ran the command and failed -> 1, and+the message is still on stderr either way. A status tools/tv.py does not document (it answers 0, 1 and 2) comes+from a crash of its own, so it is a command that failed: 1, and this script's three statuses stay true.+"""+import contextlib+import io+import os+import sys+import tempfile+import unittest+from pathlib import Path+from unittest import mock++ROOT = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT / "tools"))+import manage # noqa: E402++# tools/tv.py's own words for a port that refuses, the sentence manage.py re-emits.+REFUSED = ("127.0.0.1:1 refused the connection: nothing listens for ADB there, so network ADB is off or on "+ "another port.")+VENV_LINE = "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt"+++def stop(call, *args, **kwargs):+ """The SystemExit `call` raises, and what it wrote to stderr."""+ err = io.StringIO()+ with contextlib.redirect_stderr(err):+ with contextlib.redirect_stdout(io.StringIO()):+ with unittest.TestCase().assertRaises(SystemExit) as raised:+ call(*args, **kwargs)+ return raised.exception.code, err.getvalue()+++class TvStatusTest(unittest.TestCase):+ """`tv()` keeps tools/tv.py's status, and answers 2 before a TV was asked anything."""++ def test_no_tv_host_answers_2_and_names_the_setting(self):+ environ = {k: v for k, v in os.environ.items() if k != "TV_HOST"}+ with mock.patch.dict(os.environ, environ, clear=True), \+ mock.patch.object(manage.subprocess, "run") as run:+ code, err = stop(manage.tv, "pm list packages")+ self.assertEqual(2, code)+ self.assertIn("set TV_HOST to the TV IP address", err)+ # tools/tv.py's own hint, which this line dropped when it was a one-line sys.exit+ self.assertIn("Settings → Network", err)+ self.assertIn("Nothing was asked of the TV.", err)+ run.assert_not_called()++ def test_a_refused_port_keeps_tools_tv_py_status_2(self):+ done = mock.Mock(returncode=2, stderr=REFUSED + "\n", stdout="")+ with mock.patch.dict(os.environ, {"TV_HOST": "127.0.0.1", "TV_PORT": "1"}), \+ mock.patch.object(manage, "tv_python", return_value="python"), \+ mock.patch.object(manage.subprocess, "run", return_value=done):+ code, err = stop(manage.tv, "pm list packages")+ self.assertEqual(2, code)+ self.assertIn(REFUSED, err)++ def test_a_command_the_tv_failed_keeps_1(self):+ done = mock.Mock(returncode=1, stderr="Error: java.lang.SecurityException\n", stdout="")+ with mock.patch.dict(os.environ, {"TV_HOST": "192.168.1.50"}), \+ mock.patch.object(manage, "tv_python", return_value="python"), \+ mock.patch.object(manage.subprocess, "run", return_value=done):+ code, err = stop(manage.tv, "pm uninstall com.example.addon")+ self.assertEqual(1, code)+ self.assertIn("SecurityException", err)++ def test_a_tv_py_that_said_nothing_still_gives_its_status_and_a_line(self):+ done = mock.Mock(returncode=2, stderr="", stdout="")+ with mock.patch.dict(os.environ, {"TV_HOST": "192.168.1.50"}), \+ mock.patch.object(manage, "tv_python", return_value="python"), \+ mock.patch.object(manage.subprocess, "run", return_value=done):+ code, err = stop(manage.tv, "pm list packages")+ self.assertEqual(2, code)+ self.assertIn("tv.py exited with status 2", err)++ def test_a_status_tv_py_does_not_document_is_a_failed_command(self):+ """tools/tv.py documents 0, 1 and 2; anything else comes from a crash of its own, which is a command+ that failed - and this script's docstring still promises no status but 0, 1 and 2."""+ for returncode in (3, 130, 255):+ with self.subTest(returncode=returncode):+ done = mock.Mock(returncode=returncode, stderr="", stdout="")+ with mock.patch.dict(os.environ, {"TV_HOST": "192.168.1.50"}), \+ mock.patch.object(manage, "tv_python", return_value="python"), \+ mock.patch.object(manage.subprocess, "run", return_value=done):+ code, err = stop(manage.tv, "pm list packages")+ self.assertEqual(1, code)+ self.assertIn(f"tv.py exited with status {returncode}", err)++ def test_a_tv_that_answered_returns_its_output(self):+ done = mock.Mock(returncode=0, stderr="", stdout="package:com.example.a\n")+ with mock.patch.dict(os.environ, {"TV_HOST": "192.168.1.50"}), \+ mock.patch.object(manage, "tv_python", return_value="python"), \+ mock.patch.object(manage.subprocess, "run", return_value=done):+ self.assertEqual("package:com.example.a\n", manage.tv("pm list packages"))+++class NoVenvTest(unittest.TestCase):+ """A clone that has no tools/venv answers 2, as b64push.py does for the same shape."""++ def test_no_venv_answers_2_and_says_how_to_make_one(self):+ with tempfile.TemporaryDirectory() as empty:+ with mock.patch.object(manage, "TOOLS", Path(empty)):+ code, err = stop(manage.tv_python)+ self.assertEqual(2, code)+ self.assertIn("cannot load tools/tv.py", err)+ self.assertIn(VENV_LINE, err)+ self.assertIn("Nothing was asked of the TV.", err)++ def test_the_venv_is_still_found_when_it_is_there(self):+ with tempfile.TemporaryDirectory() as tree:+ python = Path(tree) / "venv" / "bin" / "python"+ python.parent.mkdir(parents=True)+ python.write_text("#!/bin/sh\n", encoding="utf-8")+ with mock.patch.object(manage, "TOOLS", Path(tree)):+ self.assertEqual(str(python), manage.tv_python())+++class InstallPushTest(unittest.TestCase):+ """The other path to tools/tv.py, `install`'s --push, already gave its status back."""++ def test_a_push_that_never_reached_the_tv_keeps_2(self):+ item = {"id": "frigate-glance", "kind": "add-on", "script": "install.sh", "package": "com.example.a"}+ with mock.patch.object(manage, "run", return_value=2) as run:+ self.assertEqual(2, manage.install_addon(item, []))+ self.assertEqual("bash", run.call_args[0][0][0])+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/manage.py b/tools/manage.pyindex 354a6c9bc..ab0426eae 100644--- a/tools/manage.py+++ b/tools/manage.py@@ -25,7 +25,10 @@ of the TV.Exit status: 0 the command ran, 1 a command on the TV or a script this checkout runs failed, 2 the command lineasked for something this script does not do - a command it does not have, or a word the command would drop - which-is answered before the READMEs are read or the TV is asked anything.+is answered before the READMEs are read or the TV is asked anything. 2 is also a run that ends with nothing asked of+the TV: no TV_HOST, a checkout with no tools/venv, and a TV tools/tv.py answered 2 for because it never reached it+(a refused port, a Wireless debugging port that answers TLS, an address with no route to it). check.py answers the+first two of those with 2 as well, and b64push.py the second."""import jsonimport math@@ -297,16 +300,37 @@ def tv_python():for p in (TOOLS / "venv" / "bin" / "python", TOOLS / "venv" / "Scripts" / "python.exe"):if p.is_file():return str(p)- sys.exit("run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt")+ # Nothing reached the TV: this checkout has no tools/venv, so tools/tv.py cannot even be imported by a+ # Python that has adb_shell. sys.exit(str) answers 1, which the docstring keeps for a command on the TV+ # or a script of this checkout that failed, so a clone with no venv read as an install or a removal that+ # ran and failed - and status/install/remove would have changed nothing either way. b64push.py answers+ # this shape, and check.py the same and the one below, with 2.+ sys.stderr.write("cannot load tools/tv.py (tools/venv is not there).\n"+ "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt\n"+ "Nothing was asked of the TV.\n")+ raise SystemExit(2)def tv(command):if not os.environ.get("TV_HOST"):- sys.exit("set TV_HOST to the TV IP address")+ # The same two lines tools/tv.py prints for this, and the status check.py answers it with: without+ # TV_HOST there is no TV to ask anything, which is not the same as a command that failed on one.+ sys.stderr.write("set TV_HOST to the TV IP address, e.g. TV_HOST=192.168.1.50 tools/tv.py --probe\n"+ "(find it under Settings → Network, or look for the TV in `arp -a`)\n"+ "Nothing was asked of the TV.\n")+ raise SystemExit(2)r = subprocess.run([tv_python(), "-I", str(TOOLS / "tv.py"), command], capture_output=True, text=True)# A failed read is unknown, not empty: never report "not installed" for a TV that didn't answer.if r.returncode:- sys.exit(r.stderr.strip() or r.stdout.strip() or f"tv.py exited with status {r.returncode}")+ sys.stderr.write((r.stderr.strip() or r.stdout.strip()+ or f"tv.py exited with status {r.returncode}") + "\n")+ # tools/tv.py answers 2 for a TV it never reached (a refused port, a Wireless debugging port that answers+ # TLS, an address with no route to it, a Mac that was never granted Local Network) and 1 for a command the+ # TV ran and failed; a status it does not document, from a crash of its own, is a command that failed too.+ # Flattened to 1, a set that was asleep read as a status/install/remove that ran and failed, which is the+ # one thing the reader of this status has to tell apart: install and remove change the TV, so "nothing was+ # asked of it" cannot look like "the TV refused it".+ raise SystemExit(2 if r.returncode == 2 else 1)return r.stdout