orca/scripts/log.py

81 lines
1.9 KiB
Python
Raw Normal View History

import subprocess
import sys
errors = []
warnings = []
class Entry:
def __init__(self, msg):
self.msgs = [msg]
def more(self, *msgs):
if len(msgs) == 0:
msgs = [""]
for msg in msgs:
print(msg)
self.msgs.append(msg)
def log_error(msg):
msg = f"ERROR: {msg}"
print(msg)
entry = Entry(msg)
errors.append(entry)
return entry
def log_warning(msg):
msg = f"WARNING: {msg}"
print(msg)
entry = Entry(msg)
warnings.append(entry)
return entry
def log_finish():
if len(errors) + len(warnings) == 0:
print("Task completed successfully.")
return
print()
errors_str = "1 error" if len(errors) == 1 else f"{len(errors)} errors"
warnings_str = "1 warning" if len(warnings) == 1 else f"{len(warnings)} warnings"
if len(errors) > 0 and len(warnings) > 0:
print(f"Task failed with {errors_str} and {warnings_str}:")
for entry in warnings:
print("\n".join(entry.msgs))
for entry in errors:
print("\n".join(entry.msgs))
elif len(errors) > 0:
print(f"Task failed with {errors_str}:")
for entry in errors:
print("\n".join(entry.msgs))
elif len(warnings) > 0:
print(f"Task failed with {warnings_str}:")
for entry in warnings:
print("\n".join(entry.msgs))
def shellish(func):
def shellfunc(*args, **kwargs):
exitcode = 0
try:
func(*args, **kwargs)
except subprocess.CalledProcessError as err:
msg = log_error(f"The following command failed with code {err.returncode}:")
msg.more(" ".join(err.cmd))
exitcode = err.returncode
except SystemExit as err:
exitcode = err.code
except:
log_error(sys.exception())
exitcode = 1
finally:
log_finish()
exit(exitcode)
return shellfunc