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): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 11 | logdata = resultutils.ptestresult_get_log(result, ptest) |
| 12 | if logdata is not None: |
| 13 | print(logdata) |
| 14 | return 0 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 15 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 16 | print("ptest '%s' log not found" % ptest) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 17 | return 1 |
| 18 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 19 | def show_reproducible(result, reproducible, logger): |
| 20 | try: |
| 21 | print(result['reproducible'][reproducible]['diffoscope.text']) |
| 22 | return 0 |
| 23 | |
| 24 | except KeyError: |
| 25 | print("reproducible '%s' not found" % reproducible) |
| 26 | return 1 |
| 27 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 28 | def log(args, logger): |
| 29 | results = resultutils.load_resultsdata(args.source) |
| 30 | |
| 31 | ptest_count = sum(1 for _, _, _, r in resultutils.test_run_results(results) if 'ptestresult.sections' in r) |
| 32 | if ptest_count > 1 and not args.prepend_run: |
| 33 | print("%i ptest sections found. '--prepend-run' is required" % ptest_count) |
| 34 | return 1 |
| 35 | |
| 36 | for _, run_name, _, r in resultutils.test_run_results(results): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 37 | if args.dump_ptest and 'ptestresult.sections' in r: |
| 38 | for name, ptest in r['ptestresult.sections'].items(): |
| 39 | logdata = resultutils.ptestresult_get_log(r, name) |
| 40 | if logdata is not None: |
| 41 | dest_dir = args.dump_ptest |
| 42 | if args.prepend_run: |
| 43 | dest_dir = os.path.join(dest_dir, run_name) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 44 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 45 | os.makedirs(dest_dir, exist_ok=True) |
| 46 | dest = os.path.join(dest_dir, '%s.log' % name) |
| 47 | print(dest) |
| 48 | with open(dest, 'w') as f: |
| 49 | f.write(logdata) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 50 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 51 | if args.raw_ptest: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 52 | rawlog = resultutils.ptestresult_get_rawlogs(r) |
| 53 | if rawlog is not None: |
| 54 | print(rawlog) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 55 | else: |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 56 | print('Raw ptest logs not found') |
| 57 | return 1 |
| 58 | |
| 59 | if args.raw_reproducible: |
| 60 | if 'reproducible.rawlogs' in r: |
| 61 | print(r['reproducible.rawlogs']['log']) |
| 62 | else: |
| 63 | print('Raw reproducible logs not found') |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 64 | return 1 |
| 65 | |
| 66 | for ptest in args.ptest: |
| 67 | if not show_ptest(r, ptest, logger): |
| 68 | return 1 |
| 69 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 70 | for reproducible in args.reproducible: |
| 71 | if not show_reproducible(r, reproducible, logger): |
| 72 | return 1 |
| 73 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 74 | def register_commands(subparsers): |
| 75 | """Register subcommands from this plugin""" |
| 76 | parser = subparsers.add_parser('log', help='show logs', |
| 77 | description='show the logs from test results', |
| 78 | group='analysis') |
| 79 | parser.set_defaults(func=log) |
| 80 | parser.add_argument('source', |
| 81 | help='the results file/directory/URL to import') |
| 82 | parser.add_argument('--ptest', action='append', default=[], |
| 83 | help='show logs for a ptest') |
| 84 | parser.add_argument('--dump-ptest', metavar='DIR', |
| 85 | help='Dump all ptest log files to the specified directory.') |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 86 | parser.add_argument('--reproducible', action='append', default=[], |
| 87 | help='show logs for a reproducible test') |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 88 | parser.add_argument('--prepend-run', action='store_true', |
| 89 | help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest. |
| 90 | Required if more than one test run is present in the result file''') |
| 91 | parser.add_argument('--raw', action='store_true', |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 92 | help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest') |
| 93 | parser.add_argument('--raw-ptest', action='store_true', |
| 94 | help='show raw ptest log') |
| 95 | parser.add_argument('--raw-reproducible', action='store_true', |
| 96 | help='show raw reproducible build logs') |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 97 | |