Created
November 25, 2023 01:08
-
-
Save hartwork/ef18f3d80dad9f13bbecb8d0f8456b87 to your computer and use it in GitHub Desktop.
Allow running e.g. asciinema in headless CI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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