"""Shoot one screenshot per X* monitor example on hot_dallas. Recipe (from /opt/lightgui-docs/shoot.py, adapted): Xvfb :107, launch the example, find the window by title with xdotool, ImageMagick `import -window`, root-crop fallback if the shot comes back pure black. Every shot is then verified non-blank with PIL (distinct colors + grayscale stddev) -- a file present is not proof it rendered. Usage: python3 shoot_monitor.py [name ...] With no args, shoots all 15. Saves to examples/shots/.png. """ import os import subprocess import sys import time REPO = "/root/lg-review/lightgui_python" DISPLAY = ":107" SHOTS = os.path.join(REPO, "examples", "shots") NAMES = [ "XHistoryGraph", "XCoreGrid", "XNetGraph", "XTempHistory", "XUsageBar", "XStackBar", "XVerticalBar", "XTachometer", "XArcGauge", "XDonutGauge", "XProcessList", "XLoadBadge", "XMonitorButton", "XMonitorGrid", "XSysBox", ] def run(cmd, env=None, timeout=30): full_env = dict(os.environ) full_env["DISPLAY"] = DISPLAY if env: full_env.update(env) return subprocess.run(cmd, env=full_env, capture_output=True, text=True, timeout=timeout) def ensure_xvfb(): if os.path.exists("/tmp/.X11-unix/X107"): return subprocess.Popen( ["Xvfb", DISPLAY, "-screen", "0", "1024x768x24"], stdout=open("/tmp/xvfb107.log", "w"), stderr=subprocess.STDOUT) for _ in range(20): if os.path.exists("/tmp/.X11-unix/X107"): return time.sleep(0.5) raise RuntimeError("Xvfb :107 did not start") def find_window(title, timeout=15.0): # 15s: cold GTK startup on a loaded box can take ~5s, and each # xdotool poll costs ~0.5s, so a 6s window only gets ~7 polls. deadline = time.time() + timeout while time.time() < deadline: proc = run(["xdotool", "search", "--name", title]) wids = proc.stdout.split() if wids: return wids[0] time.sleep(0.25) return None def black_fraction(path): from PIL import Image img = Image.open(path).convert("RGB") pixels = list(img.getdata()) black = sum(1 for r, g, b in pixels if r < 8 and g < 8 and b < 8) return black / max(1, len(pixels)) def verify(path): """(distinct_colors, stddev) -- non-blank means colors > 50 and stddev > 3 (dark panels still have text + graph lines).""" from PIL import Image, ImageStat img = Image.open(path).convert("RGB") colors = len(img.getcolors(maxcolors=1000000) or []) stddev = sum(ImageStat.Stat(img.convert("L")).stddev) return colors, stddev def shoot(name): lower = name.lower() example = os.path.join(REPO, "examples", lower + ".py") out = os.path.join(SHOTS, lower + ".png") title = "%s example" % name env = {"PYTHONPATH": REPO} proc = subprocess.Popen( ["python3", example], env={**os.environ, "DISPLAY": DISPLAY, "PYTHONPATH": REPO}, stdout=open("/tmp/shot-%s.log" % lower, "w"), stderr=subprocess.STDOUT) try: wid = find_window(title) if wid is None: return "NOWINDOW" time.sleep(3.0) # let live data flow (process CPU% needs 2+ ticks) run(["import", "-window", wid, out]) if not os.path.exists(out) or black_fraction(out) > 0.5: # Root-crop fallback (shoot.py recipe). geo = run(["xdotool", "getwindowgeometry", wid]).stdout # "Window 123\n Position: 100,100 (screen: 0)\n Geometry: 352x232" geom = None for line in geo.splitlines(): if "Geometry:" in line: geom = line.split("Geometry:")[1].strip() if geom: w, h = geom.split("x") pos = None for line in geo.splitlines(): if "Position:" in line: pos = line.split("Position:")[1].split("(")[0].strip() x, y = pos.split(",") if pos else ("0", "0") run(["import", "-window", "root", "-crop", "%sx%s+%s+%s" % (w, h, x, y), out]) if not os.path.exists(out): return "SHOTFAIL" colors, stddev = verify(out) if colors <= 50 or stddev <= 3: return "BLANK(colors=%d stddev=%.1f)" % (colors, stddev) return "OK(colors=%d stddev=%.1f)" % (colors, stddev) finally: proc.terminate() try: proc.wait(timeout=5) except subprocess.TimeoutExpired: proc.kill() def main(): names = sys.argv[1:] or NAMES ensure_xvfb() os.makedirs(SHOTS, exist_ok=True) results = {} for name in names: try: results[name] = shoot(name) except Exception as exc: # noqa: BLE001 - report per widget results[name] = "ERROR %r" % (exc,) print("%-16s %s" % (name, results[name]), flush=True) failed = [n for n, r in results.items() if not r.startswith("OK")] print("shot %d/%d OK" % (len(results) - len(failed), len(results))) if failed: print("FAILED: %s" % ", ".join(failed)) return 1 return 0 if __name__ == "__main__": sys.exit(main())