Skip to content

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

tools: manage.py's list, status and remove refuse a word they would drop

xbshengcedd6f995d66
diff --git a/tests/test_manage_command_words.py b/tests/test_manage_command_words.py
new file mode 100644
index 000000000..37cdf7566
--- /dev/null
+++ b/tests/test_manage_command_words.py
@@ -0,0 +1,120 @@
+"""tools/manage.py's list, status, show, install and remove read their own words, as set does.
+
+Each of them looked at the first word or two of its own command line and ignored the rest. `list addons themes`
+printed the add-ons and exited 0, which reads as "both are listed"; `status --json` printed the status it prints
+without the flag; and `remove weather --dry-run` ran pm uninstall on the TV, so a set was changed by a command line
+that asked for something else. set has answered a word left over with the usage line and exit 2 since it was given
+one (tests/test_manage_set_arguments.py), and the other commands answer the same way now, before a README is read
+or the TV is asked anything.
+
+`show` and `install` keep taking several words on purpose: an add-on's name in addons/README.md can be two
+("Frigate Glance"), and install passes what is left on its line to the creator's own script - both pinned here so
+the judgement cannot start refusing the documented shape.
+"""
+import io
+import sys
+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
+
+# The three groups `manage.py list` prints, as the docstring's usage line writes them.
+GROUPS = ("addons", "themes", "switches")
+
+
+class CommandWordsTest(unittest.TestCase):
+ """A word a command does not read is the usage line and exit 2, before the TV is asked anything."""
+
+ def setUp(self):
+ # The refusals are the point: a command line that cannot run must not reach a TV. Any call fails here.
+ patcher = mock.patch.object(manage, "tv", side_effect=AssertionError("the TV was asked"))
+ self.tv = patcher.start()
+ self.addCleanup(patcher.stop)
+
+ def run_main(self, *argv):
+ """manage.main() for that command line: (status, stdout, stderr)."""
+ out, err = io.StringIO(), io.StringIO()
+ with mock.patch("sys.stdout", out), mock.patch("sys.stderr", err):
+ status = manage.main(list(argv))
+ return status, out.getvalue(), err.getvalue()
+
+ def assertRefused(self, argv, *expected):
+ status, out, err = self.run_main(*argv)
+ self.assertEqual(status, 2, err)
+ self.assertEqual(out, "", "a refused command line prints nothing on stdout")
+ for phrase in expected:
+ self.assertIn(phrase, err)
+ self.tv.assert_not_called()
+ return err
+
+ def test_a_second_word_after_list_would_be_dropped(self):
+ # The shape that printed the add-ons only, and exited 0 as if both groups were listed.
+ err = self.assertRefused(["list", "addons", "themes"], "'themes'", "would drop")
+ self.assertIn("list [addons|themes|switches]", err)
+
+ def test_list_of_a_group_that_is_not_there_names_the_three(self):
+ self.assertRefused(["list", "apps"], "'apps'", "addons, themes or switches")
+
+ def test_the_group_list_documents_still_lists(self):
+ for group in GROUPS:
+ with self.subTest(group=group):
+ status, out, err = self.run_main("list", group)
+ self.assertEqual(status, 0, err)
+ self.assertTrue(out.startswith(f"{group} ("), out)
+ self.assertEqual(err, "")
+
+ def test_no_word_lists_the_three_groups(self):
+ status, out, err = self.run_main("list")
+ self.assertEqual(status, 0, err)
+ for group in GROUPS:
+ self.assertIn(f"{group} (", out)
+
+ def test_status_takes_no_words(self):
+ self.assertRefused(["status", "--json"], "'--json'", "status takes no words")
+
+ def test_a_word_after_the_addon_is_refused_before_the_uninstall(self):
+ # The one that changed a TV: `remove weather --dry-run` ran pm uninstall with a word it never read.
+ err = self.assertRefused(["remove", "weather", "--dry-run"], "'--dry-run'", "remove takes one add-on")
+ self.assertIn("remove <add-on>", err)
+
+ def test_remove_needs_the_addon(self):
+ self.assertRefused(["remove"], "remove needs one add-on", "remove <add-on>")
+
+ def test_show_still_takes_a_name_in_two_words(self):
+ # addons/README.md writes names such as "Frigate Glance"; these are not words to drop.
+ spaced = next((a for a in manage.addons() if " " in a["name"]), None)
+ if spaced is None:
+ self.fail("an add-on whose row label is two words keeps `show` a many-word command")
+ status, out, err = self.run_main("show", *spaced["name"].split())
+ self.assertEqual(status, 0, err)
+ self.assertIn(spaced["id"], out)
+ self.tv.assert_not_called()
+
+ def test_show_needs_a_name(self):
+ self.assertRefused(["show"], "show needs an add-on, theme or switch", "show <add-on, theme or switch>")
+
+ def test_install_needs_something_to_install(self):
+ self.assertRefused(["install"], "install needs an add-on or a theme", "install <add-on or theme>")
+
+ def test_every_command_in_the_usage_block_is_judged(self):
+ # The usage block in the docstring is what a reader copies; a command added there without its words being
+ # read here is the bug this file is about, so the two lists are held to each other.
+ block = [line.strip() for line in (manage.__doc__ or "").splitlines() if "manage.py " in line]
+ commands = {line.split("manage.py ", 1)[1].split()[0] for line in block if "manage.py " in line}
+ self.assertGreaterEqual(len(commands), 6, block)
+ self.assertEqual(commands - {"set"}, set(manage.WORDS), "usage block and WORDS")
+ self.assertEqual(set(manage.WORDS), set(manage.USAGE_LINES))
+ self.assertEqual(set(manage.LIST_GROUPS), set(GROUPS))
+
+ def test_a_word_before_the_command_still_prints_the_usage(self):
+ status, out, err = self.run_main("publish", "addons")
+ self.assertEqual(status, 2)
+ self.assertIn("manage.py list", out) # the docstring, as `--help` prints it
+ self.tv.assert_not_called()
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/manage.py b/tools/manage.py
index e45d86746..354a6c9bc 100644
--- a/tools/manage.py
+++ b/tools/manage.py
@@ -19,7 +19,13 @@ table says so, the text itself for a string - and refuses one the launcher would
range the table states. A theme's config.json is hand-edited, so one that cannot be read, is not JSON, or is not a
JSON object is named before the key is written, and a write it cannot make says so instead of ending in a traceback.
set's own arguments are read first: a switch without a value, a --theme without the folder behind it, and a word
-left over are answered with the usage line and exit 2 before a theme folder is read or written.
+left over are answered with the usage line and exit 2 before a theme folder is read or written. list, status,
+show, install and remove read their own words the same way, before anything is read from this checkout or asked
+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 line
+asked 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.
"""
import json
import math
@@ -345,9 +351,10 @@ def show_line(item, installed=None):
def cmd_list(what):
groups = {"addons": addons, "themes": themes, "switches": switches}
- for g in ([what] if what else ["addons", "themes", "switches"]):
+ for g in ([what] if what else list(LIST_GROUPS)):
if g not in groups:
- sys.exit("list addons, themes or switches")
+ sys.stderr.write(f"list takes addons, themes or switches, not {g!r}: {USAGE_LINES['list']}\n")
+ return 2
items = groups[g]()
print(f"{g} ({len(items)})")
for item in items:
@@ -445,6 +452,51 @@ class Usage(Exception):
"""A command line that cannot run: the message goes to stderr and the status is 2."""
+# What each command's own line may carry, and the usage line it answers with. `list` prints one of the three
+# groups: its usage line has one word because `list addons themes` printed the add-ons and exited 0, which reads as
+# "both are listed". `show` and `install` take any number of words on purpose - an add-on's name in
+# addons/README.md can be several ("Frigate Glance"), and install passes what is left to the creator's script.
+# `remove` is the reason this is worth judging: `remove weather --dry-run` ran pm uninstall on the TV with a word
+# the command never read, so the set was changed by a command line that asked for something else.
+WORDS = {
+ "list": (0, 1, "addons, themes or switches"),
+ "show": (1, None, "an add-on, theme or switch"),
+ "status": (0, 0, "no words"),
+ "install": (1, None, "an add-on or a theme"),
+ "remove": (1, 1, "one add-on"),
+}
+
+USAGE_LINES = {
+ "list": "list [addons|themes|switches]",
+ "show": "show <add-on, theme or switch>",
+ "status": "status",
+ "install": "install <add-on or theme> [script arguments]",
+ "remove": "remove <add-on>",
+}
+
+LIST_GROUPS = ("addons", "themes", "switches")
+
+
+def own_words(command, args):
+ """The words `command` reads, or Usage naming what is missing or what it would drop there.
+
+ Every command here used to read the first word or two of its own line and ignore the rest, while set has
+ answered a leftover word with the usage line and exit 2 since it was given one: `list addons themes` listed
+ the add-ons and exited 0, `status --json` printed the status it prints without the flag, and `remove
+ weather --dry-run` uninstalled the add-on. A word the command does not read is a command line that asked for
+ something this script does not do, so it is answered before a README is read or the TV is asked anything.
+ """
+ least, most, what = WORDS[command]
+ if len(args) < least:
+ raise Usage(f"{command} needs {what}: {USAGE_LINES[command]}")
+ if most is not None and len(args) > most:
+ dropped = " ".join(args[most:])
+ raise Usage(f"{command} takes {what}, and would drop {dropped!r}: {USAGE_LINES[command]}")
+ if command == "list" and args and args[0] not in LIST_GROUPS:
+ raise Usage(f"list takes addons, themes or switches, not {args[0]!r}: {USAGE_LINES[command]}")
+ return args
+
+
def set_args(args):
"""(key, value, theme) from set's own arguments, or Usage naming what is missing.
@@ -486,16 +538,6 @@ def main(argv):
print(__doc__)
return 0
c, args = argv[0], argv[1:]
- if c == "list":
- return cmd_list(args[0] if args else None)
- if c == "show" and args:
- return cmd_show(" ".join(args))
- if c == "status":
- return cmd_status()
- if c == "install" and args:
- return cmd_install(args[0], args[1:])
- if c == "remove" and args:
- return cmd_remove(args[0])
if c == "set":
try:
key, value, theme = set_args(args)
@@ -503,8 +545,23 @@ def main(argv):
sys.stderr.write(f"{usage}\n")
return 2
return cmd_set(key, value, theme)
- print(__doc__)
- return 2
+ if c not in WORDS:
+ print(__doc__)
+ return 2
+ try:
+ args = own_words(c, args)
+ except Usage as usage:
+ sys.stderr.write(f"{usage}\n")
+ return 2
+ if c == "list":
+ return cmd_list(args[0] if args else None)
+ if c == "show":
+ return cmd_show(" ".join(args)) # a name can be several words: "Frigate Glance"
+ if c == "status":
+ return cmd_status()
+ if c == "install":
+ return cmd_install(args[0], args[1:])
+ return cmd_remove(args[0])
if __name__ == "__main__":