-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.py
executable file
·584 lines (399 loc) · 15.1 KB
/
cmdline.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Optional, Tuple, Type
import blessed
import codecs
import fcntl
import io
import os
import termios
from asyncio import (
AbstractEventLoop,
BaseProtocol,
BaseTransport,
Protocol,
Transport,
get_event_loop,
)
from asyncio.coroutines import coroutine
from collections import deque
def _debug_fileno(filelike):
try:
fileno = filelike.fileno()
except (AttributeError, OSError):
return None
return fileno
def _debug_cls(cls):
return f"{cls.__module__}.{cls.__name__}"
def _debug_mro(obj):
return ", ".join(_debug_cls(cls) for cls in obj.__class__.__mro__)
def _accmode(mode: str) -> int:
flags = 0
for i, c in enumerate('rwxa+'):
if c in mode:
flags |= 1 << i
try:
return {
0b00001: os.O_RDONLY,
0b00010: os.O_WRONLY,
0b00100: os.O_WRONLY,
0b01000: os.O_WRONLY,
0b10001: os.O_RDWR,
0b10010: os.O_RDWR,
0b10100: os.O_RDWR,
0b11000: os.O_RDWR,
}[flags]
except KeyError:
raise ValueError(f"invalid mode {mode!r}")
class _File:
def __init__(
self, filelike: io.IOBase,
mode: str = None,
encoding: str = None,
non_blocking: bool = False,
) -> None:
if not isinstance(filelike, io.IOBase):
raise TypeError("must be a file-like object")
filelike = self._maybe_text(filelike)
filelike = self._maybe_bytes(filelike)
self._maybe_raw(filelike)
self._maybe_fd(filelike)
try:
self.isatty = filelike.isatty()
except AttributeError:
self.isatty = os.isatty(self.fd) if self.fd is not None else False
self.encoding = None
self._determine_encoding(encoding)
if non_blocking and self.fd is not None and os.get_blocking(self.fd):
os.set_blocking(self.fd, False)
mode, accmode = self._determine_mode(mode)
mode, accmode = self._maybe_raw_from_fd(mode, accmode)
mode, accmode = self._maybe_bytes_from_raw(mode, accmode)
self._maybe_text_from_bytes(mode, accmode)
def _maybe_text(self, filelike: io.IOBase) -> io.IOBase:
if (isinstance(filelike, io.TextIOBase) or
hasattr(filelike, 'encoding')):
self.text = filelike
try:
filelike = filelike.buffer
except AttributeError:
try:
# noinspection PyUnresolvedReferences
filelike = filelike.raw
except AttributeError:
pass
else:
self.text = None
return filelike
def _maybe_bytes(self, filelike: io.IOBase) -> io.IOBase:
if (isinstance(filelike, io.BufferedIOBase) or
hasattr(filelike, 'read1') or
(not hasattr(filelike, 'encoding') and
hasattr(filelike, 'detach'))):
self.bytes = filelike
try:
# noinspection PyUnresolvedReferences
filelike = filelike.raw
except AttributeError:
pass
else:
self.bytes = None
return filelike
def _maybe_raw(self, filelike: io.IOBase) -> None:
if (isinstance(filelike, io.RawIOBase) or
not hasattr(filelike, 'detach')):
self.raw = filelike
else:
self.raw = None
def _maybe_fd(self, filelike: io.IOBase) -> None:
try:
self.fd = filelike.fileno()
except (AttributeError, OSError):
self.fd = None
def _determine_encoding(self, encoding: Optional[str]) -> None:
self.encoding = encoding
if self.encoding is None and self.text is not None:
self.encoding = self.text.encoding
if self.encoding is None and self.fd is not None:
self.encoding = os.device_encoding(self.fd)
if self.encoding is None:
self.encoding = 'utf-8'
def _determine_mode(self, mode: Optional[str]) -> Tuple[str, int]:
if mode is None:
accmode = (os.O_RDWR if self.fd is None
else fcntl.fcntl(self.fd, fcntl.F_GETFL) & os.O_ACCMODE)
mode = ('rb', 'wb', 'r+b')[accmode]
else:
accmode = _accmode(mode)
if 'b' not in mode:
mode += 'b'
return mode, accmode
def _maybe_raw_from_fd(self, mode: str, accmode: int) -> Tuple[str, int]:
if self.fd is not None:
# noinspection PyUnresolvedReferences
if (self.raw is None or
not hasattr(self.raw, 'mode') or
_accmode(self.raw.mode) != accmode):
self.raw = io.open(self.fd,
mode=mode,
buffering=0,
closefd=False,
opener=lambda path, flags: self.fd)
elif self.raw is not None:
try:
# noinspection PyUnresolvedReferences
mode = self.raw.mode
except AttributeError:
pass
else:
accmode = _accmode(mode)
return mode, accmode
def _maybe_bytes_from_raw(self, mode: str, accmode: int) -> Tuple[str,
int]:
if self.raw is not None:
if (self.bytes is None or
not hasattr(self.bytes, 'mode') or
_accmode(self.bytes.mode) != accmode):
buffered_io = (
io.BufferedReader,
io.BufferedWriter,
io.BufferedRandom,
)[accmode]
if accmode == os.O_RDWR and not self.raw.seekable():
self.bytes = io.BufferedRWPair(self.raw, self.raw)
else:
self.bytes = buffered_io(self.raw)
elif self.bytes is not None:
try:
mode = self.bytes.mode
except AttributeError:
pass
else:
accmode = _accmode(mode)
return mode, accmode
def _maybe_text_from_bytes(self, mode: str, accmode: int) -> Tuple[str,
int]:
if self.bytes is not None:
# noinspection PyUnresolvedReferences
if (self.text is None or
not hasattr(self.text, 'mode') or
_accmode(self.text.mode) != accmode):
self.text = io.TextIOWrapper(
self.bytes,
encoding=self.encoding,
errors='replace' if self.isatty else 'strict',
line_buffering=True,
)
elif self.text is not None:
try:
# noinspection PyUnresolvedReferences
mode = self.text.mode
except AttributeError:
pass
else:
accmode = _accmode(mode)
return mode, accmode
def __eq__(self, other: '_File') -> bool:
if other is self:
return True
if not isinstance(other, _File):
return NotImplemented
if self.text is not None and other.text is self.text:
return True
if self.bytes is not None and other.bytes is self.bytes:
return True
if self.raw is not None and other.raw is self.raw:
return True
if self.fd is None or other.fd is None:
return False
if other.fd == self.fd:
return True
stat1 = os.fstat(self.fd)
stat2 = os.fstat(other.fd)
return stat1.st_dev == stat2.st_dev and stat1.st_ino == stat2.st_ino
def tty_file(self, mode: str = 'w+') -> io.TextIOBase:
if not self.isatty:
raise TypeError("must be a tty")
if self.fd is None:
raise TypeError("must have a file descriptor")
return open(os.ttyname(self.fd),
mode=mode,
encoding=self.encoding,
errors='replace')
class _CmdLineTransport(Transport):
def __init__(self,
loop: AbstractEventLoop,
protocol: Protocol,
input_handler,
output_handler,
) -> None:
super().__init__()
self._loop = loop
self._protocol = protocol
import sys
self._input = _File(sys.__stdin__, mode='r', non_blocking=True)
self._output = _File(sys.__stdout__, mode='w')
self._input_decoder = codecs.getincrementaldecoder(self._input.encoding)(errors='ignore')
self._input_buf = []
self._output_encoder = codecs.getincrementalencoder(self._output.encoding)(errors='ignore')
self._output_buf = deque()
self._saved_attr = None
self._terminal = None
self._shared = False
self._echo = None
if self._input.isatty:
self._saved_attr = termios.tcgetattr(self._input.fd)
attr = termios.tcgetattr(self._input.fd)
attr[3] &= ~termios.ICANON
attr[6][termios.VMIN] = 1
attr[6][termios.VTIME] = 0
termios.tcsetattr(self._input.fd, termios.TCSADRAIN, attr)
if self._input == self._output:
self._terminal = blessed.Terminal()
self._shared = True
else:
self._terminal = blessed.Terminal(
stream=self._input.tty_file(mode='w'),
)
self._echo = _File(self._terminal.stream, mode='w')
self._loop.call_soon(self._protocol.connection_made, self)
self._loop.call_soon(self._add_reader)
def _add_reader(self) -> None:
try:
self._loop.add_reader(self._input.fd, self._input_available)
except PermissionError:
# FIXME: handle case when file descriptor cannot be watched
pass
def _add_writer(self) -> None:
try:
import sys
print("adding writer", file=sys.stderr, flush=True)
self._loop.add_writer(self._output.fd, self._output_available)
except PermissionError:
# FIXME: handle case when file descriptor cannot be watched
import sys
print("output file descriptor cannot be watched", file=sys.stderr, flush=True)
def _input_available(self) -> None:
raw_buf = os.read(self._input.fd, 4096)
while True:
raw_line, sep, raw_buf = raw_buf.partition(b'\n')
if not sep:
break
self._input_buf.append(self._input_decoder.decode(raw_line, True))
self._input_decoder.reset()
self._loop.call_soon(self._protocol.data_received,
''.join(self._input_buf))
self._input_buf.clear()
if not raw_line:
return
self._input_buf.append(self._input_decoder.decode(raw_line, False))
def _output_available(self) -> None:
if not self._output_buf:
self._loop.remove_writer(self._output.fd)
import sys
print("fsync", file=sys.stderr, flush=True)
try:
os.fsync(self._output.fd)
except OSError:
pass
return
raw_data = self._output_buf.popleft()
if not raw_data:
import sys
print("data empty", file=sys.stderr, flush=True)
return
bytes_written = os.write(self._output.fd, raw_data[:4096])
assert 0 <= bytes_written <= len(raw_data)
if bytes_written >= len(raw_data):
import sys
print(f"complete write ({bytes_written})", file=sys.stderr, flush=True)
return
import sys
print(f"incomplete write ({bytes_written})", file=sys.stderr, flush=True)
self._output_buf.appendleft(raw_data[bytes_written:])
def close(self) -> None:
self._protocol = None
if self._saved_attr:
termios.tcsetattr(self._input.fd,
termios.TCSAFLUSH,
self._saved_attr)
def is_closing(self) -> bool:
return self._protocol is None
def set_protocol(self, protocol: Protocol) -> None:
self._protocol = protocol
def get_protocol(self) -> Protocol:
return self._protocol
def get_write_buffer_size(self):
assert False
def set_write_buffer_limits(self, high=None, low=None):
assert False
def abort(self):
assert False
def can_write_eof(self) -> bool:
return True
def write_eof(self):
raw_data = self._output_encoder.encode('', True)
self._output_encoder.reset()
if not raw_data:
return
self._output_buf.append(raw_data)
self._add_writer()
def write(self, data: str) -> None:
raw_data = self._output_encoder.encode(data, True)
self._output_encoder.reset()
if not raw_data:
return
self._output_buf.append(raw_data)
self._add_writer()
def pause_reading(self):
assert False
def resume_reading(self):
assert False
class DumbOutput:
def __init__(self, loop: AbstractEventLoop):
self._loop = loop
class DumbInput:
def __init__(self, loop: AbstractEventLoop):
self._loop = loop
@coroutine
def connect_console(
protocol_factory,
loop: AbstractEventLoop,
input_factory=DumbInput,
output_factory=DumbOutput,
) -> Tuple[BaseTransport, Protocol]:
output_handler = output_factory(loop=loop)
input_handler = input_factory(loop=loop)
transport = _CmdLineTransport(
loop=loop,
protocol=protocol_factory(),
input_handler=input_handler,
output_handler=output_handler,
)
return transport, transport.get_protocol()
def _main():
loop = get_event_loop()
class _DebugProtocol(Protocol):
def __init__(self) -> None:
self._transport = None
def connection_made(self, transport: BaseTransport) -> None:
assert self._transport is None
self._transport = transport
def connection_lost(self, exc: BaseException) -> None:
assert self._transport is not None
self._transport = None
def data_received(self, data: str) -> None:
self._transport.write(f"{data!r}\n")
def eof_received(self) -> None:
self._transport.write_eof()
connector_coroutine = connect_console(_DebugProtocol, loop=loop)
# connector_coroutine = connect_console(_DebugProtocol, loop=loop,
# input_factory=DumbInput,
# output_factory=DumbOutput)
transport, protocol = loop.run_until_complete(connector_coroutine)
try:
loop.run_forever()
finally:
transport.close()
if __name__ == '__main__':
_main()