Skip to content

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

tools: theme_restart.py --help prints this script's text, and a word it does not take is refused

xbsheng0797194c71e9
diff --git a/tests/test_theme_restart.py b/tests/test_theme_restart.py
index c9ea3dc85..5a0d4e40c 100644
--- a/tests/test_theme_restart.py
+++ b/tests/test_theme_restart.py
@@ -348,7 +348,7 @@ class ExitStatus(unittest.TestCase):
stderr = io.StringIO()
with patch.dict(sys.modules, tv=module), patch.object(restart, 'restart', side_effect=RuntimeError('unbound')):
with contextlib.redirect_stderr(stderr):
- self.assertEqual(restart.main(), 1)
+ self.assertEqual(restart.main([]), 1)
self.assertTrue(tv.closed)
self.assertIn('Theme restart failed', stderr.getvalue())
@@ -367,7 +367,7 @@ class ExitStatus(unittest.TestCase):
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(restart.main([]), 2)
self.assertEqual([True], opened)
self.assertIn(sentence, stderr.getvalue())
self.assertIn('Nothing was restarted', stderr.getvalue())
@@ -384,7 +384,7 @@ class ExitStatus(unittest.TestCase):
stderr = io.StringIO()
with patch.dict(sys.modules, tv=module):
with contextlib.redirect_stderr(stderr):
- self.assertEqual(restart.main(), 1)
+ self.assertEqual(restart.main([]), 1)
self.assertIn('Theme restart failed', stderr.getvalue())
self.assertNotIn('Nothing was restarted', stderr.getvalue())
@@ -392,7 +392,7 @@ class ExitStatus(unittest.TestCase):
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.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())
@@ -410,12 +410,78 @@ class ExitStatus(unittest.TestCase):
stderr = io.StringIO()
with patch('builtins.__import__', refusing):
with contextlib.redirect_stderr(stderr):
- self.assertEqual(restart.main(), 2)
+ 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_help_is_this_scripts_text_and_needs_no_client(self):
+ # tv.py answers --help itself at import time with the ADB client's usage, so this script's own
+ # --help printed --probe, --batch, --push and --screenshot, none of which it takes. Its text is
+ # answered from the docstring, before the client is imported, so a reader needs no venv and no TV.
+ def no_imports(name, *args, **kwargs):
+ raise AssertionError(f'--help imported {name}')
+
+ for word in ('--help', '-h'):
+ with self.subTest(word=word):
+ stdout, stderr = io.StringIO(), io.StringIO()
+ with patch('builtins.__import__', no_imports), \
+ contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
+ self.assertEqual(0, restart.main([word]))
+ text = stdout.getvalue()
+ self.assertIn('Restart Home after a theme push', text)
+ self.assertIn('takes no arguments', text)
+ self.assertIn('Exit status', text)
+ for flag in ('--probe', '--batch', '--push', '--pull', '--screenshot', '--reboot'):
+ self.assertNotIn(flag, text, f'{flag} is tv.py\'s flag, not this script\'s')
+ self.assertEqual('', stderr.getvalue())
+
+ def test_a_word_it_does_not_take_is_refused_before_the_tv_is_asked(self):
+ # It takes no arguments, and a word was dropped without a word: `--reset` (theme.sh's word) ran a
+ # plain restart, so a reader who meant something else still had Home restarted.
+ opened = []
+ module = types.SimpleNamespace(connect=lambda: opened.append(True),
+ connect_error=lambda error: 'nothing to add')
+ stderr, stdout = io.StringIO(), io.StringIO()
+ with patch.dict(sys.modules, tv=module), \
+ contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(stdout):
+ self.assertEqual(2, restart.main(['--reset']))
+ self.assertEqual([], opened, 'the TV must not be asked anything for a word that is refused')
+ self.assertIn('takes no arguments', stderr.getvalue())
+ self.assertIn('--reset', stderr.getvalue())
+ self.assertIn('Nothing was restarted', stderr.getvalue())
+ self.assertNotIn('Theme restart failed', stderr.getvalue())
+ self.assertEqual('', stdout.getvalue())
+
+ def test_several_words_are_all_named_in_the_refusal(self):
+ stderr = io.StringIO()
+ with contextlib.redirect_stderr(stderr):
+ self.assertEqual(2, restart.main(['--reset', 'now']))
+ self.assertIn('--reset, now', stderr.getvalue())
+
+ def test_the_script_end_to_end_answers_help_and_a_word_it_does_not_take(self):
+ """No fake module: the real script, in a process with no TV_HOST, as theme.sh would run it."""
+ root = Path(__file__).resolve().parents[1]
+ env = dict(os.environ, TV_HOST='127.0.0.1', TV_PORT='1')
+ env.pop('TV_ADB_BINARY', None)
+ script = str(root / 'tools' / 'theme_restart.py')
+
+ helped = subprocess.run([sys.executable, '-I', script, '--help'], cwd=str(root), text=True,
+ capture_output=True, timeout=120, env=env)
+ self.assertEqual(0, helped.returncode, helped.stdout + helped.stderr)
+ self.assertIn('Restart Home after a theme push', helped.stdout)
+ self.assertNotIn('--probe', helped.stdout)
+ self.assertEqual('', helped.stderr)
+
+ refused = subprocess.run([sys.executable, '-I', script, '--reset'], cwd=str(root), text=True,
+ capture_output=True, timeout=120, env=env)
+ self.assertEqual(2, refused.returncode, refused.stdout + refused.stderr)
+ self.assertIn('takes no arguments', refused.stderr)
+ self.assertIn('Nothing was restarted', refused.stderr)
+ self.assertNotIn('Theme restart failed', refused.stderr)
+ self.assertEqual('', refused.stdout)
+
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')
diff --git a/tools/theme_restart.py b/tools/theme_restart.py
index afa75c41f..a2da05cb0 100644
--- a/tools/theme_restart.py
+++ b/tools/theme_restart.py
@@ -1,12 +1,14 @@
#!/usr/bin/env python3
"""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.
+Used by theme.sh and theme.ps1, and it takes no arguments: --help prints this text (no tools/venv, no
+TV_HOST and no TV needed for it), and a word it does not take is refused before the TV is asked anything.
+
+Exit status: 0 Home is up and the key helpers it had are bound again, or --help printed this text; 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 - a word it does not take, tools/venv is missing, TV_HOST is unset, or the TV did not
+answer. The three shapes at the end 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,7 +158,24 @@ def restart(dev, pause=time.sleep):
raise
-def main():
+def main(argv=None):
+ """The script's whole command line, then the restart. `argv` is sys.argv[1:] unless a caller passes
+ its own words (the tests do), so an in-process caller's arguments are never read here."""
+ words = list(sys.argv[1:] if argv is None else argv)
+ # tv.py is the module this script loads, and it answers --help at import time with its own usage: so
+ # `theme_restart.py --help` printed the ADB client's flags (--probe, --batch, --push, --screenshot,
+ # --reboot), none of which this script takes, and said nothing about what it does. Answered here from
+ # this file's own docstring, before `import tv`: no venv, no TV_HOST and no TV are needed for it.
+ if words[:1] in (["-h"], ["--help"]):
+ print(__doc__.strip())
+ return 0
+ # It takes no arguments, so a word that is not one used to be dropped without a word: `--reset` (the
+ # neighbouring theme.sh word) reached the TV as a plain restart. theme.sh refuses a second word the same
+ # way and for the same reason.
+ if words:
+ print(f"theme_restart.py takes no arguments: {', '.join(words)} would not have been used. "
+ "Nothing was restarted: Home was not asked to start again.", file=sys.stderr)
+ return 2
# Both callers use Python -I, which excludes the script directory from sys.path.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
dev = None
@@ -177,7 +196,7 @@ def main():
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.
+ # module that exits 0 answered the call itself, and that status stands.
if not exit_request.code:
raise
if isinstance(exit_request.code, str):