okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
1#!/usr/bin/env python3 2"""Run a command in a real terminal (a pty) and answer its questions, for the real-package flow tests (update_model_flow.sh). 3 4pty_drive.py [--timeout S] [--cols N --rows N] [--quit-after S --quit-with KEYS] [--answer 'REGEX=>TEXT' ...] -- command args 5 6Each --answer is used once, in order, the first time its REGEX shows in what the command printed after the previous answer; TEXT is typed followed by 7Enter (TEXT may be empty). The whole transcript goes to stdout (control codes kept: the screens are drawn with them); the exit code is the command's, 8or 124 when the time ran out. Standard library only. 9""" 10import argparse 11import fcntl 12import os 13import pty 14import re 15import select 16import struct 17import sys 18import termios 19import time 20 21 22def main ()-> int : 23ap = argparse .ArgumentParser () 24ap .add_argument ("--timeout" ,type = float ,default = 600 ) 25ap .add_argument ("--cols" ,type = int ,default = 140 ) 26ap .add_argument ("--rows" ,type = int ,default = 45 ) 27ap .add_argument ("--answer" ,action = "append" ,default = []) 28ap .add_argument ("--quit-after" ,type = float ,default = 0 ,help = "seconds after which --quit-with is typed (a screen that never ends by itself)" ) 29ap .add_argument ("--quit-with" ,default = "q" ) 30ap .add_argument ("cmd" ,nargs = argparse .REMAINDER ) 31a = ap .parse_args () 32cmd = a .cmd [1 :]if a .cmd and a .cmd [0 ]== "--" else a .cmd 33answers = [] 34for item in a .answer : 35pattern ,_ ,text = item .partition ("=>" ) 36answers .append ((re .compile (pattern ),text )) 37pid ,fd = pty .fork () 38if pid == 0 : 39os .environ ["TERM" ]= os .environ .get ("TERM" ,"xterm-256color" ) 40os .execvp (cmd [0 ],cmd ) 41fcntl .ioctl (fd ,termios .TIOCSWINSZ ,struct .pack ("HHHH" ,a .rows ,a .cols ,0 ,0 )) 42start = time .monotonic () 43seen = "" # what was printed since the last answer 44quit_sent = False 45code = None 46while True : 47if time .monotonic ()- start > a .timeout : 48os .kill (pid ,15 ) 49code = 124 50break 51if a .quit_after and not quit_sent and time .monotonic ()- start > a .quit_after : 52os .write (fd ,a .quit_with .encode ()) 53quit_sent = True 54ready ,_ ,_ = select .select ([fd ], [], [],0.5 ) 55if ready : 56try : 57data = os .read (fd ,65536 ) 58except OSError : 59data = b"" 60if not data : 61break 62text = data .decode ("utf-8" ,"replace" ) 63sys .stdout .write (text ) 64sys .stdout .flush () 65seen += text 66plain = re .sub (r"\x1b\[[0-9;?]*[A-Za-z]" ,"" ,seen ) 67if answers and answers [0 ][0 ].search (plain ): 68os .write (fd , (answers [0 ][1 ]+ "\n" ).encode ()) 69answers .pop (0 ) 70seen = "" 71try : 72_ ,status = os .waitpid (pid ,0 ) 73if code is None : 74code = os .waitstatus_to_exitcode (status ) 75except ChildProcessError : 76code = code if code is not None else 0 77sys .stdout .write (f"\n[pty_drive] exit { code } ; unanswered: { [ p . pattern for p , _ in answers ] } \n" ) 78return code 79 80 81if __name__ == "__main__" : 82sys .exit (main ())