From 7909a77a20f2fbe8d4df49a0b432b847d36e1612 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 31 Mar 2022 02:24:15 +0200 Subject: [PATCH] When stdout buffer is full, process is paused: read stdout continuously --- archive.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/archive.py b/archive.py index ea58e94..7a34639 100644 --- a/archive.py +++ b/archive.py @@ -102,23 +102,33 @@ def extract(cnt, dest=None): try: with tempfile.TemporaryDirectory() as temp: - with subprocess.Popen(["tar", "--no-same-owner", "--no-same-permissions", - ("-xvC" + temp) if dest is not None else "-t", - "-z" if type in ["application/gzip", "application/x-gzip"] else "", - "-J" if type in ["application/xz", "application/x-xz"] else "", - "-j" if type in ["application/bzip", "application/bzip2", "application/x-bzip", "application/x-bzip2"] else "", - "-"], env={"LANG": 'C'}, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: + cmdline = ["tar", "--no-same-owner", "--no-same-permissions"] + if dest is not None: + cmdline.append("-xvC") + cmdline.append(temp) + else: + cmdline.append("-t") + if type in ["application/gzip", "application/x-gzip"]: + cmdline.append("-z") + elif type in ["application/xz", "application/x-xz"]: + cmdline.append("-J") + elif type in ["application/bzip", "application/bzip2", "application/x-bzip", "application/x-bzip2"]: + cmdline.append("-j") + elif type in ["application/lzma", "application/x-lzma"]: + cmdline.append("--lzma") + elif type in ["application/zstd", "application/x-zstd"]: + cmdline.append("--zstd") + + with subprocess.Popen(cmdline, env={"LANG": 'C'}, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as p: try: p.stdin.write(data.encode() if isinstance(data, str) else data) except Exception as e: print(type) print(p.stdout.read().decode('utf-8', 'replace')) - print(p.stderr.read().decode('utf-8', 'replace')) raise e p.stdin.close() - p.wait() err = p.stdout.read().decode('utf-8', 'replace') - err += p.stderr.read().decode('utf-8', 'replace') + p.wait() if p.returncode == 0: if dest is not None: nsub = len([x for x in os.listdir(odest) if x.find(os.path.basename(ldest) + ".") == 0]) + 1