| 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): | 
| Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 37 | if args.dump_ptest: | 
|  | 38 | for sectname in ['ptestresult.sections', 'ltpposixresult.sections', 'ltpresult.sections']: | 
|  | 39 | if sectname in r: | 
|  | 40 | for name, ptest in r[sectname].items(): | 
|  | 41 | logdata = resultutils.generic_get_log(sectname, r, name) | 
| Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 42 | if logdata is not None: | 
|  | 43 | dest_dir = args.dump_ptest | 
|  | 44 | if args.prepend_run: | 
|  | 45 | dest_dir = os.path.join(dest_dir, run_name) | 
| Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 46 | if not sectname.startswith("ptest"): | 
|  | 47 | dest_dir = os.path.join(dest_dir, sectname.split(".")[0]) | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 48 |  | 
| Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 49 | os.makedirs(dest_dir, exist_ok=True) | 
|  | 50 | dest = os.path.join(dest_dir, '%s.log' % name) | 
|  | 51 | print(dest) | 
|  | 52 | with open(dest, 'w') as f: | 
|  | 53 | f.write(logdata) | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 54 |  | 
| Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 55 | if args.raw_ptest: | 
| Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 56 | found = False | 
|  | 57 | for sectname in ['ptestresult.rawlogs', 'ltpposixresult.rawlogs', 'ltpresult.rawlogs']: | 
|  | 58 | rawlog = resultutils.generic_get_rawlogs(sectname, r) | 
|  | 59 | if rawlog is not None: | 
|  | 60 | print(rawlog) | 
|  | 61 | found = True | 
|  | 62 | if not found: | 
| Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 63 | print('Raw ptest logs not found') | 
|  | 64 | return 1 | 
|  | 65 |  | 
|  | 66 | if args.raw_reproducible: | 
|  | 67 | if 'reproducible.rawlogs' in r: | 
|  | 68 | print(r['reproducible.rawlogs']['log']) | 
|  | 69 | else: | 
|  | 70 | print('Raw reproducible logs not found') | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 71 | return 1 | 
|  | 72 |  | 
|  | 73 | for ptest in args.ptest: | 
|  | 74 | if not show_ptest(r, ptest, logger): | 
|  | 75 | return 1 | 
|  | 76 |  | 
| Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 77 | for reproducible in args.reproducible: | 
|  | 78 | if not show_reproducible(r, reproducible, logger): | 
|  | 79 | return 1 | 
|  | 80 |  | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 81 | def register_commands(subparsers): | 
|  | 82 | """Register subcommands from this plugin""" | 
|  | 83 | parser = subparsers.add_parser('log', help='show logs', | 
|  | 84 | description='show the logs from test results', | 
|  | 85 | group='analysis') | 
|  | 86 | parser.set_defaults(func=log) | 
|  | 87 | parser.add_argument('source', | 
|  | 88 | help='the results file/directory/URL to import') | 
|  | 89 | parser.add_argument('--ptest', action='append', default=[], | 
|  | 90 | help='show logs for a ptest') | 
|  | 91 | parser.add_argument('--dump-ptest', metavar='DIR', | 
|  | 92 | help='Dump all ptest log files to the specified directory.') | 
| Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 93 | parser.add_argument('--reproducible', action='append', default=[], | 
|  | 94 | help='show logs for a reproducible test') | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 95 | parser.add_argument('--prepend-run', action='store_true', | 
|  | 96 | help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest. | 
|  | 97 | Required if more than one test run is present in the result file''') | 
|  | 98 | parser.add_argument('--raw', action='store_true', | 
| Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 99 | help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest') | 
|  | 100 | parser.add_argument('--raw-ptest', action='store_true', | 
|  | 101 | help='show raw ptest log') | 
|  | 102 | parser.add_argument('--raw-reproducible', action='store_true', | 
|  | 103 | help='show raw reproducible build logs') | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 104 |  |