1# resulttool - Show logs 2# 3# Copyright (c) 2019 Garmin International 4# 5# SPDX-License-Identifier: GPL-2.0-only 6# 7import os 8import resulttool.resultutils as resultutils 9 10def show_ptest(result, ptest, logger): 11 logdata = resultutils.ptestresult_get_log(result, ptest) 12 if logdata is not None: 13 print(logdata) 14 return 0 15 16 print("ptest '%s' log not found" % ptest) 17 return 1 18 19def 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 28def 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): 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) 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) 46 if not sectname.startswith("ptest"): 47 dest_dir = os.path.join(dest_dir, sectname.split(".")[0]) 48 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) 54 55 if args.raw_ptest: 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: 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') 71 return 1 72 73 for ptest in args.ptest: 74 if not show_ptest(r, ptest, logger): 75 return 1 76 77 for reproducible in args.reproducible: 78 if not show_reproducible(r, reproducible, logger): 79 return 1 80 81def 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.') 93 parser.add_argument('--reproducible', action='append', default=[], 94 help='show logs for a reproducible test') 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', 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') 104 105