Skip to content

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

tools: trace.py runs again under tools/venv's Python when adb_shell is missing

xbshenga970453b22cd
diff --git a/tools/trace.py b/tools/trace.py
index 38843edab..67b64327d 100644
--- a/tools/trace.py
+++ b/tools/trace.py
@@ -17,7 +17,9 @@ and later. Set TV_HOST to the TV's IP (and TV_PORT if it is not 5555). Usage:
--out DIR is the folder the traces go into; 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 as argument errors (exit 2) before the TV is
-asked anything, so a typo cannot read as a recording that failed.
+asked anything, so a typo cannot read as a recording that failed. Started with a Python that lacks the
+packages, it runs again under tools/venv's Python, or says how to make that venv, the way tools/bench.py
+does.
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`
@@ -28,6 +30,7 @@ Scenarios (setup runs before the recording starts, so it stays out of the trace)
"""
import argparse
import base64
+import importlib.util
import json
import os
from pathlib import Path
@@ -38,6 +41,9 @@ import time
PACKAGE = re.compile(r"^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$")
COMPONENT = re.compile(r"^[A-Za-z][A-Za-z0-9_.]*/[A-Za-z0-9_.$]+$")
REMOTE = "/data/misc/perfetto-traces/cleanyourtv.pftrace" # the folder perfetto may write to on Android 9 and 10
+# Set on the run that starts again under tools/venv's Python, so a venv without the package answers with
+# the line to run instead of starting a third process. tv.py carries the same marker for the same reason.
+VENV_MARKER = "CLEANYOURTV_TRACE_PY_VENV"
NAV_KEYS = ("RIGHT RIGHT RIGHT LEFT LEFT LEFT DOWN RIGHT DOWN LEFT DOWN UP "
"UP RIGHT UP LEFT DOWN DOWN RIGHT RIGHT UP UP UP LEFT").split()
START_MARGIN_S = 3 # recording starts in the background; give it time before the first step
@@ -217,10 +223,54 @@ def _run(run_shell, cmd):
run_shell(cmd)
+def venv_python():
+ """tools/venv's interpreter, which has adb_shell, or None."""
+ here = Path(__file__).resolve().parent
+ for candidate in (here / "venv" / "bin" / "python", here / "venv" / "Scripts" / "python.exe"):
+ if candidate.is_file():
+ return candidate
+ return None
+
+
+def client_python_or_exit():
+ """Run again under tools/venv's Python when this one has no adb_shell, or say how to make that venv.
+
+ tv.py, imported below for connect(), the shell and its timeouts, imports adb_shell, and the README
+ installs that into tools/venv only. This file is started both ways: the pages print
+ `tools/venv/bin/python -I tools/trace.py home ...`, and a reader who has just cloned the repository
+ types `python3 tools/trace.py home`. That second run ended in a ModuleNotFoundError traceback out of
+ `import tv`, naming neither tools/requirements.txt nor the venv sitting next to it - while bench.py
+ re-execs itself the same way and tv.py, check.py, privacy.py and recovery.py each answer with the
+ line to run. This is the third place the client is imported, so it answers like they do.
+ """
+ if importlib.util.find_spec("adb_shell") is not None:
+ return
+ venv = venv_python()
+ # Once only, marked in the environment as tv.py does: a venv that lacks adb_shell as well gets the
+ # line below rather than another run of itself. (tools/venv/bin/python is often a symlink to the very
+ # interpreter that started this run, so comparing the two paths would skip the venv that has the
+ # package while the one in hand does not.)
+ if venv and not os.environ.get(VENV_MARKER):
+ os.environ[VENV_MARKER] = "1"
+ # -I as in the printed commands, so a module beside the checkout cannot stand in for adb_shell.
+ os.execv(str(venv), [str(venv), "-I", os.path.abspath(__file__), *sys.argv[1:]])
+ make = (r"python -m venv tools\venv; tools\venv\Scripts\python.exe -m pip install -r tools\requirements.txt"
+ if os.name == "nt" else
+ "python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt")
+ sys.exit("tools/trace.py needs the packages in tools/requirements.txt (No module named 'adb_shell').\n"
+ f"run: {make}")
+
+
def main(argv=None):
args = parse(sys.argv[1:] if argv is None else argv)
# Imported only now, so --help and argument errors work without TV_HOST set.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+ # Only a run that started this file as a script may start it again: main(argv) is called by
+ # tests/test_trace.py with a faked TV, and sys.argv there is the test runner's own command line
+ # (`python3 -m unittest discover -s tests`), which execv would hand to trace.py's parser as
+ # 'invalid choice: discover'. A caller that passes its arguments owns the interpreter.
+ if argv is None:
+ client_python_or_exit()
import bench # noqa: E402 the accessibility helpers bench.py already tested
import tv # noqa: E402