-
Notifications
You must be signed in to change notification settings - Fork 832
/
terminal.py
281 lines (227 loc) · 7.51 KB
/
terminal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from gevent.socket import wait_read, wait_write
import fcntl
import gevent
import logging
import os
import pty
import setproctitle
import signal
import struct
import subprocess
import sys
import termios
import pyte
from aj.util import BroadcastQueue
class Terminal():
"""
Terminal emulator based on pyte.
"""
def __init__(self, manager=None, id=None, command=None, autoclose=False, autoclose_retain=5, redirect=None):
"""
Setup the environment for a new terminal.
:param manager: TerminalManager object
:type manager: TerminalManager
:param id: Id fo the terminal
:type id: hex
:param command: Command to run
:type command: string
:param autoclose: Parameter to close the terminal after the command
:type autoclose: bool
:param autoclose_retain: Time to wait before closing in seconds
:type autoclose_retain: integer
:param redirect: Default redirect URL after closing
:type redirect: string
"""
self.width = 80
self.height = 25
self.id = id
self.manager = manager
self.autoclose = autoclose
self.autoclose_retain = autoclose_retain
if redirect:
self.redirect = redirect
else:
self.redirect = '/view/terminal'
self.output = BroadcastQueue()
env = {}
env.update(os.environ)
env['TERM'] = 'linux'
env['COLUMNS'] = str(self.width)
env['LINES'] = str(self.height)
env['LC_ALL'] = 'en_US.UTF8'
self.command = command
if not self.command:
shell = os.environ.get('SHELL', None)
if not shell:
for sh in ['zsh', 'bash', 'sh']:
try:
shell = subprocess.check_output(['which', sh])
break
except Exception as e:
pass
self.command = shell
args = ['sh', '-c', self.command]
exe = 'sh'
logging.info(f'Activating new terminal: {self.command}')
self.pid, self.fd = pty.fork()
if self.pid == 0:
setproctitle.setproctitle(f'{sys.argv[0]} terminal session #{os.getpid():d}')
os.execvpe(exe, args, env)
logging.info(f'Subprocess PID {self.pid}')
self.dead = False
fl = fcntl.fcntl(self.fd, fcntl.F_GETFL)
fcntl.fcntl(self.fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
self.stream_in = os.fdopen(self.fd, 'rb', 0)
self.stream_out = os.fdopen(self.fd, 'wb', 0)
self.pyte_stream = pyte.Stream()
self.screen = pyte.DiffScreen(self.width, self.height)
self.pyte_stream.attach(self.screen)
self.last_cursor_position = None
self.reader = gevent.spawn(self.reader_fn)
def reader_fn(self):
"""
Infinite reader of the file descriptor associated with the shell.
"""
while True:
wait_read(self.fd)
self.run_single_read()
self._check()
if self.dead:
return
def run_single_read(self):
"""
Propagate the new data read in the file descriptor.
"""
try:
data = self.stream_in.read()
except IOError:
data = ''
if data:
try:
self.pyte_stream.feed(data.decode('utf-8'))
self.broadcast_update()
except UnicodeDecodeError:
pass
def broadcast_update(self):
"""
Send the new formated data to the broadcast queue.
"""
self.output.broadcast(self.format())
def _check(self):
"""
Check if a child process of the shell pid is available.
"""
try:
pid, status = os.waitpid(self.pid, os.WNOHANG)
except OSError:
self.on_died(code=0)
return
if pid:
self.on_died(code=status)
def on_died(self, code=0):
"""
Change terminal status to dead, broadcast new status and autoclose it.
:param code: Status code of the pid
:type code: integer
"""
if self.dead:
return
logging.info(f'Terminal {self.id} has died')
self.dead = True
if code:
self.pyte_stream.feed('\n\n * ' + f'Process has exited with status {code}')
else:
self.pyte_stream.feed('\n\n * ' + 'Process has exited successfully')
self.broadcast_update()
if self.autoclose:
gevent.spawn_later(self.autoclose_retain, self.manager.remove, self.id)
"""
TODO
if self.callback:
try:
self.callback(exitcode=code)
except TypeError:
self.callback()
"""
def has_updates(self):
"""
Test if the user made some changes.
:return: Bool
:rtype: bool
"""
if self.last_cursor_position != (self.screen.cursor.x, self.screen.cursor.y):
return True
return len(self.screen.dirty) > 0
def format(self, full=False):
"""
Prepare the terminal content and attributes to send to the frontend.
:param full: To send the full buffer
:type full: bool
:return: Attributes and content of the terminal
:rtype: dict
"""
def compress(line):
return [[tok or 0 for tok in ch] for _, ch in line.items()]
l = {}
self.screen.dirty.add(self.screen.cursor.y)
if self.last_cursor_position:
self.screen.dirty.add(self.last_cursor_position[1])
for k in self.screen.dirty:
if k < len(self.screen.buffer):
l[k] = compress(self.screen.buffer[k])
self.screen.dirty.clear()
if full:
l = [compress(x) for _, x in self.screen.buffer.items()]
r = {
'lines': l,
'cx': self.screen.cursor.x,
'cy': self.screen.cursor.y,
'cursor': not self.screen.cursor.hidden,
'h': self.screen.lines,
'w': self.screen.columns,
}
self.last_cursor_position = (self.screen.cursor.x, self.screen.cursor.y)
return r
def feed(self, data):
"""
Write input data into the file descriptor.
:param data: New data from the user
:type data: string
"""
wait_write(self.fd)
self.stream_out.write(data.encode('utf-8'))
self.stream_out.flush()
def resize(self, w, h):
"""
Resize terminal to new width and height.
:param w: Width
:type w: integer
:param h: Height
:type h: integer
"""
if self.dead:
return
if (h, w) == (self.screen.lines, self.screen.columns):
return
if w <= 0 or h <= 0:
return
self.width = w
self.height = h
logging.debug(f'Resizing terminal to {w}x{h}')
self.screen.resize(h, w)
winsize = struct.pack("HHHH", h, w, 0, 0)
fcntl.ioctl(self.fd, termios.TIOCSWINSZ, winsize)
def kill(self):
"""
Kill this terminal through killing the process associated with the pid.
"""
self.reader.kill(block=False)
logging.info(f'Killing subprocess PID {self.pid}')
try:
os.killpg(self.pid, signal.SIGTERM)
except OSError:
pass
try:
os.kill(self.pid, signal.SIGKILL)
except OSError:
pass