MCPcopy Index your code
hub / github.com/bugy/script-server / PtyProcessWrapper

Class PtyProcessWrapper

src/execution/process_pty.py:28–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28class PtyProcessWrapper(process_base.ProcessWrapper):
29 def __init__(self, command, working_directory, all_env_variables):
30 super().__init__(command, working_directory, all_env_variables)
31
32 self.pty_master = None
33 self.pty_slave = None
34
35 self.encoding = get_encoding(command, working_directory)
36
37 def start_execution(self, command, working_directory):
38 master, slave = pty.openpty()
39
40 env_variables = self.prepare_env_variables()
41
42 self.process = subprocess.Popen(command,
43 cwd=working_directory,
44 stdin=slave,
45 stdout=slave,
46 stderr=slave,
47 start_new_session=True,
48 env=env_variables)
49 self.pty_slave = slave
50 self.pty_master = master
51
52 # ONLCR - transform \n to \r\n
53 _unset_output_flags(self.pty_master, termios.ONLCR)
54 fcntl.fcntl(self.pty_master, fcntl.F_SETFL, os.O_NONBLOCK)
55
56 def write_to_input(self, value):
57 if self.is_finished():
58 return
59
60 input_value = value
61 if not input_value.endswith("\n"):
62 input_value += "\n"
63
64 os.write(self.pty_master, input_value.encode())
65
66 def wait_finish(self):
67 self.process.wait()
68
69 def pipe_process_output(self):
70 utf8_stream = self.encoding.lower() == 'utf-8'
71
72 buffer = b''
73
74 try:
75 while True:
76 finished = False
77 wait_new_output = False
78
79 max_read_bytes = 1024
80
81 data = buffer
82 buffer = b''
83
84 if self.is_finished():
85 while True:

Callers 3

test_mixed_encodingMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_mixed_encodingMethod · 0.72