Skip to content

Instantly share code, notes, and snippets.

@hartwork
Created November 25, 2023 01:08
Show Gist options
  • Save hartwork/ef18f3d80dad9f13bbecb8d0f8456b87 to your computer and use it in GitHub Desktop.
Save hartwork/ef18f3d80dad9f13bbecb8d0f8456b87 to your computer and use it in GitHub Desktop.
Allow running e.g. asciinema in headless CI
#! /usr/bin/env python3
# Copyright (c) 2015 by pyte authors and contributors
# Copyright (c) 2023 by Sebastian Pipping <[email protected]>
#
# Licensed under LGPL v3, see pyte's LICENSE file for more details.
#
# Based on pyte's example "capture.py"
# https://raw.githubusercontent.com/selectel/pyte/master/examples/capture.py
import os
import pty
import signal
import select
import sys
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(0)
p_pid, master_fd = pty.fork()
if p_pid == 0: # Child.
env = os.environ.copy()
env['TERM'] = 'xterm-256color'
os.execvpe(sys.argv[1], sys.argv[1:], env=env)
assert False # never gets here
# Parent.
while True:
try:
[_master_fd], _w, _x = select.select([master_fd], [], [])
except (KeyboardInterrupt, # Stop right now!
ValueError): # Nothing to read.
break
try:
data = os.read(master_fd, 1024)
except OSError:
break
if not data:
break
os.kill(p_pid, signal.SIGTERM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment