Skip to content

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

tools: b64push.py names the venv instead of a ModuleNotFoundError traceback

xbshengd7e80409a468
diff --git a/tests/test_b64push_client.py b/tests/test_b64push_client.py
new file mode 100644
index 000000000..5a998f19a
--- /dev/null
+++ b/tests/test_b64push_client.py
@@ -0,0 +1,109 @@
+"""tools/b64push.py: a client that is not installed is refused, not a traceback.
+
+The script is started both ways, like the other tools: every add-on install script and
+`tools/theme.sh` run it as `tools/venv/bin/python -I tools/b64push.py`, and a reader who has just
+cloned the repository types `python3 tools/b64push.py app.apk /data/local/tmp/app.apk`. That second
+run ended in a ModuleNotFoundError out of `import tv` - a traceback with exit 1, the code this file
+keeps for a transfer that failed, although nothing had been sent and no TV had been opened - and it
+named neither tools/requirements.txt nor the venv sitting next to it.
+
+b64push.py is the fourth place the client is imported. tools/privacy.py's and tools/recovery.py's
+connect() stop with the line that builds the venv, tools/bench.py and tools/trace.py start again
+under it, and tools/tv.py does the same when it is the script that was started; this file now answers
+with that line and exits 2, which is what its own exit status reserves for a run that sent nothing.
+
+The first run below copies the real tools/tv.py beside a copy of the script and starts it with -S, so
+the interpreter cannot import adb_shell whatever it is installed into: that is the shape a fresh clone
+gives. The second puts a stand-in tv.py there instead, and shows the handler is entered only when the
+import fails: a client that loads is used as it was.
+
+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
+
+ROOT = Path(__file__).resolve().parents[1]
+
+# A tools/tv.py that records being opened, so a run that gets past the import leaves a file behind.
+FAKE_TV = r'''import os
+
+open(os.environ["FAKE_CONNECTED"], "w").write("connected")
+
+
+class Device:
+ max_chunk_size = 4096
+
+ def shell(self, command, read_timeout_s=None):
+ return ""
+
+ def close(self):
+ pass
+
+
+def connect():
+ return Device()
+'''
+
+
+class MissingClientTest(unittest.TestCase):
+ def setUp(self):
+ self.base = Path(tempfile.mkdtemp())
+ self.addCleanup(shutil.rmtree, self.base, ignore_errors=True)
+ tools = self.base / "tools"
+ tools.mkdir()
+ shutil.copy2(ROOT / "tools" / "b64push.py", tools / "b64push.py")
+ shutil.copy2(ROOT / "tools" / "tv.py", tools / "tv.py")
+ self.script = tools / "b64push.py"
+ self.connected = self.base / "connected"
+ self.local = self.base / "app.apk"
+ self.local.write_bytes(b"an apk")
+
+ def run_b64push(self, *args, isolated=False):
+ """The script, with no TV_HOST in the environment and adb_shell unavailable under -S."""
+ env = {k: v for k, v in os.environ.items() if k not in ("TV_HOST", "TV_PORT")}
+ env["FAKE_CONNECTED"] = str(self.connected)
+ command = [sys.executable] + (["-S"] if isolated else []) + [str(self.script), *args]
+ return subprocess.run(command, env=env, capture_output=True, text=True, timeout=60)
+
+ def assert_client_refused(self, result):
+ self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
+ self.assertIn("cannot load tools/tv.py", result.stderr)
+ self.assertIn("tools/venv", result.stderr, "the message names the venv to install the packages into")
+ self.assertIn("tools/requirements.txt", result.stderr, "and the file the packages are listed in")
+ self.assertNotIn("Traceback", result.stderr)
+ self.assertEqual(result.stdout, "", "nothing is reported as transferred")
+ self.assertFalse(self.connected.exists(), "the TV is not opened")
+
+ def test_a_run_without_the_packages_is_refused(self):
+ self.assert_client_refused(self.run_b64push(str(self.local), "/data/local/tmp/app.apk",
+ isolated=True))
+
+ def test_the_refusal_comes_before_the_tv_host_check(self):
+ # tv.py reads TV_HOST at import time, so a run with no TV_HOST and no packages has to answer
+ # about the packages: that is the thing the reader can fix.
+ result = self.run_b64push(str(self.local), "/data/local/tmp/app.apk", isolated=True)
+ self.assertNotIn("Set TV_HOST", result.stderr)
+ self.assertIn("adb_shell", result.stderr, "the missing package is named")
+
+ def test_a_word_count_that_is_wrong_still_gets_the_usage_line(self):
+ # The usage message is written before the client is imported, so this refusal is unchanged.
+ result = self.run_b64push(str(self.local), isolated=True)
+ self.assertEqual(result.returncode, 2, result.stderr)
+ self.assertIn("usage: b64push.py", result.stderr)
+ self.assertNotIn("cannot load", result.stderr)
+
+ def test_a_client_that_loads_is_used_as_before(self):
+ (self.base / "tools" / "tv.py").write_text(FAKE_TV, encoding="utf-8")
+ result = self.run_b64push(str(self.local), "/data/local/tmp/app.apk")
+ self.assertNotIn("cannot load tools/tv.py", result.stderr,
+ "the handler is for a failed import, not for every run")
+ self.assertTrue(self.connected.exists(), "the client beside the copy was imported and used")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/b64push.py b/tools/b64push.py
index 65b6f2985..98cb1b1ba 100644
--- a/tools/b64push.py
+++ b/tools/b64push.py
@@ -10,7 +10,8 @@ verified file is copied over the target and checked again. That copy is not atom
Exit status: 0 the copy is on the TV, 1 a transfer failed (the target is as it was, unless that
copy failed or was interrupted, which the message says), 2 nothing was sent: the arguments were
-refused, or the connection failed in one of the ways tv.py explains (a refused port, a Wireless
+refused, the local file could not be read, the packages in tools/requirements.txt are not installed,
+or the connection failed in one of the ways tv.py explains (a refused port, a Wireless
debugging port, a handshake that timed out, a TV that cannot be reached). Why it stopped goes to
stderr, which the scripts that throw the progress on stdout away still show.
"""
@@ -63,7 +64,22 @@ def main():
# Imported only now: tv.py exits at import time when TV_HOST is unset, which would hide the
# usage message above.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
- import tv # noqa: E402
+ # The client needs adb_shell, which the README installs into tools/venv only. This is the fourth
+ # place that client is imported and the only one that answered a plain `python3 tools/b64push.py`
+ # with a ModuleNotFoundError traceback and exit 1 - the code this file keeps for a transfer that
+ # failed - for a run that never reached the TV. privacy.py's and recovery.py's connect() stop with
+ # the line that builds the venv, and bench.py and trace.py start again under it, so a reader who
+ # has just cloned the repository gets the same answer here. The handler is entered only when the
+ # import fails, so a working client - the real one, or the stand-in the tests put beside a copy of
+ # this file - is used as before.
+ try:
+ import tv # noqa: E402
+ except ImportError as error:
+ sys.stderr.write(f"cannot load tools/tv.py ({error}).\n"
+ "run: python3 -m venv tools/venv && "
+ "tools/venv/bin/pip install -r tools/requirements.txt\n"
+ "Nothing was sent to the TV.\n")
+ return 2
# A refused port or a TV that cannot be reached gets tv.py's own explanation, not a traceback, and
# exits 2 as tv.py does: nothing was sent. An error tv.py has nothing to add to keeps its traceback.