mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
some cleanuo
This commit is contained in:
parent
4e012be3b4
commit
c5288df4b5
1 changed files with 5 additions and 72 deletions
|
@ -167,15 +167,12 @@ class BufferedFDParallelReader:
|
||||||
self.chunksize = chunksize
|
self.chunksize = chunksize
|
||||||
self.closed = False
|
self.closed = False
|
||||||
# start reading from stream
|
# start reading from stream
|
||||||
print('starting buffered read')
|
|
||||||
self.thread = threading.Thread(target=populate_buffer,
|
self.thread = threading.Thread(target=populate_buffer,
|
||||||
args=(self, fd, self.buffer, chunksize))
|
args=(self, fd, self.buffer, chunksize))
|
||||||
self.thread.daemon = True
|
self.thread.daemon = True
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PopenThread(threading.Thread):
|
class PopenThread(threading.Thread):
|
||||||
|
|
||||||
def __init__(self, *args, stdin=None, stdout=None, stderr=None, **kwargs):
|
def __init__(self, *args, stdin=None, stdout=None, stderr=None, **kwargs):
|
||||||
|
@ -187,8 +184,10 @@ class PopenThread(threading.Thread):
|
||||||
if on_main_thread():
|
if on_main_thread():
|
||||||
self.old_winch_handler = signal.signal(signal.SIGWINCH,
|
self.old_winch_handler = signal.signal(signal.SIGWINCH,
|
||||||
self._signal_winch)
|
self._signal_winch)
|
||||||
# start up process
|
env = builtins.__xonsh_env__
|
||||||
self.orig_stdin = stdin
|
self.orig_stdin = stdin
|
||||||
|
self.store_stdin = env.get('XONSH_STORE_STDIN')
|
||||||
|
# start up process
|
||||||
self.proc = proc = subprocess.Popen(*args,
|
self.proc = proc = subprocess.Popen(*args,
|
||||||
stdin=stdin,
|
stdin=stdin,
|
||||||
stdout=stdout,
|
stdout=stdout,
|
||||||
|
@ -197,13 +196,10 @@ class PopenThread(threading.Thread):
|
||||||
self._in_alt_mode = False
|
self._in_alt_mode = False
|
||||||
self.pid = proc.pid
|
self.pid = proc.pid
|
||||||
self.universal_newlines = uninew = proc.universal_newlines
|
self.universal_newlines = uninew = proc.universal_newlines
|
||||||
#self.stdin = proc.stdin if self.orig_stdin is None else stdin.buffer
|
|
||||||
#self.stdin = getattr(stdin, 'buf', proc.stdin)
|
|
||||||
if uninew:
|
if uninew:
|
||||||
env = builtins.__xonsh_env__
|
|
||||||
self.encoding = enc = env.get('XONSH_ENCODING')
|
self.encoding = enc = env.get('XONSH_ENCODING')
|
||||||
self.encoding_errors = err = env.get('XONSH_ENCODING_ERRORS')
|
self.encoding_errors = err = env.get('XONSH_ENCODING_ERRORS')
|
||||||
self.stdin = io.BytesIO()
|
self.stdin = io.BytesIO() # stdin is always bytes!
|
||||||
self.stdout = io.StringIO()
|
self.stdout = io.StringIO()
|
||||||
self.stderr = io.StringIO()
|
self.stderr = io.StringIO()
|
||||||
else:
|
else:
|
||||||
|
@ -224,17 +220,9 @@ class PopenThread(threading.Thread):
|
||||||
stdin = self.stdin
|
stdin = self.stdin
|
||||||
stdout = self.stdout
|
stdout = self.stdout
|
||||||
stderr = self.stderr
|
stderr = self.stderr
|
||||||
#if self.piped_stdin is None:
|
|
||||||
# pipein = None
|
|
||||||
#else:
|
|
||||||
# pipein = self.piped_stdin
|
|
||||||
# pipefd = pipein if isinstance(pipein, int) else pipein.fileno()
|
|
||||||
# pipein = NonBlockingFDReader(pipefd, timeout=self.timeout)
|
|
||||||
# sysin = NonBlockingFDReader(0, timeout=self.timeout)
|
|
||||||
#capin = self.captured_stdin
|
|
||||||
if self.orig_stdin is None:
|
if self.orig_stdin is None:
|
||||||
origin = None
|
origin = None
|
||||||
elif ON_POSIX:
|
elif ON_POSIX and self.store_stdin:
|
||||||
origin = self.orig_stdin
|
origin = self.orig_stdin
|
||||||
origfd = origin if isinstance(origin, int) else origin.fileno()
|
origfd = origin if isinstance(origin, int) else origin.fileno()
|
||||||
origin = BufferedFDParallelReader(origfd, buffer=stdin)
|
origin = BufferedFDParallelReader(origfd, buffer=stdin)
|
||||||
|
@ -244,78 +232,23 @@ class PopenThread(threading.Thread):
|
||||||
timeout=self.timeout)
|
timeout=self.timeout)
|
||||||
procerr = NonBlockingFDReader(self.captured_stderr.fileno(),
|
procerr = NonBlockingFDReader(self.captured_stderr.fileno(),
|
||||||
timeout=self.timeout)
|
timeout=self.timeout)
|
||||||
#print('a')
|
|
||||||
#print(self.piped_stdin)
|
|
||||||
#self._read_write_in(pipein, sysin, stdin, capin)
|
|
||||||
#self._communicate_in(origin, stdin)
|
|
||||||
self._read_write(procout, stdout, sys.stdout)
|
self._read_write(procout, stdout, sys.stdout)
|
||||||
self._read_write(procerr, stderr, sys.stderr)
|
self._read_write(procerr, stderr, sys.stderr)
|
||||||
#print('b')
|
|
||||||
while proc.poll() is None:
|
while proc.poll() is None:
|
||||||
#print('c')
|
|
||||||
#self._read_write_in(pipein, sysin, stdin, capin)
|
|
||||||
#self._communicate_in(origin, stdin)
|
|
||||||
self._read_write(procout, stdout, sys.stdout)
|
self._read_write(procout, stdout, sys.stdout)
|
||||||
self._read_write(procerr, stderr, sys.stderr)
|
self._read_write(procerr, stderr, sys.stderr)
|
||||||
#if pipein is not None and pipein.closed:
|
|
||||||
# break
|
|
||||||
if self.prevs_are_closed:
|
if self.prevs_are_closed:
|
||||||
#pipein.closed = True
|
|
||||||
break
|
break
|
||||||
time.sleep(self.timeout)
|
time.sleep(self.timeout)
|
||||||
#break
|
|
||||||
#print('d')
|
|
||||||
#print('e')
|
|
||||||
#self._read_write_in(pipein, sysin, stdin, capin)
|
|
||||||
#self._communicate_in(origin, stdin)
|
|
||||||
self._read_write(procout, stdout, sys.stdout)
|
self._read_write(procout, stdout, sys.stdout)
|
||||||
self._read_write(procerr, stderr, sys.stderr)
|
self._read_write(procerr, stderr, sys.stderr)
|
||||||
#print('f')
|
|
||||||
if proc.poll() is None:
|
if proc.poll() is None:
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
#print('g')
|
|
||||||
|
|
||||||
def _wait_for_attr(self, name):
|
def _wait_for_attr(self, name):
|
||||||
while not hasattr(self, name):
|
while not hasattr(self, name):
|
||||||
time.sleep(1e-7)
|
time.sleep(1e-7)
|
||||||
|
|
||||||
def _communicate_in(self, origin, stdbuf):
|
|
||||||
if origin is None:
|
|
||||||
return
|
|
||||||
proc = self.proc
|
|
||||||
uninew = self.universal_newlines
|
|
||||||
timeout = self.timeout
|
|
||||||
if uninew:
|
|
||||||
enc = self.encoding
|
|
||||||
err = self.encoding_errors
|
|
||||||
for line in iter(origin.readline, b''):
|
|
||||||
stdbuf.write(line)
|
|
||||||
#if uninew:
|
|
||||||
# line = line.decode(encoding=enc, errors=err)
|
|
||||||
if uninew:
|
|
||||||
proc.stdin.buffer.write(line)
|
|
||||||
else:
|
|
||||||
proc.stdin.write(line)
|
|
||||||
|
|
||||||
def _read_write_in(self, pipein, sysin, stdbuf, writer):
|
|
||||||
if pipein is None or pipein.closed: #or self._in_alt_mode:
|
|
||||||
return
|
|
||||||
for line in iter(pipein.readline, b''):
|
|
||||||
print(line)
|
|
||||||
writer.write(line)
|
|
||||||
writer.flush()
|
|
||||||
stdbuf.write(line)
|
|
||||||
stdbuf.flush()
|
|
||||||
for line in iter(sysin.readline, b''):
|
|
||||||
#print(line)
|
|
||||||
writer.write(line)
|
|
||||||
writer.flush()
|
|
||||||
#if self.prev_are_closed:
|
|
||||||
# pipein.closed = True
|
|
||||||
# writer.close()
|
|
||||||
# self.orig_stdin.close()
|
|
||||||
#print('closed')
|
|
||||||
|
|
||||||
def _read_write(self, reader, writer, stdbuf):
|
def _read_write(self, reader, writer, stdbuf):
|
||||||
for line in iter(reader.readline, b''):
|
for line in iter(reader.readline, b''):
|
||||||
self._alt_mode_switch(line, writer, stdbuf)
|
self._alt_mode_switch(line, writer, stdbuf)
|
||||||
|
|
Loading…
Add table
Reference in a new issue