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 0 or 1, for a mistyped launcher, an unusable list or a TV that lists no packages
Mazin Kanugae250772bd12c
diff --git a/tests/test_check_command_line.py b/tests/test_check_command_line.pyindex af6385546..7255a48a9 100644--- a/tests/test_check_command_line.py+++ b/tests/test_check_command_line.py@@ -6,6 +6,11 @@ run must therefore not come back as 1: `check.py --help` - the first thing a reathey do not know - was taken as a list named `--help` and ended in a FileNotFoundError traceback withexit 1, and a flag typed where the launcher package goes reached set-home-activity as a package name.+The same goes for what the command line points at. A list check.py cannot use, a launcher package the+TV does not have and a TV that answers no packages are exit 2 too. The launcher one was worse than exit+1: a typo there turned the Home check off, so Home on the wrong launcher came back as "nothing drifted",+exit 0, and a weekly job stayed quiet.+Run from the repository root: python3 -m unittest discover -s tests -v"""import json@@ -23,6 +28,8 @@ from test_drift_check import FAKE_TV # noqa: E402 (the simulated TV check.py aROOT = Path(__file__).resolve().parents[1]STOCK = "com.google.android.tvlauncher"CUSTOM = "com.gathin.tvhome"+# Google TV's stock launcher, which the list leaves out: build-and-install.sh disables it instead.+LAUNCHERX = "com.google.android.apps.tv.launcherx"def collapsed(text):@@ -159,6 +166,91 @@ class CheckCommandLineTest(unittest.TestCase):self.assertNotIn("Traceback", result.stderr)self.assertEqual(self.sent(), [])+ def test_a_list_with_every_line_commented_out_is_not_drift(self):+ # CONTRIBUTING has untested entries commented out, so a list can be all comments.+ self.list_path.write_text("# My TV\n# com.example.music untested\n")+ self.tv()+ result = self.run_check(str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertIn("no package names in this list", result.stderr)+ self.assertIn("Nothing was checked or changed", result.stderr)+ self.assert_untouched(result)++ def test_a_line_that_is_no_package_name_is_not_drift(self):+ # A curly quote from a word processor, in a report-only run as the weekly job makes it.+ self.list_path.write_text("# My TV\ncom.example.music\n“com.example.store”\n")+ self.tv()+ result = self.run_check(str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stderr)+ self.assertIn("debloat.txt:3: not a package name", result.stderr)+ self.assertEqual(result.stdout, "")+ self.assert_untouched(result)++ def drifted_home(self, **overrides):+ """A firmware update gave Home back to launcherx, which the list does not name."""+ state = {"installed": ["com.example.music", "com.example.store", STOCK, CUSTOM, LAUNCHERX],+ "home": f"{LAUNCHERX}/.home.HomeActivity",+ "home_activities": [f"{LAUNCHERX}/.home.HomeActivity", f"{CUSTOM}/.MainActivity"]}+ self.tv(**dict(state, **overrides))++ def test_a_launcher_that_is_not_installed_is_refused_not_ignored(self):+ self.drifted_home()+ named = self.run_check(str(self.list_path), CUSTOM)+ self.assertEqual(named.returncode, 1, named.stdout + named.stderr)+ self.assertIn(f"Home is {LAUNCHERX}/.home.HomeActivity, not {CUSTOM}", named.stdout)+ # One letter short: it used to turn the Home check off and report "nothing drifted", exit 0.+ typo = self.run_check(str(self.list_path), "com.gathin.tvhom")+ self.assertEqual(typo.returncode, 2, typo.stdout + typo.stderr)+ self.assertIn("com.gathin.tvhom is not installed on this TV", typo.stderr)+ self.assertNotIn("nothing drifted", typo.stdout)+ self.assert_untouched(typo)++ def test_fix_with_a_launcher_that_is_not_installed_changes_nothing(self):+ self.drifted_home()+ result = self.run_check("--fix", str(self.list_path), "com.gathin.tvhom")+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertIn("is not installed on this TV", result.stderr)+ self.assert_untouched(result)++ def test_a_home_activity_names_the_package_to_pass(self):+ # "Home is <package>/<activity>" is how the report prints Home, so that is what gets copied.+ # Refused before the TV is reached: an unreachable TV would say so.+ self.drifted_home(unreachable=True)+ for args in ((str(self.list_path), f"{CUSTOM}/.MainActivity"),+ ("--fix", str(self.list_path), f"{CUSTOM}/.MainActivity")):+ with self.subTest(args=args):+ result = self.run_check(*args)+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertIn(f"is a Home activity, not a package: pass {CUSTOM}", result.stderr)+ self.assertIn("usage: check.py", result.stderr)+ self.assertNotIn("could not reach the TV", result.stderr)+ self.assert_untouched(result)++ def test_a_launcher_with_shell_syntax_never_reaches_the_tv(self):+ self.drifted_home()+ result = self.run_check("--fix", str(self.list_path), f"{CUSTOM};reboot")+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertIn(f"not a package name: '{CUSTOM};reboot'", result.stderr)+ self.assertEqual(result.stdout, "")+ self.assert_untouched(result)++ def test_an_installed_stock_launcher_can_still_be_named(self):+ # The stock_home_redirect case README.md gives: the stock launcher stays Home on purpose.+ self.list_path.write_text("# My TV\ncom.example.music\ncom.example.store\n")+ self.drifted_home()+ result = self.run_check(str(self.list_path), LAUNCHERX)+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertIn(f"nothing drifted: 2 listed packages are still disabled and Home is {LAUNCHERX}/",+ result.stdout)++ def test_a_tv_that_answers_no_packages_is_not_drift(self):+ self.tv(installed=[], disabled=[])+ result = self.run_check(str(self.list_path))+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)+ self.assertIn("the TV returned no packages", result.stderr)+ self.assertEqual(result.stdout, "")+ self.assert_untouched(result)+def test_no_argument_still_exits_2_with_the_usage(self):self.tv()result = self.run_check()diff --git a/tests/test_drift_check.py b/tests/test_drift_check.pyindex 155b9d080..1369f9be6 100644--- a/tests/test_drift_check.py+++ b/tests/test_drift_check.py@@ -404,7 +404,8 @@ class DriftCheckTest(unittest.TestCase):self.write_list(["com.example.music", "com.example.store; reboot"])self.tv(disabled=[STOCK])result = self.run_check("--fix", str(self.list_path))- self.assertNotEqual(result.returncode, 0)+ # 2, the list could not be used: 1 is what a weekly job reads as drift.+ self.assertEqual(result.returncode, 2, result.stderr)self.assertIn("not a package name", result.stderr)self.assertEqual(self.sent(), [])diff --git a/tools/check.py b/tools/check.pyindex 809c294b6..b44bd08cd 100644--- a/tools/check.py+++ b/tools/check.py@@ -22,8 +22,9 @@ owns, and a fix that skipped the checks would take that TV off Wi-Fi with no wayExit 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. 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.+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"."""import osimport re@@ -288,6 +289,17 @@ def main():return 2list_path = args[0]preferred = args[1] if len(args) == 2 else None+ # The launcher package is judged here as well, not only in launcher_to_use, which runs under+ # --fix and only once Home has drifted. A second argument that is no installed package turns the+ # Home check off (expected_launcher has nothing to compare Home with), so a typo there, or the+ # package/activity form the report prints Home in, came back as "nothing drifted", exit 0.+ if preferred and not recovery.PACKAGE_RE.fullmatch(preferred):+ if "/" in preferred and recovery.PACKAGE_RE.fullmatch(home_of(preferred)):+ why = f"{preferred} is a Home activity, not a package: pass {home_of(preferred)}"+ else:+ why = f"not a package name: {preferred!r}"+ sys.stderr.write(f"check.py: {why}\n{USAGE}")+ return 2# read_list validates every name before anything is sent to the TV: its entries end up in the# TV's shell, and a list edited by hand since then is not to be trusted.try:@@ -299,6 +311,11 @@ def main():f"pass the debloat list this TV was cleaned with, e.g. "f"examples/<your tv>/debloat.txt.\n")return 2+ except SystemExit as e:+ # The same for a line that is not a package name and for a list with every line commented+ # out: read_list stops with a message, which Python turns into exit 1.+ sys.stderr.write(f"{e}. Nothing was checked or changed.\n")+ return 2core = recovery.core_packages(listed)if core:# The same refusal as disable.sh: --fix would disable these again (#884).@@ -313,7 +330,8 @@ def main():# 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, and Ctrl-C still stop the run as before.+ # unset), the refusals in recovery.py and in this file once --fix has started, and Ctrl-C still+ # stop the run as before.probe = "tools/venv/bin/python -I tools/tv.py --probe"try:dev = recovery.connect()@@ -329,11 +347,26 @@ def main():return 2fixing = Falsetry:- _, _, home, back_on, gone, home_drifted, expected = inspect(dev, listed, preferred)+ installed, _, home, back_on, gone, home_drifted, expected = inspect(dev, listed, preferred)+ if preferred and preferred not in installed:+ # The other half of the launcher check before the list was read: only the TV can say+ # whether the package is there.+ sys.stderr.write(f"{preferred} is not installed on this TV, so Home cannot be checked "+ f"against it; there is no report and nothing was changed. Check the "+ f"package name of the launcher Home should be on, and run this again.\n")+ return 2code = report(list_path, listed, home, back_on, gone, home_drifted, expected)if code and wanted:fixing = Truecode = fix(dev, listed, back_on, home_drifted, preferred)+ except SystemExit as e:+ # recovery.query_packages stops with its own message when the TV answers no package at all:+ # the TV could not be read, so exit 2, not the 1 Python gives that message. Under --fix the+ # exits keep their code, as above.+ if fixing:+ raise+ sys.stderr.write(f"{e}\n")+ return 2except Exception as e:why = f"{type(e).__name__}: {e}"if fixing: