Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # resulttool - Show logs |
| 2 | # |
| 3 | # Copyright (c) 2019 Garmin International |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
| 7 | import os |
| 8 | import resulttool.resultutils as resultutils |
| 9 | |
| 10 | def show_ptest(result, ptest, logger): |
| 11 | if 'ptestresult.sections' in result: |
| 12 | if ptest in result['ptestresult.sections'] and 'log' in result['ptestresult.sections'][ptest]: |
| 13 | print(result['ptestresult.sections'][ptest]['log']) |
| 14 | return 0 |
| 15 | |
| 16 | print("ptest '%s' not found" % ptest) |
| 17 | return 1 |
| 18 | |
| 19 | def log(args, logger): |
| 20 | results = resultutils.load_resultsdata(args.source) |
| 21 | |
| 22 | ptest_count = sum(1 for _, _, _, r in resultutils.test_run_results(results) if 'ptestresult.sections' in r) |
| 23 | if ptest_count > 1 and not args.prepend_run: |
| 24 | print("%i ptest sections found. '--prepend-run' is required" % ptest_count) |
| 25 | return 1 |
| 26 | |
| 27 | for _, run_name, _, r in resultutils.test_run_results(results): |
| 28 | if args.dump_ptest: |
| 29 | if 'ptestresult.sections' in r: |
| 30 | for name, ptest in r['ptestresult.sections'].items(): |
| 31 | if 'log' in ptest: |
| 32 | dest_dir = args.dump_ptest |
| 33 | if args.prepend_run: |
| 34 | dest_dir = os.path.join(dest_dir, run_name) |
| 35 | |
| 36 | os.makedirs(dest_dir, exist_ok=True) |
| 37 | |
| 38 | dest = os.path.join(dest_dir, '%s.log' % name) |
| 39 | print(dest) |
| 40 | with open(dest, 'w') as f: |
| 41 | f.write(ptest['log']) |
| 42 | |
| 43 | if args.raw: |
| 44 | if 'ptestresult.rawlogs' in r: |
| 45 | print(r['ptestresult.rawlogs']['log']) |
| 46 | else: |
| 47 | print('Raw logs not found') |
| 48 | return 1 |
| 49 | |
| 50 | for ptest in args.ptest: |
| 51 | if not show_ptest(r, ptest, logger): |
| 52 | return 1 |
| 53 | |
| 54 | def register_commands(subparsers): |
| 55 | """Register subcommands from this plugin""" |
| 56 | parser = subparsers.add_parser('log', help='show logs', |
| 57 | description='show the logs from test results', |
| 58 | group='analysis') |
| 59 | parser.set_defaults(func=log) |
| 60 | parser.add_argument('source', |
| 61 | help='the results file/directory/URL to import') |
| 62 | parser.add_argument('--ptest', action='append', default=[], |
| 63 | help='show logs for a ptest') |
| 64 | parser.add_argument('--dump-ptest', metavar='DIR', |
| 65 | help='Dump all ptest log files to the specified directory.') |
| 66 | parser.add_argument('--prepend-run', action='store_true', |
| 67 | help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest. |
| 68 | Required if more than one test run is present in the result file''') |
| 69 | parser.add_argument('--raw', action='store_true', |
| 70 | help='show raw logs') |
| 71 | |