Skip to content

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

tools: theme_restart.py answers a TV it never reached with tv.py's words and exit 2

xbsheng6575bd7e9f30
diff --git a/tests/test_theme_push_ownership.py b/tests/test_theme_push_ownership.py
index 6aef065db..326bc4a74 100644
--- a/tests/test_theme_push_ownership.py
+++ b/tests/test_theme_push_ownership.py
@@ -58,6 +58,14 @@ def connect():
return Device()
+def connect_error(error):
+ """tv.py's own: what to print when connect() raised, or None for an error it cannot word.
+
+ This stub has no network, so it never has anything to add.
+ """
+ return None
+
+
args = sys.argv[1:]
if args and args[0] == "--push":
src, dst = args[1], local(args[2])
diff --git a/tests/test_theme_restart.py b/tests/test_theme_restart.py
index c67611fab..c9ea3dc85 100644
--- a/tests/test_theme_restart.py
+++ b/tests/test_theme_restart.py
@@ -344,7 +344,7 @@ class ExitStatus(unittest.TestCase):
def test_cli_returns_failure_and_closes_transport(self):
tv = TV()
- module = types.SimpleNamespace(connect=lambda: tv)
+ module = types.SimpleNamespace(connect=lambda: tv, connect_error=lambda error: 'nothing to add')
stderr = io.StringIO()
with patch.dict(sys.modules, tv=module), patch.object(restart, 'restart', side_effect=RuntimeError('unbound')):
with contextlib.redirect_stderr(stderr):
@@ -352,6 +352,82 @@ class ExitStatus(unittest.TestCase):
self.assertTrue(tv.closed)
self.assertIn('Theme restart failed', stderr.getvalue())
+ def test_cli_answers_a_tv_it_never_reached_with_tv_py_s_words_and_2(self):
+ # "Connection refused" alone was exit 1, the status this script keeps for a restart it sent and
+ # that failed: a set that was asleep read as a theme that would not restart.
+ refused = ConnectionRefusedError(111, 'Connection refused')
+ sentence = '127.0.0.1:1 refused the connection: nothing listens for ADB there.'
+ opened = []
+
+ def connect():
+ opened.append(True)
+ raise refused
+
+ module = types.SimpleNamespace(connect=connect, connect_error=lambda error: sentence)
+ stderr, stdout = io.StringIO(), io.StringIO()
+ with patch.dict(sys.modules, tv=module):
+ with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(stdout):
+ self.assertEqual(restart.main(), 2)
+ self.assertEqual([True], opened)
+ self.assertIn(sentence, stderr.getvalue())
+ self.assertIn('Nothing was restarted', stderr.getvalue())
+ self.assertNotIn('Theme restart failed', stderr.getvalue())
+ self.assertEqual('', stdout.getvalue())
+
+ def test_cli_keeps_its_own_status_for_an_error_tv_py_cannot_word(self):
+ # An ADB key it cannot read carries the file's name, which tv.py's connect_error() has nothing to
+ # add to: this script's line and exit 1 are what a reader gets, as before.
+ def connect():
+ raise PermissionError(13, 'Permission denied', '/home/me/.android/adbkey')
+
+ module = types.SimpleNamespace(connect=connect, connect_error=lambda error: None)
+ stderr = io.StringIO()
+ with patch.dict(sys.modules, tv=module):
+ with contextlib.redirect_stderr(stderr):
+ self.assertEqual(restart.main(), 1)
+ self.assertIn('Theme restart failed', stderr.getvalue())
+ self.assertNotIn('Nothing was restarted', stderr.getvalue())
+
+ def test_cli_answers_a_checkout_without_the_client_before_asking_anything(self):
+ stderr = io.StringIO()
+ with patch.dict(sys.modules, tv=None): # `import tv` answers ImportError, as with no adb_shell
+ with contextlib.redirect_stderr(stderr):
+ self.assertEqual(restart.main(), 2)
+ self.assertIn('cannot load tools/tv.py', stderr.getvalue())
+ self.assertIn('tools/venv/bin/python', stderr.getvalue())
+ self.assertIn('Nothing was restarted', stderr.getvalue())
+
+ def test_cli_answers_tv_py_s_exit_for_a_missing_tv_host_with_2(self):
+ # tv.py exits at import time when TV_HOST is unset, which is a process exit and not an exception
+ # this script can answer with its own line: 1 there read as a restart that ran on the TV.
+ real_import = __import__
+
+ def refusing(name, *args, **kwargs):
+ if name == 'tv':
+ raise SystemExit('Set TV_HOST to your TV\'s IP address')
+ return real_import(name, *args, **kwargs)
+
+ stderr = io.StringIO()
+ with patch('builtins.__import__', refusing):
+ with contextlib.redirect_stderr(stderr):
+ self.assertEqual(restart.main(), 2)
+ self.assertIn("Set TV_HOST to your TV's IP address", stderr.getvalue(),
+ 'tv.py wrote that text for its own exit, so catching the exit must not swallow it')
+ self.assertIn('Nothing was restarted', stderr.getvalue())
+ self.assertNotIn('Theme restart failed', stderr.getvalue())
+
+ def test_the_real_client_and_a_refused_port_exit_2_with_nothing_restarted(self):
+ """The script end to end: no fake module, and a port on this computer that refuses."""
+ env = dict(os.environ, TV_HOST='127.0.0.1', TV_PORT='1')
+ env.pop('TV_ADB_BINARY', None)
+ root = Path(__file__).resolve().parents[1]
+ result = subprocess.run([sys.executable, '-I', str(root / 'tools' / 'theme_restart.py')],
+ cwd=str(root), text=True, capture_output=True, env=env, timeout=120)
+ self.assertEqual(2, result.returncode, result.stdout + result.stderr)
+ self.assertIn('Nothing was restarted', result.stderr)
+ self.assertNotIn('Theme restart failed', result.stderr)
+ self.assertEqual('', result.stdout)
+
if __name__ == '__main__':
unittest.main()
diff --git a/tools/theme.ps1 b/tools/theme.ps1
index 703e6961d..25c6414ba 100644
--- a/tools/theme.ps1
+++ b/tools/theme.ps1
@@ -5,8 +5,9 @@
# tools\theme.ps1 -TvHost 192.168.1.50 -Reset
#
# One argument, and -Reset takes none: both together are refused rather than one of them dropped (below).
-# Exit status as in theme.sh: 0 the theme is on the TV, or the reset ran; 1 the theme could not be pushed; 2 the
-# command line is not one theme folder or -Reset, so nothing was sent.
+# Exit status: 0 the theme is on the TV, or the reset ran; 1 the theme could not be pushed, or Home did not
+# restart after it (a failure of the shared helper stops this script with that status); 2 the command line is
+# not one theme folder or -Reset, so nothing was sent.
param(
[Parameter(Position = 0)][string]$Theme,
[switch]$Reset,
diff --git a/tools/theme.sh b/tools/theme.sh
index f20ef82fe..b717b7bc2 100755
--- a/tools/theme.sh
+++ b/tools/theme.sh
@@ -7,8 +7,9 @@
# TV_HOST=192.168.1.50 tools/theme.sh --reset # remove the overlay, back to config.xml
#
# One argument, and --reset takes none: a second word is refused rather than dropped (see below). Exit status:
-# 0 the theme is on the TV, or the reset ran; 1 the theme could not be pushed; 2 the command line is not one
-# theme folder or --reset, so nothing was sent.
+# 0 the theme is on the TV, or the reset ran; 1 the theme could not be pushed, or Home did not restart after
+# it; 2 the run stopped before the theme was up - the command line is not one theme folder or --reset, or the
+# TV was not reached (tv.py's and theme_restart.py's own message says which).
set -euo pipefail
# Resolve the theme folder against the caller's directory before moving into tools/.
# Git Bash's pwd says /c/Users/..., which the Windows Python cannot open; pwd -W gives C:/Users/... (as in disable.sh).
diff --git a/tools/theme_restart.py b/tools/theme_restart.py
index c9624161e..afa75c41f 100644
--- a/tools/theme_restart.py
+++ b/tools/theme_restart.py
@@ -2,6 +2,11 @@
"""Restart Home after a theme push and verify its previously enabled key helpers (#994).
Used by theme.sh and theme.ps1. No theme files or app configuration are changed here.
+
+Exit status: 0 Home is up and the key helpers it had are bound again; 1 the TV ran the restart and it failed
+(a command it refused, or a helper that did not stay bound); 2 nothing was asked of the TV - tools/venv is
+missing, TV_HOST is unset, or the TV did not answer. The three shapes in 2 are the ones tv.py words, so they
+name the port, the pair-once step and the "not an Android TV" way out, as bench.py, trace.py and manage.py do.
"""
import os
import re
@@ -156,8 +161,41 @@ def main():
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
dev = None
try:
- from tv import connect
- dev = connect()
+ # The three shapes that never ask the TV anything are answered here, before connect(): a checkout
+ # without tools/venv (no adb_shell), a shell with no TV_HOST (tv.py exits at import), and a TV that
+ # does not answer. Each used to end in "Theme restart failed: ..." with exit 1 - the status this
+ # script keeps for a restart it sent and that failed - so a set that was merely asleep read as a
+ # theme that was pushed and would not restart. bench.py (#2559), trace.py (#2683) and manage.py
+ # (#2692) answer the same shapes with 2 and say what they did not do.
+ try:
+ import tv as tv_client
+ connect, connect_error = tv_client.connect, tv_client.connect_error
+ except ImportError as error:
+ print(f"cannot load tools/tv.py ({error}): run this with tools/venv/bin/python, as "
+ "tools/theme.sh does. Nothing was restarted.", file=sys.stderr)
+ return 2
+ except SystemExit as exit_request:
+ # tv.py exits at import time with its own "Set TV_HOST to your TV's IP address" text, which only
+ # the interpreter would print for that exit: written here, so catching it does not hide it. A
+ # module that exits 0 answered the call itself (its --help), and that status stands.
+ if not exit_request.code:
+ raise
+ if isinstance(exit_request.code, str):
+ print(exit_request.code, file=sys.stderr)
+ print("Nothing was restarted: Home was not asked to start again.", file=sys.stderr)
+ return 2
+ try:
+ dev = connect()
+ except Exception as error:
+ # A refused port, a Wireless debugging port that asks for TLS, a TV behind a network that does
+ # not route: tv.py words those, and they carry the pair-once advice and the "not an Android TV"
+ # way out that "Connection refused" alone does not. An error it has nothing to add to (an ADB
+ # key it cannot read) keeps this script's own line and status.
+ why = connect_error(error)
+ if why is None:
+ raise
+ print(f"{why}\nNothing was restarted: Home was not asked to start again.", file=sys.stderr)
+ return 2
restart(dev)
except Exception as error:
print(f"Theme restart failed: {error}", file=sys.stderr)