Skip to content

Commit

Permalink
version 0.4.9
Browse files Browse the repository at this point in the history
handle captured output similar to version 0.3.8
if not capturing output and job is verbose, run without capture so output is live

tried tee command and tee from pypi (didn't work with subprocess) and duplicating file descriptors
none work properly for capturing commands and printing live if they rewrite previous output

also if using alternative implementation for tee with verify mentioned in previous commit
match cmd[i] == "tee" (similar to rsync) and alter the cmd there
  • Loading branch information
elesiuta committed Jan 19, 2021
1 parent 1c5da34 commit ace8b51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 22 additions & 19 deletions baka.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,15 @@ def main() -> int:
command_output.append(">>> " + shlex.join(cmd))
else:
if args.job:
# capture command output for job, otherwise run command normally
# run command as part of job, otherwise run command normally
capture_output = bool(
("write" in config.jobs[args.job] and config.jobs[args.job]["write"]) or
("email" in config.jobs[args.job] and config.jobs[args.job]["email"] and config.jobs[args.job]["email"]["to"])
)
verbosity = ""
if "verbosity" in config.jobs[args.job] and config.jobs[args.job]["verbosity"]:
verbosity = config.jobs[args.job]["verbosity"].lower()
else:
if verbosity not in ["debug", "info", "error", "silent"]:
verbosity = "debug"
if verbosity in ["debug"]:
print("\033[94m%s\033[0m" % shlex.join(cmd))
Expand All @@ -281,26 +286,24 @@ def main() -> int:
else:
print("\033[91mInvalid response, exiting\033[0m")
break
stdout_copy = []
stderr_copy = []
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) as proc:
proc_out = subprocess.PIPE
proc_err = subprocess.PIPE
if not capture_output:
if verbosity in ["debug", "info"]:
for line in proc.stdout:
sys.stdout.write(line)
stdout_copy.append(line)
else:
stdout_copy = proc.stdout.readlines()
proc_out = sys.stdout
if verbosity in ["debug", "info", "error"]:
proc_err = sys.stdout
proc = subprocess.run(cmd, stdout=proc_out, stderr=proc_err)
if capture_output:
if verbosity in ["debug", "info"]:
sys.stdout.buffer.write(proc.stdout)
if verbosity in ["debug", "info", "error"]:
for line in proc.stderr:
sys.stderr.write(line)
stderr_copy.append(line)
sys.stdout.buffer.write(proc.stderr)
print("\n")
else:
stderr_copy = proc.stderr.readlines()
command_output.append(">>> " + shlex.join(cmd))
command_output.append("".join(stdout_copy).strip())
command_output.append("".join(stderr_copy).strip())
command_output.append("\n")
command_output.append(">>> " + shlex.join(cmd))
command_output.append(proc.stdout.decode().strip())
command_output.append(proc.stderr.decode().strip())
command_output.append("\n")
elif cmd[0] == "rsync":
# hide permission errors for rsync, otherwise run command normally
proc = subprocess.run(cmd, stderr=subprocess.PIPE, universal_newlines=True)
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.8",
version="0.4.9",
description="Baka Admin's Kludge Assistant",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit ace8b51

Please sign in to comment.