Skip to content

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

tools: a TV trace.py cannot reach gets tv.py's sentence and exit 2

xbsheng74b270381b09
diff --git a/tests/test_trace.py b/tests/test_trace.py
index b01af143b..66e212a9b 100644
--- a/tests/test_trace.py
+++ b/tests/test_trace.py
@@ -170,6 +170,79 @@ class ArgumentTest(unittest.TestCase):
self.assertIn("is a file", err.getvalue())
+class ReachTest(unittest.TestCase):
+ """A TV the client cannot reach: tools/tv.py's sentence and exit 2, before anything is recorded.
+
+ The handler used to catch OSError only and answer with a line of this file's own and exit 1, which is
+ the status a recording that started and then failed leaves - and a Wireless debugging port, which
+ adb_shell reports as an InvalidCommandError that is not an OSError, was not caught at all and ended in
+ a traceback naming neither the port nor the way to it.
+ """
+
+ REFUSED = ("192.0.2.7:5555 refused the connection: nothing listens for ADB there, so network ADB is "
+ "off or on another port.")
+ TLS = "192.0.2.7:5555 is a Wireless debugging port: it asks for TLS, which tv.py cannot use."
+
+ def run_main(self, connect, connect_error=None):
+ """main() with a faked tv/bench, as tests/test_trace.py fakes them everywhere else."""
+ fake_tv = types.ModuleType("tv")
+ fake_tv.HOST, fake_tv.PORT = "192.0.2.7", "5555"
+ fake_tv.connect = connect
+ fake_tv.connect_error = connect_error or (lambda error: None)
+ fake_bench = types.ModuleType("bench")
+ fake_bench.accessibility_services = lambda *a: []
+ fake_bench.restore_accessibility = lambda *a: None
+ return mock.patch.dict(sys.modules, {"tv": fake_tv, "bench": fake_bench})
+
+ def test_a_refused_port_is_the_client_s_sentence_and_exit_2(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ out = Path(tmp) / "before"
+
+ def refuse():
+ raise ConnectionRefusedError(111, "Connection refused")
+
+ with self.run_main(refuse, lambda error: self.REFUSED \
+ if isinstance(error, ConnectionRefusedError) else None), \
+ contextlib.redirect_stderr(io.StringIO()) as err, \
+ contextlib.redirect_stdout(io.StringIO()) as out_text, \
+ self.assertRaises(SystemExit) as caught:
+ trace.main(["coldsys", "--out", str(out)])
+ self.assertEqual(caught.exception.code, 2)
+ self.assertIn("refused the connection", err.getvalue())
+ self.assertIn("Nothing was recorded", err.getvalue())
+ self.assertEqual(out_text.getvalue(), "", "a run that never started wrote to stdout")
+ self.assertFalse(out.exists(), "the folder was made for a run that never started")
+
+ def test_a_port_that_asks_for_tls_is_answered_too(self):
+ # adb_shell's InvalidCommandError carries this one, and it is not an OSError: the old handler let it
+ # through as a traceback with exit 1.
+ class NotAnOSError(Exception):
+ pass
+
+ def ask_for_tls():
+ raise NotAnOSError("STLS requested")
+
+ with tempfile.TemporaryDirectory() as tmp:
+ out = Path(tmp) / "before"
+ with self.run_main(ask_for_tls, lambda error: self.TLS \
+ if isinstance(error, NotAnOSError) else None), \
+ contextlib.redirect_stderr(io.StringIO()) as err, \
+ self.assertRaises(SystemExit) as caught:
+ trace.main(["nav", "--out", str(out)])
+ self.assertEqual(caught.exception.code, 2)
+ self.assertIn("asks for TLS", err.getvalue())
+ self.assertFalse(out.exists())
+
+ def test_an_error_the_client_has_nothing_to_add_to_keeps_its_traceback(self):
+ def unreadable_key():
+ raise RuntimeError("cannot read the ADB key")
+
+ with tempfile.TemporaryDirectory() as tmp, self.run_main(unreadable_key), \
+ contextlib.redirect_stderr(io.StringIO()):
+ with self.assertRaises(RuntimeError):
+ trace.main(["coldsys", "--out", str(tmp)])
+
+
class RecordTest(unittest.TestCase):
def record(self, device, setup, steps):
with tempfile.TemporaryDirectory() as tmp, mock.patch.object(trace.time, "sleep") as sleep:
diff --git a/tools/trace.md b/tools/trace.md
index a5630b5d1..e4494904a 100644
--- a/tools/trace.md
+++ b/tools/trace.md
@@ -31,6 +31,11 @@ TV_HOST=<tv ip> tools/venv/bin/python -I tools/trace.py home --runs 5 --out trac
`--out` is the folder the traces are written into, and it is created if it is not there. A file where the
folder goes, a path through a file and an empty `--out` are refused before the TV is asked anything.
+A TV that cannot be reached stops the run with `tools/tv.py`'s sentence - a port that refuses, a Wireless
+debugging port that asks for TLS, an address nothing answers on - and exit 2, before the folder is made.
+Nothing was recorded, so nothing is half-written: `tools/bench.py`, `tools/check.py` and
+`tools/b64push.py` answer a TV like that the same way.
+
| Scenario | Recorded | Use for |
|---|---|---|
| `home` | HOME pressed while another app is in front | Most Home changes |
diff --git a/tools/trace.py b/tools/trace.py
index 67b64327d..190335fb7 100644
--- a/tools/trace.py
+++ b/tools/trace.py
@@ -21,6 +21,12 @@ asked anything, so a typo cannot read as a recording that failed. Started with a
packages, it runs again under tools/venv's Python, or says how to make that venv, the way tools/bench.py
does.
+Exit status: 0 every run is recorded and written; 2 an argument this run cannot use (an --out that is a
+file or passes through one, an empty --out, a --package or --app that is not one, --runs outside 1 to 50)
+and a TV that cannot be reached (tools/tv.py's sentence says which failure it was) - both judged before
+anything is recorded, so nothing was written and --out was not made; 1 a recording that started and then
+failed.
+
Scenarios (setup runs before the recording starts, so it stays out of the trace):
cold another app in front, the launcher force-stopped, then started with `am start -W`
coldsys Home in front, the launcher force-stopped: Android starts it again by itself
@@ -276,8 +282,19 @@ def main(argv=None):
try:
dev = tv.connect()
- except OSError as e:
- sys.exit(f"cannot reach the TV at {tv.HOST}:{tv.PORT} ({e}). Is it on and awake, with network debugging on?")
+ except Exception as error:
+ # tv.py words the failures that mean the TV was not reached: a port that refuses, a Wireless
+ # debugging port that asks for TLS, an address nothing answers on, and the macOS Local Network
+ # denial that looks like one. This file answered those with a line of its own and exit 1 - the
+ # status a recording that started and then failed leaves - and it caught only OSError, so the TLS
+ # one (adb_shell's InvalidCommandError is not an OSError) ended in a traceback. bench.py, check.py
+ # and b64push.py answer both with tv.py's sentence and exit 2. The errors tv.py has nothing to add
+ # to (a key file it cannot read, a reset in the handshake) keep their traceback.
+ why = tv.connect_error(error)
+ if why is None:
+ raise
+ print(f"{why}\nNothing was recorded: {args.out} is made when the first run starts.", file=sys.stderr)
+ raise SystemExit(2) from error
shell = lambda cmd: dev.shell(cmd, read_timeout_s=120.0)
try:
home = shell("cmd package resolve-activity --brief -a android.intent.action.MAIN "