Skip to content

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

tools: theme.sh answers a run that never reached the theme with 2, as its header says

xbshengdf97c756662b
diff --git a/tests/test_theme_exit_status.py b/tests/test_theme_exit_status.py
new file mode 100644
index 000000000..419161d7f
--- /dev/null
+++ b/tests/test_theme_exit_status.py
@@ -0,0 +1,191 @@
+"""theme.sh answers the two shapes its header gives exit 2 for with 2, not with 1.
+
+The header says: "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, ... the TV was not reached". Two shapes that never reached the TV answered 1 instead, the status a
+reader (and an assistant following PROMPT.md) reads as "the theme was tried and the TV refused it":
+
+ * TV_HOST unset or empty: bash's own `${TV_HOST:?}` expansion wrote "tools/theme.sh: line 44: TV_HOST: set
+ TV_HOST ..." and exited 1 - bash answers a failed `:?` with 1 - so a run that sent nothing looked like a
+ push that had run. An empty TV_HOST (TV_HOST= ...) took the same path.
+ * a word starting with a dash: `--help`, or a `--resett` one letter too many, was resolved against the
+ caller's directory like a folder name and the run stopped at "no config.json in <cwd>/--help", exit 1.
+ Nothing was sent either, and the message named a file nobody wrote instead of the command line.
+ * the checkout has no tools/venv: the reader was given the venv line, then exit 1 for a run that never left
+ this computer - while theme_restart.py, the helper the push ends with, answers that with 2 (#2763).
+
+All three are judged before anything is asked of the TV now, in this script's own words, on exit 2. The usage
+line in the header is unchanged: a theme folder or --reset, one argument.
+
+These tests run the real tools/theme.sh against a stub tools/tv.py that appends every call to a log and exits 0,
+so "nothing was sent" means the log is missing and "the theme is still pushed" means the log holds the
+config.json push. A theme folder with a config.json is written in the test.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+
+TOOLS = Path(__file__).resolve().parents[1] / "tools"
+USAGE = "usage: theme.sh <theme folder> | --reset"
+NOTHING_SENT = "Nothing was sent to the TV."
+
+# Every call theme.sh (and theme_restart.py) makes reaches this file, so a test can tell whether anything was
+# asked of the TV. The real client is never imported: the tools are copied into a folder of the test's own.
+FAKE_TV = '''\
+import os
+import sys
+with open(os.environ["FAKE_LOG"], "a") as log:
+ log.write(" ".join(sys.argv[1:]) + "\\n")
+sys.exit(0)
+'''
+
+
+class ThemeExitStatusTest(unittest.TestCase):
+ def setUp(self):
+ self.base = Path(tempfile.mkdtemp())
+ self.addCleanup(shutil.rmtree, self.base, ignore_errors=True)
+ self.tools = self.base / "tools"
+ self.tools.mkdir()
+ for name in ("theme.sh", "theme_restart.py"):
+ shutil.copy2(TOOLS / name, self.tools / name)
+ (self.tools / "tv.py").write_text(FAKE_TV)
+ venv = self.tools / "venv" / "bin"
+ venv.mkdir(parents=True)
+ (venv / "python").symlink_to(sys.executable)
+ self.theme = self.base / "my-theme"
+ self.theme.mkdir()
+ (self.theme / "config.json").write_text('{"background_color": "#000000"}\n', encoding="utf-8")
+ self.log = self.base / "tv-calls.log"
+
+ def run_theme(self, *args, **env_overrides):
+ if os.name != "posix" or not shutil.which("bash"):
+ raise unittest.SkipTest("theme.sh is a POSIX script; it needs bash")
+ env = {k: v for k, v in os.environ.items() if k != "TV_HOST"}
+ env.update({"TV_HOST": "192.0.2.10", "FAKE_LOG": str(self.log)})
+ env.update(env_overrides)
+ for key, value in env_overrides.items():
+ if value is None:
+ env.pop(key, None)
+ return subprocess.run(["bash", str(self.tools / "theme.sh"), *args], env=env,
+ capture_output=True, text=True, timeout=120)
+
+ def tv_calls(self):
+ if not self.log.exists():
+ return []
+ return [line for line in self.log.read_text().splitlines() if line.strip()]
+
+ def assert_stopped(self, result, word=None):
+ self.assertEqual(2, result.returncode, result.stdout + result.stderr)
+ self.assertEqual("", result.stdout, "a run that stopped before the theme was up writes nothing on stdout")
+ self.assertIn(NOTHING_SENT, result.stderr, result.stderr)
+ self.assertNotIn("Traceback", result.stderr, result.stderr)
+ if word is not None:
+ self.assertIn(word, result.stderr, "the message has to name what it could not read")
+ self.assertEqual([], self.tv_calls(), "a run that sent nothing asked the TV for something")
+
+ def test_no_tv_host_stops_with_2(self):
+ # The defect: this answered 1, the code for "the theme could not be pushed", and the message was bash's
+ # own expansion error naming a line number.
+ result = self.run_theme(str(self.theme), TV_HOST=None)
+ self.assert_stopped(result, "set TV_HOST to the TV IP address")
+ self.assertNotIn("theme.sh: line", result.stderr,
+ "bash's own expansion error is not this script's message")
+ self.assertNotIn("TV_HOST: set TV_HOST", result.stderr)
+
+ def test_an_empty_tv_host_is_the_same_mistake(self):
+ # `TV_HOST= tools/theme.sh themes/aurora`, or a variable that came out empty: `${TV_HOST:?}` treats an
+ # empty value as unset too, so this reached the same exit 1.
+ result = self.run_theme(str(self.theme), TV_HOST="")
+ self.assert_stopped(result, "set TV_HOST to the TV IP address")
+
+ def test_reset_without_a_host_sends_nothing(self):
+ result = self.run_theme("--reset", TV_HOST=None)
+ self.assert_stopped(result, "set TV_HOST to the TV IP address")
+
+ def test_help_is_a_command_line_the_script_cannot_read(self):
+ # The defect: resolved as a folder beside the caller, so the reader was told their theme had no
+ # config.json at a path they never typed.
+ result = self.run_theme("--help")
+ self.assert_stopped(result, "--help")
+ self.assertIn(USAGE, result.stderr, result.stderr)
+ self.assertNotIn("no config.json", result.stderr,
+ "the theme folder was never read: the command line was")
+
+ def test_a_misspelt_reset_is_not_read_as_a_folder(self):
+ result = self.run_theme("--resett")
+ self.assert_stopped(result, "--resett")
+ self.assertIn(USAGE, result.stderr, result.stderr)
+
+ def test_a_checkout_without_the_venv_stops_with_2(self):
+ # theme_restart.py already answers its missing venv with 2 (#2763); theme.sh answered 1 - the code for
+ # "the theme could not be pushed" - for a run that never left this computer.
+ shutil.rmtree(self.tools / "venv")
+ result = self.run_theme(str(self.theme))
+ self.assert_stopped(result, "python3 -m venv tools/venv")
+
+ def test_a_theme_folder_still_pushes_with_a_host(self):
+ # The other direction: the refusal above must not catch a folder, and the harness has to reach the TV.
+ result = self.run_theme(str(self.theme))
+ self.assertEqual(0, result.returncode, result.stdout + result.stderr)
+ self.assertIn("is on the TV", result.stdout)
+ pushes = [line for line in self.tv_calls() if line.startswith("--push")]
+ self.assertTrue(pushes, "the theme's config.json is pushed")
+ self.assertTrue(any(str(self.theme / "config.json") in line for line in pushes), pushes)
+
+
+class ThemeExitStatusSourceTest(unittest.TestCase):
+ """The judgement sits in the script's own words and before anything is sent, and the twin says the same."""
+
+ def setUp(self):
+ self.shell = (TOOLS / "theme.sh").read_text(encoding="utf-8")
+ self.ps = (TOOLS / "theme.ps1").read_text(encoding="utf-8")
+
+ def test_the_address_is_judged_by_the_script_not_by_bash(self):
+ # The comment above the judgement quotes the old expansion; what must not come back is the command.
+ self.assertNotIn(': "${TV_HOST:?', self.shell,
+ "bash's `:?` answers a missing address with its own line and exit 1")
+ judged = self.shell.index('if [ -z "${TV_HOST:-}" ]')
+ self.assertLess(judged, self.shell.index('PY=venv/bin/python'),
+ "the address is read before the client is chosen")
+ self.assertLess(judged, self.shell.index('tv "mkdir -p $DIR'),
+ "nothing can be sent before the address is read")
+ block = self.shell[judged:]
+ self.assertIn("exit 2", block[:block.index("fi")])
+
+ def test_a_dash_word_is_refused_before_it_is_resolved_as_a_path(self):
+ judged = self.shell.index('-*) if [ "$THEME_ARG" != "--reset" ]')
+ self.assertLess(judged, self.shell.index('*) THEME_ARG="$(pwd -W'),
+ "the dash word is refused before the folder resolution reads it")
+ self.assertLess(judged, self.shell.index('if [ "${1:-}" = --reset ]'),
+ "the reset branch is not what answers it")
+
+ def test_the_header_states_what_2_covers(self):
+ header = self.shell[:self.shell.index("set -euo pipefail")]
+ self.assertIn("TV_HOST is unset or empty", header)
+ self.assertIn("starts with a dash", header)
+ self.assertIn("Nothing was sent to the TV.", header + self.shell)
+
+ def test_the_windows_twin_answers_the_missing_host_with_2(self):
+ judged = self.ps.index("if (-not $TvHost)")
+ block = self.ps[judged:self.ps.index("$env:TV_HOST = $TvHost")]
+ self.assertIn("exit 2", block)
+ self.assertIn(NOTHING_SENT, block)
+ self.assertNotIn("throw", block, "a throw exits 1, the status of a push that failed")
+ self.assertIn("no TV host was given", self.ps[:judged])
+
+ def test_the_windows_twin_answers_a_missing_venv_with_2(self):
+ judged = self.ps.index("if (-not (Test-Path $py))")
+ block = self.ps[judged:self.ps.index("$pkg = 'com.gathin.tvhome'")]
+ self.assertIn("exit 2", block)
+ self.assertIn(NOTHING_SENT, block)
+ self.assertNotIn("throw", block)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/theme.ps1 b/tools/theme.ps1
index 25c6414ba..7379a5878 100644
--- a/tools/theme.ps1
+++ b/tools/theme.ps1
@@ -7,7 +7,7 @@
# One argument, and -Reset takes none: both together are refused rather than one of them dropped (below).
# 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.
+# not one theme folder or -Reset, no TV host was given, or tools\venv is missing, so nothing was sent.
param(
[Parameter(Position = 0)][string]$Theme,
[switch]$Reset,
@@ -23,13 +23,23 @@ if ($Theme -and $Reset) {
}
$ErrorActionPreference = 'Stop'
$OutputEncoding = New-Object System.Text.UTF8Encoding $false
-if (-not $TvHost) { throw 'pass -TvHost <tv ip> or set $env:TV_HOST' }
+if (-not $TvHost) {
+ # The same status theme.sh answers, for the same reason: a missing address is a run that never started, not
+ # a theme that could not be pushed, and 1 is what the header keeps for the push that ran and failed.
+ [Console]::Error.WriteLine('pass -TvHost <tv ip> or set $env:TV_HOST. Nothing was sent to the TV.')
+ exit 2
+}
$env:TV_HOST = $TvHost
# Resolve the theme folder against the caller's directory before moving into tools\.
if ($Theme) { $Theme = (Resolve-Path $Theme).Path }
Set-Location $PSScriptRoot
$py = 'venv\Scripts\python.exe'
-if (-not (Test-Path $py)) { throw 'run: python -m venv tools\venv; tools\venv\Scripts\python.exe -m pip install -r tools\requirements.txt' }
+if (-not (Test-Path $py)) {
+ # As in theme.sh: the venv is on this computer, so nothing was sent and this is a run that stopped before the
+ # theme was up, not a push that ran and failed.
+ [Console]::Error.WriteLine('run: python -m venv tools\venv; tools\venv\Scripts\python.exe -m pip install -r tools\requirements.txt. Nothing was sent to the TV.')
+ exit 2
+}
$pkg = 'com.gathin.tvhome'
# Runs tools\tv.py or b64push.py and stops on a non-zero exit: 'Stop' only covers cmdlets, so a failed
# native call would otherwise fall through to "Theme ... is on the TV".
diff --git a/tools/theme.sh b/tools/theme.sh
index b717b7bc2..c90cfbd8f 100755
--- a/tools/theme.sh
+++ b/tools/theme.sh
@@ -8,20 +8,11 @@
#
# 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, 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).
+# it; 2 the run stopped before the theme was up - the command line is not one theme folder or --reset (a word
+# that starts with a dash is not a folder name, -h and --help included), TV_HOST is unset or empty, the checkout
+# has no tools/venv, 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).
-# An absolute argument needs the same: /c/Users/... passed through as typed reached the JSON check below, which
-# could not open it and called the file "not valid JSON". A folder that does not exist keeps the path as typed,
-# so the "no config.json" message names what the caller gave.
THEME_ARG="${1:-}"
-case "$THEME_ARG" in
- --reset|"") ;;
- /*|[A-Za-z]:[\\/]*) THEME_ARG="$(cd "$THEME_ARG" 2>/dev/null && { pwd -W 2>/dev/null || pwd; } || printf '%s' "$THEME_ARG")" ;;
- *) THEME_ARG="$(pwd -W 2>/dev/null || pwd)/$THEME_ARG" ;;
-esac
# A theme folder or --reset, and nothing else. The first word alone decided everything the script did, so
# `theme.sh --reset themes/aurora` matched the reset branch on that word and threw the folder away: the theme
# the reader named was removed from the TV instead of pushed, and the run answered "reset" with exit 0 (the
@@ -40,11 +31,47 @@ if [ "$#" -gt 1 ]; then
"Nothing was sent to the TV." >&2
exit 2
fi
+# A word that starts with a dash is an option to the reader who types it, and this script has one: --reset.
+# Left to the path below, `--help` (or `--resett`, one letter too many) was resolved against the caller's
+# directory and the run named a config.json nobody wrote - "no config.json in <cwd>/--help", exit 1, the status
+# the header keeps for a theme that could not be pushed, so a run that sent nothing read as one that had. No
+# folder a theme lives in starts with a dash, and this script sends nothing for a command line it cannot read:
+# answer 2 with the usage, before TV_HOST is required and before anything is sent.
+case "$THEME_ARG" in
+ -*) if [ "$THEME_ARG" != "--reset" ]; then
+ echo "theme.sh takes a theme folder or --reset, so $THEME_ARG is not a command line it can read." \
+ "Nothing was sent to the TV." >&2
+ echo "usage: theme.sh <theme folder> | --reset" >&2
+ exit 2
+ fi ;;
+esac
+# 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).
+# An absolute argument needs the same: /c/Users/... passed through as typed reached the JSON check below, which
+# could not open it and called the file "not valid JSON". A folder that does not exist keeps the path as typed,
+# so the "no config.json" message names what the caller gave.
+case "$THEME_ARG" in
+ --reset|"") ;;
+ /*|[A-Za-z]:[\\/]*) THEME_ARG="$(cd "$THEME_ARG" 2>/dev/null && { pwd -W 2>/dev/null || pwd; } || printf '%s' "$THEME_ARG")" ;;
+ *) THEME_ARG="$(pwd -W 2>/dev/null || pwd)/$THEME_ARG" ;;
+esac
cd "$(dirname "$0")"
-: "${TV_HOST:?set TV_HOST to the TV IP address, e.g. TV_HOST=192.168.1.50 $0 themes/aurora}"
+# The address is the reader's to give, and a missing one is not a theme that could not be pushed: bash's own
+# `${TV_HOST:?}` answered it with "tools/theme.sh: line N: TV_HOST: ..." and exit 1 - the status the header
+# keeps for a push that ran and failed - so a run that never started read as one that had. Judged here, before
+# the client is chosen, in this script's own words and with the status the header gives a run that stopped
+# before the theme was up. An empty TV_HOST is the same mistake as an unset one.
+if [ -z "${TV_HOST:-}" ]; then
+ echo "set TV_HOST to the TV IP address, e.g. TV_HOST=192.168.1.50 $0 themes/aurora." \
+ "Nothing was sent to the TV." >&2
+ exit 2
+fi
PY=venv/bin/python
[ -x "$PY" ] || PY=venv/Scripts/python.exe
-[ -x "$PY" ] || { echo "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt" >&2; exit 1; }
+# The venv is on this computer, so a checkout without one is another run that stopped before the theme was up:
+# it answered 1, the status for a push that ran and failed, while theme_restart.py's own missing-venv answer is 2.
+[ -x "$PY" ] || { echo "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt." \
+ "Nothing was sent to the TV." >&2; exit 2; }
export MSYS_NO_PATHCONV=1
PKG=com.gathin.tvhome
DIR=/sdcard/Android/data/$PKG/files