fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
190d367bc
master
1"""pexpect_helper provides a wrapper around the pexpect module. 2 3This module exposes a single class SpawnedProc, which wraps pexpect.spawn(). 4This exposes a pseudo-tty, which fish or another process may talk to. 5The send() function may be used to send data to fish, and the expect_* family 6of functions may be used to match what is output to the tty. 7 8Example usage: 9sp = SpawnedProc() # this launches fish 10sp.expect_prompt() # wait for a prompt 11sp.sendline("echo hello world") 12sp.expect_prompt("hello world") 13 14""" 15 16from __future__import print_function 17 18import inspect 19import os 20import os .path 21import re 22import sys 23import time 24import pexpect 25from signal import Signals 26 27# Default timeout for failing to match. 28TIMEOUT_SECS = 5 29 30UNEXPECTED_SUCCESS = object () 31 32# When rendering fish's output, remove the control sequences that modify terminal state, 33# to avoid confusing the calling terminal. 34SANITIZE_FOR_PRINTING_RE = re .compile ( 35r""" 36# Filter CSI commands except for colors and cursor movement. 37(?!\x1b\[\d*m) 38(?!\x1b\[K) 39(?!\x1b\[\d*[ABCD]) 40\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e] 41# OSC 42| \x1b\].*?\x07 43# DCS 44| \x1bP.*?\x1b\\ 45# application keypad mode 46| \x1b= 47| \x1b> 48""" , 49re .VERBOSE , 50) 51 52assert SANITIZE_FOR_PRINTING_RE .sub ("" ,"\x1b[>4;1m" )== "" 53assert SANITIZE_FOR_PRINTING_RE .sub ("" ,"\x1b[31m" )== "\x1b[31m" 54 55 56def get_prompt_re (counter ): 57"""Return a regular expression for matching a with a given prompt counter.""" 58return re .compile ("prompt %d>" % counter ) 59 60 61def get_callsite (): 62"""Return a triple (filename, line_number, line_text) of the call site location.""" 63callstack = inspect .getouterframes (inspect .currentframe ()) 64for f in callstack : 65# Skip call sites from this file. 66if inspect .getmodule (f .frame )is Message .MODULE : 67continue 68# Skip functions which have a truthy callsite_skip attribute. 69if getattr (f .function ,"callsite_skip" ,False ): 70continue 71return (os .path .basename (f .filename ),f .lineno ,f .code_context ) 72return ("Unknown" ,- 1 ,"" ) 73 74 75def escape (s ): 76"""Escape the string 's' to make it human-understandable.""" 77res = [] 78for c in s : 79if c == "\n" : 80res .append ("\\n" ) 81elif c == "\r" : 82res .append ("\\r" ) 83elif c == "\t" : 84res .append ("\\t" ) 85elif c .isprintable (): 86res .append (c ) 87else : 88res .append ("\\x{:02x}" .format (ord (c ))) 89return "" .join (res ) 90 91 92def pexpect_error_type (err ): 93"""Return a human-readable description of a pexpect error type.""" 94if isinstance (err ,pexpect .EOF ): 95return "EOF" 96elif isinstance (err ,pexpect .TIMEOUT ): 97return "timeout" 98elif err is UNEXPECTED_SUCCESS : 99return "unexpected success" 100else : 101return "unknown error" 102 103 104class Message (object ): 105"""Some text either sent-to or received-from the spawned proc. 106 107Attributes: 108dir: the message direction, either DIR_INPUT or DIR_OUTPUT 109filename: the name of the file from which the message was sent 110text: the text of the messages 111when: a timestamp of when the message was sent 112""" 113 114# Input is input into fish shell ("sent data"). 115DIR_INPUT = " INPUT" 116 117# Output means output from fish shell ("received data"). 118DIR_OUTPUT = "OUTPUT" 119 120MODULE = sys .modules [__name__ ] 121 122def __init__ (self ,dir ,text ,when ): 123"""Construct from a direction, message text and timestamp.""" 124self .dir = dir 125self .filename ,self .lineno ,_ = get_callsite () 126self .text = text 127self .when = when 128 129@staticmethod 130def sent_input (text ,when ): 131"""Return an input message with the given text.""" 132return Message (Message .DIR_INPUT ,text ,when ) 133 134@staticmethod 135def received_output (text ,when ): 136"""Return a output message with the given text.""" 137return Message (Message .DIR_OUTPUT ,text ,when ) 138 139 140class SpawnedProc (object ): 141"""A process, talking to our ptty. This wraps pexpect.spawn. 142 143Attributes: 144colorize: whether error messages should have ANSI color escapes 145messages: list of Message sent and received, in-order 146start_time: the timestamp of the first message, or None if none yet 147spawn: the pexpect.spawn value 148prompt_counter: the index of the prompt. This cooperates with the fish_prompt 149function to ensure that each printed prompt is distinct. 150""" 151 152def __init__ ( 153self , 154name = "fish" , 155timeout = TIMEOUT_SECS , 156env = os .environ .copy (), 157scroll_content_up_supported :bool = False , 158** kwargs , 159 ): 160"""Construct from a name, timeout, and environment. 161 162Args: 163name: the name of the executable to launch, as a key into the 164environment dictionary. By default this is 'fish' but may be 165other executables. 166timeout: A timeout to pass to pexpect. This indicates how long to wait 167before giving up on some expected output. 168env: a string->string dictionary, describing the environment variables. 169""" 170import shlex 171 172if name not in env : 173raise ValueError ("'%s' variable not found in environment" % name ) 174exe_path = env .get (name ) 175# HACK: If there are no args, pexpect will fail if exe_path contains any shell metachars. 176# But not if there are args, in which case it probably switches spawning method? 177if "args" not in kwargs : 178exe_path = shlex .quote (exe_path ) 179self .colorize = sys .stdout .isatty ()or env .get ("FISH_FORCE_COLOR" ,"0" )== "1" 180self .messages = [] 181self .start_time = None 182if "FISH_TEST_NO_RECURRENT_QUERIES" not in env : 183env ["FISH_TEST_NO_RECURRENT_QUERIES" ]= "1" 184self .spawn = pexpect .spawn ( 185exe_path ,env = env ,encoding = "utf-8" ,timeout = timeout ,** kwargs 186 ) 187self .spawn .delaybeforesend = None 188self .prompt_counter = 0 189if scroll_content_up_supported : 190# XTGETTCAP 191key = bytes .hex (b"indn" ) 192value = bytes .hex (b"dont-care" ) 193self .spawn .send (f"\x1bP1+r { key } = { value } \x1b\\" ) 194if env .get ("TERM" )!= "dumb" : 195self .send_primary_device_attribute () 196 197def send_cursor_position_report (self ,* ,y :int ,x :int ): 198assert x != 0 199assert y != 0 200self .spawn .send (f"\x1b[ { y } ; { x } R" ) 201 202def send_primary_device_attribute (self ): 203self .spawn .send ("\x1b[?123c" )# Primary Device Attribute 204 205def time_since_first_message (self ): 206"""Return a delta in seconds since the first message, or 0 if this is the first.""" 207now = time .monotonic () 208if not self .start_time : 209self .start_time = now 210return now - self .start_time 211 212def send (self ,s ): 213"""Cover over pexpect.spawn.send(). 214Send the given string to the tty, returning the number of bytes written. 215""" 216res = self .spawn .send (s ) 217when = self .time_since_first_message () 218self .messages .append (Message .sent_input (s ,when )) 219return res 220 221def sendline (self ,s ): 222"""Cover over pexpect.spawn.sendline(). 223Send the given string + linesep to the tty, returning the number of bytes written. 224""" 225return self .send (s + os .linesep ) 226 227def expect_re (self ,pat ,pat_desc = None ,unmatched = None ,shouldfail = False ,** kwargs ): 228"""Cover over pexpect.spawn.expect(). 229Consume all "new" output of self.spawn until the given pattern is matched, or 230the timeout is reached. 231Note that output between the current position and the location of the match is 232consumed as well. 233The pattern is typically a regular expression in string form, but may also be 234any of the types accepted by pexpect.spawn.expect(). 235If the 'unmatched' parameter is given, it is printed as part of the error message 236of any failure. 237On failure, this prints an error and exits. 238""" 239try : 240self .spawn .expect (pat ,** kwargs ) 241when = self .time_since_first_message () 242self .messages .append ( 243Message .received_output (self .spawn .match .group (),when ) 244 ) 245# When a match is found, 246# spawn.match is the MatchObject that produced it. 247# This can be used to check what exactly was matched. 248if shouldfail : 249err = UNEXPECTED_SUCCESS 250if not pat_desc : 251pat_desc = str (pat ) 252self .report_exception_and_exit (pat_desc ,unmatched ,err ) 253return self .spawn .match 254except pexpect .ExceptionPexpect as err : 255if shouldfail : 256return True 257if not pat_desc : 258pat_desc = str (pat ) 259self .report_exception_and_exit (pat_desc ,unmatched ,err ) 260 261def expect_str (self ,s ,** kwargs ): 262"""Cover over expect_re() which accepts a literal string.""" 263return self .expect_re (re .escape (s ),** kwargs ) 264 265def expect_prompt (self ,* args ,increment = True ,** kwargs ): 266"""Convenience function which matches some text and then a prompt. 267Match the given positional arguments as expect_re, and then look 268for a prompt. 269If increment is set, then this should be a new prompt and the prompt counter 270should be bumped; otherwise this is not a new prompt. 271Returns None on success, and exits on failure. 272Example: 273sp.sendline("echo hello world") 274sp.expect_prompt("hello world") 275""" 276if args : 277self .expect_re (* args ,** kwargs ) 278if increment : 279self .prompt_counter += 1 280self .expect_re ( 281get_prompt_re (self .prompt_counter ), 282pat_desc = "prompt %d" % self .prompt_counter , 283 ) 284 285def report_exception_and_exit (self ,pat ,unmatched ,err ): 286"""Things have gone badly. 287We have an exception 'err', some pexpect.ExceptionPexpect. 288Report it to stdout, along with the offending call site. 289If 'unmatched' is set, print it to stdout. 290""" 291# Close the process so we can get the status 292self .spawn .close () 293colors = self .colors () 294failtype = pexpect_error_type (err ) 295# If we get an EOF, we check if the process exited with a signal. 296# This shows us e.g. if it crashed 297if ( 298failtype == "EOF" 299and self .spawn .signalstatus is not None 300and self .spawn .signalstatus != 0 301 ): 302failtype = "SIGNAL " + Signals (self .spawn .signalstatus ).name 303 304fmtkeys = {"failtype" :failtype ,"pat" :escape (pat )} 305fmtkeys .update (** colors ) 306 307filename ,lineno ,code_context = get_callsite () 308fmtkeys ["filename" ]= filename 309fmtkeys ["lineno" ]= lineno 310fmtkeys ["code" ]= "\n" .join ([n .strip ()for n in code_context if n ]) 311 312if unmatched : 313"{RED}Error: {NORMAL}{BOLD}{unmatched}{RESET}" .format ( 315unmatched = unmatched ,** fmtkeys 316 ) 317 ) 318"{RED}Failed to match pattern:{NORMAL} {BOLD}{pat}{RESET}" .format (** fmtkeys ) 320 ) 321"{filename}:{lineno}: {BOLD}{failtype}{RESET} from {code}" .format (** fmtkeys ) 323 ) 324 325"" ) 326"{CYAN}Escaped buffer:{RESET}" .format (** colors )) 327escape (self .spawn .before )) 328"" ) 329"{CYAN}When written to the tty, this looks like:{RESET}" .format (** colors )) 330"{CYAN}<-------{RESET}" .format (** colors )) 331sys .stdout .write (SANITIZE_FOR_PRINTING_RE .sub ("" ,self .spawn .before )) 332sys .stdout .flush () 333maybe_nl = "" 334if not self .spawn .before .endswith ("\n" ): 335maybe_nl = "\n{CYAN}(no trailing newline)" .format (** colors ) 336"{RESET}{maybe_nl}{CYAN}------->{RESET}" .format (maybe_nl = maybe_nl ,** colors ) 338 ) 339 340"" ) 341 342# Show the last 10 messages. 343"Last 10 messages:" ) 344delta = None 345for m in self .messages [- 10 :]: 346etext = escape (m .text ) 347timestamp = m .when * 1000.0 348# Use relative timestamps and add a sign. 349# This assumes a max length of 10^10 milliseconds (115 days) for the initial timestamp, 350# and 11.5 days for the delta. 351if delta : 352timestamp -= delta 353timestampstr = "{timestamp:+10.2f} ms" .format (timestamp = timestamp ) 354else : 355timestampstr = "{timestamp:10.2f} ms" .format (timestamp = timestamp ) 356delta = m .when * 1000.0 357"{dir} {timestampstr} (Line {lineno}): {BOLD}{etext}{RESET}" .format ( 359dir = m .dir , 360timestampstr = timestampstr , 361filename = m .filename , 362lineno = m .lineno , 363etext = etext , 364** colors , 365 ) 366 ) 367"" ) 368sys .exit (1 ) 369 370def sleep (self ,secs ): 371"""Cover over time.sleep().""" 372time .sleep (secs ) 373 374def colors (self ): 375"""Return a dictionary mapping color names to ANSI escapes""" 376 377def ansic (n ): 378"""Return either an ANSI escape sequence for a color, or empty string.""" 379return "\033[%dm" % n if self .colorize else "" 380 381return { 382"RESET" :ansic (0 ), 383"BOLD" :ansic (1 ), 384"NORMAL" :ansic (39 ), 385"BLACK" :ansic (30 ), 386"RED" :ansic (31 ), 387"GREEN" :ansic (32 ), 388"YELLOW" :ansic (33 ), 389"BLUE" :ansic (34 ), 390"MAGENTA" :ansic (35 ), 391"CYAN" :ansic (36 ), 392"LIGHTGRAY" :ansic (37 ), 393"DARKGRAY" :ansic (90 ), 394"LIGHTRED" :ansic (91 ), 395"LIGHTGREEN" :ansic (92 ), 396"LIGHTYELLOW" :ansic (93 ), 397"LIGHTBLUE" :ansic (94 ), 398"LIGHTMAGENTA" :ansic (95 ), 399"LIGHTCYAN" :ansic (96 ), 400"WHITE" :ansic (97 ), 401 } 402 403 404def control (char :str )-> str : 405"""Returns the char sent when control is pressed along the given key.""" 406assert len (char )== 1 407char = char .lower () 408if ord ("a" )<= ord (char )<= ord ("z" ): 409return chr (ord (char )- ord ("a" )+ 1 ) 410return chr ( 411 { 412"@" :0 , 413"`" :0 , 414"[" :27 , 415"{" :27 , 416"\\" :28 , 417"|" :28 , 418"]" :29 , 419"}" :29 , 420"^" :30 , 421"~" :30 , 422"_" :31 , 423"?" :127 , 424 }[char ] 425 )