From eebd1b2bdf13c41ffe04ab6e14374c1d4e40fac5 Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 5 Dec 2022 17:25:28 +0800 Subject: [PATCH] fix infinite wait on newline bug --- src/pacroller/utils.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/pacroller/utils.py b/src/pacroller/utils.py index d6e80fc..41e9c43 100644 --- a/src/pacroller/utils.py +++ b/src/pacroller/utils.py @@ -76,6 +76,7 @@ def execute_with_io(command: List[str], timeout: int = 3600, interactive: bool = pass Thread(target=set_timeout, args=(p, timeout, cleanup), daemon=True).start() output = '' + SHOW_CURSOR, HIDE_CURSOR = '\x1b[?25h', '\x1b[?25l' while p.poll() is None: try: select([ptymaster], list(), list()) @@ -89,17 +90,19 @@ def execute_with_io(command: List[str], timeout: int = 3600, interactive: bool = logger.debug(f"raw stdout: {_raw}") for b in GENERAL_NON_PRINTABLE: _raw = _raw.replace(b, b'') - need_attention = b'\x1b[?25h' in _raw raw = _raw.decode('utf-8', errors='replace') - raw = raw.replace('\r\n', '\n') - raw = ANSI_ESCAPE.sub('', raw) + raw = raw.replace('\r\n', '\n').replace(HIDE_CURSOR, '') + rawl = raw.split('\n') + if output and output[-1] != '\n': + rawl[0] = output[output.rfind('\n')+1:] + rawl[0] output += raw - rawl = (raw[:-1] if raw.endswith('\n') else raw).split('\n') - for l in rawl: + for l in rawl[:-1]: + l = ANSI_ESCAPE.sub('', l) logger.log(logging.DEBUG+1, 'STDOUT: %s', l) rstrip1 = lambda x: x[:-1] if x.endswith(' ') else x + rstrip_cursor = lambda s: rstrip1(s[:-len(SHOW_CURSOR)]) if s.endswith(SHOW_CURSOR) else f"{s}" for l in rawl: - line = rstrip1(l) + line = rstrip_cursor(l) if line == ':: Proceed with installation? [Y/n]': need_attention = False stdin.write('y\n') @@ -117,8 +120,6 @@ def execute_with_io(command: List[str], timeout: int = 3600, interactive: bool = else: terminate(p, signal=SIGINT) raise UnknownQuestionError(line, output) - if need_attention and raw: - raise UnknownQuestionError("", output) except (KeyboardInterrupt, UnknownQuestionError): terminate(p, signal=SIGINT) raise