diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-03-09 01:36:20 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-03-09 01:36:20 -0500 |
commit | 2295f42d515d46aa8175a956ebbde33292d442b2 (patch) | |
tree | eea25e1a42e9ff75ad24e43ab173ce9997c58970 | |
parent | fee4186f4ad11ec78f745d13665e45fd84625420 (diff) | |
download | log_raider-2295f42d515d46aa8175a956ebbde33292d442b2.tar.gz |
Split main into cli/jenkins.py
* Add verbose_{enable, disable}
* printv function uses stderr
-rw-r--r-- | log_raider/cli/__init__.py | 0 | ||||
-rw-r--r-- | log_raider/cli/jenkins.py | 47 | ||||
-rw-r--r-- | log_raider/jenkins.py | 52 | ||||
-rw-r--r-- | setup.py | 2 |
4 files changed, 61 insertions, 40 deletions
diff --git a/log_raider/cli/__init__.py b/log_raider/cli/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/log_raider/cli/__init__.py diff --git a/log_raider/cli/jenkins.py b/log_raider/cli/jenkins.py new file mode 100644 index 0000000..27094fb --- /dev/null +++ b/log_raider/cli/jenkins.py @@ -0,0 +1,47 @@ +import argparse +import json +import os +import sys + +from .. import jenkins +from .. import __version__ + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-j", "--json", action="store_true", help="Emit JSON") + parser.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity") + parser.add_argument("-V", "--version", action="store_true", help="Show version") + parser.add_argument("logfile", nargs='?', help="Path to Jenkins log file") + + args = parser.parse_args() + + if args.version: + print(__version__) + exit(0) + + if not args.logfile: + print(f"{os.path.basename(sys.argv[0])}: error: the following arguments are required: logfile") + parser.print_help() + exit(1) + + if not os.path.exists(args.logfile): + print(f"{args.logfile}: does not exist") + + if args.verbose: + jenkins.verbose_enable() + + data = jenkins.parse_log(args.logfile) + + if args.json: + print(json.dumps(data, indent=2)) + else: + for x in data: + print("#" * 79) + print(f"{x['type']} - {x['name']} (line @ {x['line']})") + print("#" * 79) + print(f"{x['data']}") + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/log_raider/jenkins.py b/log_raider/jenkins.py index 16096a5..b2d1bfa 100644 --- a/log_raider/jenkins.py +++ b/log_raider/jenkins.py @@ -1,8 +1,5 @@ -import argparse -import json -import os import sys -from . import __version__ + PIPELINE_VERBOSE = False PIPELINE_MARKER = "[Pipeline]" @@ -13,6 +10,16 @@ PIPELINE_BLOCK_START_NOTE_END = ')' PIPELINE_BLOCK_END_NOTE = "//" +def verbose_enable(): + global PIPELINE_VERBOSE + PIPELINE_VERBOSE = True + + +def verbose_disable(): + global PIPELINE_VERBOSE + PIPELINE_VERBOSE = False + + def consume_token(s, token, l_trunc=0, r_trunc=0, strip_leading=True): """ :param s: String to scan @@ -34,7 +41,8 @@ def consume_token(s, token, l_trunc=0, r_trunc=0, strip_leading=True): def printv(*args, **kwargs): if PIPELINE_VERBOSE: - print(*args, *kwargs) + kwargs["file"] = sys.stderr + print(*args, **kwargs) def parse_log(filename): @@ -161,37 +169,3 @@ def parse_log(filename): commit(section) return result - - -def main(): - global PIPELINE_VERBOSE - parser = argparse.ArgumentParser() - parser.add_argument("-j", "--json", action="store_true", help="Emit JSON") - parser.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity") - parser.add_argument("-V", "--version", action="store_true", help="Show version") - parser.add_argument("logfile", nargs='?') - - args = parser.parse_args() - - if args.version: - print(__version__) - exit(0) - - if not os.path.exists(args.logfile): - print(f"{args.logfile}: does not exist") - - PIPELINE_VERBOSE = args.verbose - data = parse_log(args.logfile) - - if args.json: - print(json.dumps(data, indent=2)) - else: - for x in data: - print("#" * 79) - print(f"{x['type']} - {x['name']} (line @ {x['line']})") - print("#" * 79) - print(f"{x['data']}") - - -if __name__ == "__main__": - sys.exit(main()) @@ -12,7 +12,7 @@ setup( ], entry_points={ "console_scripts": [ - "log_raider_jenkins = log_raider.jenkins:main", + "log_raider_jenkins = log_raider.cli.jenkins:main", ], }, author="Joseph Hunkeler", |