Skip to content

Commit

Permalink
version 0.4.8
Browse files Browse the repository at this point in the history
should work similar to 0.4.1 (prints lines as they come)
without the overly ambitious goal of handling all commands like status bars that rewrite previous lines
or maybe the one I was testing it on was just weird
don't think it is even possible without an overcomplicated solution since tee itself did not work

if this still doesn't work, will revert to subprocess.run behaviour of the last stable version (0.3.8)

also alternative implementation could use something like
tee = subprocess.Popen(["tee", "tmp.txt"], stdin=proc.stdout)
or
tee = subprocess.Popen(["tee", "tmp.txt"], stdin=subprocess.PIPE)
and set proc's stdout=tee.stdin
however it didn't appear to work any differently than what I have now
could be used to improve verify where there is a bash -c "cmd | tee ..." if quoting becomes an issue
  • Loading branch information
elesiuta committed Jan 19, 2021
1 parent 6f361e3 commit 1c5da34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 16 additions & 13 deletions baka.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,25 @@ def main() -> int:
else:
print("\033[91mInvalid response, exiting\033[0m")
break
stdout_bytes = io.BytesIO()
stderr_bytes = io.BytesIO()
with subprocess.Popen(cmd, stdout=stdout_bytes, stderr=stderr_bytes) as proc:
stdout_copy = []
stderr_copy = []
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as proc:
if verbosity in ["debug", "info"]:
for line in stdout_bytes:
sys.stdout.buffer.write(line)
for line in proc.stdout:
sys.stdout.write(line)
stdout_copy.append(line)
else:
stdout_copy = proc.stdout.readlines()
if verbosity in ["debug", "info", "error"]:
for line in stderr_bytes:
sys.stderr.buffer.write(line)
print("\n")
stdout_bytes.seek(0)
stderr_bytes.seek(0)
for line in proc.stderr:
sys.stderr.write(line)
stderr_copy.append(line)
print("\n")
else:
stderr_copy = proc.stderr.readlines()
command_output.append(">>> " + shlex.join(cmd))
cat = subprocess.run(["cat"], stdin=stdout_bytes, capture_output=True)
command_output.append(cat.stdout.decode().strip())
command_output.append(stderr_bytes.read().strip())
command_output.append("".join(stdout_copy).strip())
command_output.append("".join(stderr_copy).strip())
command_output.append("\n")
elif cmd[0] == "rsync":
# hide permission errors for rsync, otherwise run command normally
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="bakabakabaka",
version="0.4.7",
version="0.4.8",
description="Baka Admin's Kludge Assistant",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 1c5da34

Please sign in to comment.