Kaleido/scripts/device.py
Elijah 784921dba9
All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m25s
Release Kaleido 0.1.9 with responsive navigation and clearer show details
2026-10-26 09:57:02 -07:00

46 lines
3.3 KiB
Python

"""Operate only Kaleido on an authorized streamer. Credentials stay on stdin."""
import argparse, json, os, pathlib, shutil, subprocess, sys, time, xml.etree.ElementTree as ET
ROOT=pathlib.Path(__file__).resolve().parents[1]
ADB=pathlib.Path(os.environ.get('KALEIDO_ADB') or shutil.which('adb') or r'C:\Users\Elijah\Desktop\platform-tools\adb.exe')
# The onn is the default; KALEIDO_DEVICE selects another authorized test streamer.
DEVICE=os.environ.get('KALEIDO_DEVICE','192.168.50.195:5555')
OUT=ROOT/'evidence/private'
OUT.mkdir(parents=True,exist_ok=True)
def adb(*args, data=None):
for attempt in range(4):
p=subprocess.run([str(ADB),'-s',DEVICE,*args],input=data,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
error=p.stderr.decode(errors='replace')
# The TCL's network ADB link drops intermittently; reconnecting restores it.
if p.returncode and attempt<3 and any(reason in error for reason in ('closed','offline',f"device '{DEVICE}' not found",f"no such device '{DEVICE}'")):
subprocess.run([str(ADB),'disconnect',DEVICE],capture_output=True);time.sleep(2)
subprocess.run([str(ADB),'connect',DEVICE],capture_output=True);time.sleep(2);continue
# It also occasionally fails a shell command silently. Retry, except key input,
# which may already have been delivered.
if p.returncode and attempt<3 and not error.strip() and args[:2]!=('shell','input'):time.sleep(1);continue
if p.returncode: raise RuntimeError(error)
return p.stdout
def screenshot(name):
path=OUT/(name+'.png');path.write_bytes(adb('exec-out','screencap','-p'));print(path)
def hierarchy():
adb('shell','uiautomator','dump','/data/local/tmp/kaleido-window.xml')
raw=adb('shell','cat','/data/local/tmp/kaleido-window.xml');(OUT/'window.xml').write_bytes(raw)
root=ET.fromstring(raw)
for n in root.iter('node'):
if n.get('text') or n.get('focused')=='true':print({k:n.get(k) for k in ['text','content-desc','focused','bounds']})
def seed():
env=dict(l.strip().split('=',1) for l in (ROOT/'.env').read_text().splitlines() if '=' in l and not l.startswith('#'))
data=json.dumps({'server':env['PLEX_SERVER_URL'].strip('\"\''),'token':env['PLEX_TOKEN'].strip('\"\'')}).encode()
adb('shell','am','force-stop','tv.kaleido.app')
adb('shell','run-as','tv.kaleido.app','mkdir','-p','files')
adb('shell','run-as','tv.kaleido.app','sh','-c','"cat > files/bootstrap.json"',data=data)
adb('shell','am','start','-n','tv.kaleido.app/.MainActivity')
print('Private test session provisioned. No credentials are embedded in the APK.')
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('action',choices=['seed','screenshot','hierarchy','key','launch','logs']);p.add_argument('value',nargs='?',default='screen');a=p.parse_args()
if a.action=='seed':seed()
if a.action=='screenshot':screenshot(a.value)
if a.action=='hierarchy':hierarchy()
if a.action=='key':adb('shell','input','keyevent',a.value)
if a.action=='launch':print(adb('shell','am','start','-n','tv.kaleido.app/.MainActivity').decode())
if a.action=='logs':
text=adb('logcat','-d','-t','2000','KaleidoPlayback:I','AndroidRuntime:E','ExoPlayerImpl:I','*:S').decode(errors='replace');(OUT/'device.log').write_text(text);print(text[-16000:])