When stdout buffer is full, process is paused: read stdout continuously

This commit is contained in:
nemunaire 2022-03-31 02:24:15 +02:00
parent 121da2bbe0
commit 7909a77a20

View File

@ -102,23 +102,33 @@ def extract(cnt, dest=None):
try: try:
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
with subprocess.Popen(["tar", "--no-same-owner", "--no-same-permissions", cmdline = ["tar", "--no-same-owner", "--no-same-permissions"]
("-xvC" + temp) if dest is not None else "-t", if dest is not None:
"-z" if type in ["application/gzip", "application/x-gzip"] else "", cmdline.append("-xvC")
"-J" if type in ["application/xz", "application/x-xz"] else "", cmdline.append(temp)
"-j" if type in ["application/bzip", "application/bzip2", "application/x-bzip", "application/x-bzip2"] else "", else:
"-"], env={"LANG": 'C'}, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: 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: try:
p.stdin.write(data.encode() if isinstance(data, str) else data) p.stdin.write(data.encode() if isinstance(data, str) else data)
except Exception as e: except Exception as e:
print(type) print(type)
print(p.stdout.read().decode('utf-8', 'replace')) print(p.stdout.read().decode('utf-8', 'replace'))
print(p.stderr.read().decode('utf-8', 'replace'))
raise e raise e
p.stdin.close() p.stdin.close()
p.wait()
err = p.stdout.read().decode('utf-8', 'replace') err = p.stdout.read().decode('utf-8', 'replace')
err += p.stderr.read().decode('utf-8', 'replace') p.wait()
if p.returncode == 0: if p.returncode == 0:
if dest is not None: if dest is not None:
nsub = len([x for x in os.listdir(odest) if x.find(os.path.basename(ldest) + ".") == 0]) + 1 nsub = len([x for x in os.listdir(odest) if x.find(os.path.basename(ldest) + ".") == 0]) + 1