#!/usr/bin/env python3 """A stand-in for `grim`: writes one fake PNG frame to stdout. Injected through `captureCommand` (see docs/CONTRACT.md) so no unit test needs a compositor or a GPU. One script plays every part the server has to cope with: a healthy backend, a broken one, a silent one, a slow one, and one that only starts working later. """ from __future__ import annotations import argparse import os import struct import sys import time import zlib def png_bytes(width: int = 16, height: int = 8, color: tuple[int, int, int] = (0, 0, 0), level: int = 6) -> bytes: """A real, decodable RGB PNG of a solid colour. `level` 0 stores the pixels uncompressed, which is how the tests get a frame big enough to fill a socket buffer. """ def chunk(tag: bytes, data: bytes) -> bytes: return (struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF)) ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) row = bytes(color) * width raw = b"".join(b"\x00" + row for _ in range(height)) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, level)) + chunk(b"IEND", b"")) def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--width", type=int, default=16) parser.add_argument("--height", type=int, default=8) parser.add_argument("--counter", metavar="PATH", help="file holding a call count; each frame's colour " "differs so frames are distinguishable") parser.add_argument("--gate", metavar="PATH", help="fail until this path exists (a compositor that " "is not up yet)") parser.add_argument("--fail", action="store_true", help="exit non-zero") parser.add_argument("--empty", action="store_true", help="exit 0 having written nothing") parser.add_argument("--garbage", action="store_true", help="write output that is not an image") parser.add_argument("--delay", type=float, default=0.0, help="sleep this long before answering") parser.add_argument("--big", action="store_true", help="store the pixels uncompressed (a large frame)") parser.add_argument("--message", default="fake_capture: simulated failure") return parser def main(argv: list[str] | None = None) -> int: args = build_parser().parse_args(argv) if args.delay: time.sleep(args.delay) if args.gate and not os.path.exists(args.gate): sys.stderr.write(f"fake_capture: gate {args.gate} is missing\n") return 4 if args.fail: sys.stderr.write(args.message + "\n") return 3 if args.empty: sys.stderr.write("fake_capture: nothing to capture\n") return 0 count = 0 if args.counter: try: with open(args.counter) as handle: count = int(handle.read() or 0) except FileNotFoundError: count = 0 count += 1 with open(args.counter, "w") as handle: handle.write(str(count)) if args.garbage: sys.stdout.buffer.write(b"fake_capture: not an image, sorry\n") sys.stdout.buffer.flush() return 0 colour = (count % 251, (count * 7) % 251, (count * 13) % 251) sys.stdout.buffer.write( png_bytes(args.width, args.height, colour, 0 if args.big else 6) ) sys.stdout.buffer.flush() return 0 if __name__ == "__main__": raise SystemExit(main())