update pacroller-analyze: add colors

This commit is contained in:
JerryXiao 2021-10-11 13:21:49 +08:00
parent 92fafba311
commit ef7198ec78
Signed by: Jerry
GPG key ID: 22618F758B5BE2E5
2 changed files with 64 additions and 8 deletions

View file

@ -4,6 +4,21 @@ from pacroller.config import PACMAN_LOG
from pacroller.checker import _log_parser, checkReport
from pacroller.utils import back_readline
import logging
import re
class _colors:
TITLE = '\033[96m'
PINK = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
WARN = '\033[93m'
ERROR = '\033[91m'
ENDC = '\033[0m'
class _nocolors:
def __getattr__(self, attr: str) -> str:
return ""
colors = _colors()
nocolors = _nocolors()
def main() -> None:
import argparse
@ -12,7 +27,9 @@ def main() -> None:
parser.add_argument('-d', '--debug', action='store_true', help='enable debug mode')
parser.add_argument('-v', '--verbose', action='store_true', help='show verbose report')
parser.add_argument('-n', '--number', type=int, default=0, help='which upgrade to parse')
parser.add_argument('-m', '--max', type=int, default=1, help='max numbers of upgrade to parse')
parser.add_argument('-p', '--no-package', action='store_true', help='do not show package changes')
parser.add_argument('-c', '--no-color', action='store_true', help='do not show colors')
args = parser.parse_args()
args.number = args.number if args.number >= 0 else - args.number - 1
@ -21,6 +38,7 @@ def main() -> None:
logger = logging.getLogger()
with open(args.log_file, "rb") as f:
current_at = 0
logs = list()
log = list()
for line in back_readline(f):
if not line:
@ -32,17 +50,47 @@ def main() -> None:
log.insert(0, line)
if rsrc == "[ALPM]" and msg == "transaction started":
current_at += 1
if current_at < args.number + args.max + 1:
if current_at > args.number:
logs.append(log)
log = list()
else:
log.clear()
else:
break
else:
log.clear()
else:
logger.info("number out of bound")
log.clear()
if log:
logs.append(log)
for seq, log in enumerate(logs):
logger.debug(f"report input {log=}")
report = checkReport()
_log_parser(log, report)
print(report.summary(show_package=not args.no_package, verbose=args.verbose))
c = nocolors if args.no_color else colors
summary = report.summary(show_package=not args.no_package, verbose=args.verbose).split('\n')
in_section = ""
for i, line in enumerate(summary):
if i == 0:
summary[i] = (f"{c.TITLE}=> Showing upgrade {c.PINK}{-args.number-1-seq}{c.ENDC}"
f" started at{c.ENDC} {c.PINK}{log[0].split()[0].strip('[]')}{c.ENDC}")
else:
if line[0] != ' ':
summary[i] = f"{c.GREEN}{line}{c.ENDC}"
in_section = line.strip()
else:
if not args.no_package and in_section == "Package changes:":
if args.verbose:
summary[i] = re.sub(f"(from|to) ([^ ]{{1,}})", f"\\1 {c.GREEN}\\2{c.ENDC}", summary[i])
for keyword in {"upgrade", "install", "remove"}:
if args.verbose:
summary[i] = re.sub(f"({keyword}) ([^ ]{{1,}})", f"{c.PINK}\\1{c.ENDC} {c.BLUE}\\2{c.ENDC}", summary[i])
else:
summary[i] = re.sub(f"({keyword}): (.*)", f"{c.PINK}\\1{c.ENDC} {c.BLUE}\\2{c.ENDC}", summary[i])
if in_section == "Collected Warnings:":
summary[i] = f"{c.WARN}{line}{c.ENDC}"
summary[i] = re.sub(f"(says|pacnew)", f"{c.ENDC}{c.BLUE}\\1{c.ENDC}{c.WARN}", summary[i])
elif in_section == "Collected Errors:":
summary[i] = f"{c.ERROR}{line}{c.ENDC}"
print("\n".join(summary))
if __name__ == '__main__':
main()

View file

@ -19,6 +19,8 @@ REGEX = {
'l_transaction_complete': r'transaction completed',
'l_upgrade': r'upgraded (.+) \((.+) -> (.+)\)',
'l_install': r'installed (.+) \((.+)\)',
'l_downgrade': r'downgraded (.+) \((.+) -> (.+)\)',
'l_reinstall': r'reinstalled (.+) \((.+)\)',
'l_remove': r'removed (.+) \((.+)\)',
'l_pacnew': r'warning: (.+) installed as (.+\.pacnew)',
}
@ -189,6 +191,12 @@ def _log_parser(log: List[str], report: checkReport) -> None:
elif _m := REGEX['l_remove'].match(msg):
name, old = _m.groups()
report.change(name, old, None)
elif _m := REGEX['l_downgrade'].match(msg):
name, old, new = _m.groups()
report.warn(f"downgrade {name} from {old} to {new}")
elif _m := REGEX['l_reinstall'].match(msg):
name, new = _m.groups()
report.warn(f"reinstall {name} {new}")
elif REGEX['l_transaction_start'].match(msg):
logger.debug('transaction_start')
if in_transaction == 0:
@ -230,7 +238,7 @@ def _log_parser(log: List[str], report: checkReport) -> None:
ln -= 1
break
else:
report.crit(f'ALPM {line=} is unknown')
report.crit(f'[NOM] {msg}')
elif source == 'ALPM-SCRIPTLET':
(_, _, _pmsg) = _split_log_line(log[ln-1])
if _m := REGEX['l_upgrade'].match(_pmsg):