From 92fafba3119e234f76314b0712d42bd0f1ab8122 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 10 Oct 2021 21:57:17 +0800 Subject: [PATCH] add pacroller-analyze script --- setup.py | 3 ++- src/pacroller/analyze.py | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/pacroller/analyze.py diff --git a/setup.py b/setup.py index 940e567..ddaff0b 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,8 @@ setuptools.setup( python_requires='>=3.9', entry_points={ 'console_scripts': [ - 'pacroller=pacroller.main:main' + 'pacroller=pacroller.main:main', + 'pacroller-analyze=pacroller.analyze:main' ] }, install_requires=['pyalpm'] diff --git a/src/pacroller/analyze.py b/src/pacroller/analyze.py new file mode 100644 index 0000000..c50a118 --- /dev/null +++ b/src/pacroller/analyze.py @@ -0,0 +1,48 @@ +#!/usr/bin/python + +from pacroller.config import PACMAN_LOG +from pacroller.checker import _log_parser, checkReport +from pacroller.utils import back_readline +import logging + +def main() -> None: + import argparse + parser = argparse.ArgumentParser(description='Standalone Parsing Tool for pacman.log') + parser.add_argument('-l', '--log-file', type=str, default=PACMAN_LOG, help='pacman log location') + 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('-p', '--no-package', action='store_true', help='do not show package changes') + args = parser.parse_args() + args.number = args.number if args.number >= 0 else - args.number - 1 + + logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO, + format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s') + logger = logging.getLogger() + with open(args.log_file, "rb") as f: + current_at = 0 + log = list() + for line in back_readline(f): + if not line: + continue + (_, rsrc, msg) = line.split(' ', maxsplit=2) + logger.debug(f"{rsrc=} {msg=}") + if rsrc == "[PACMAN]": + continue + log.insert(0, line) + if rsrc == "[ALPM]" and msg == "transaction started": + current_at += 1 + if current_at > args.number: + break + else: + log.clear() + else: + logger.info("number out of bound") + log.clear() + if log: + report = checkReport() + _log_parser(log, report) + print(report.summary(show_package=not args.no_package, verbose=args.verbose)) + +if __name__ == '__main__': + main()